1 package jalview.schemes;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
7 import jalview.datamodel.SequenceFeature;
11 import org.testng.annotations.Test;
13 public class FeatureColourTest
15 @Test(groups = { "Functional" })
16 public void testIsColored_simpleColour()
18 FeatureColour fc = new FeatureColour(Color.RED);
19 assertTrue(fc.isColored(new SequenceFeature()));
22 @Test(groups = { "Functional" })
23 public void testIsColored_colourByLabel()
25 FeatureColour fc = new FeatureColour();
26 fc.setColourByLabel(true);
27 assertTrue(fc.isColored(new SequenceFeature()));
30 @Test(groups = { "Functional" })
31 public void testIsColored_aboveThreshold()
33 // graduated colour range from score 20 to 100
34 FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 20f,
37 // score 0 is adjusted to bottom of range
38 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 0f,
40 assertTrue(fc.isColored(sf));
41 assertEquals(Color.WHITE, fc.getColor(sf));
43 // score 120 is adjusted to top of range
45 assertEquals(Color.BLACK, fc.getColor(sf));
47 // value below threshold is still rendered
48 // setting threshold has no effect yet...
51 assertTrue(fc.isColored(sf));
52 assertEquals(new Color(204, 204, 204), fc.getColor(sf));
54 // now apply threshold:
55 fc.setAboveThreshold(true);
56 assertFalse(fc.isColored(sf));
57 // colour is still returned though ?!?
58 assertEquals(new Color(204, 204, 204), fc.getColor(sf));
60 sf.setScore(84); // above threshold now
61 assertTrue(fc.isColored(sf));
62 assertEquals(new Color(51, 51, 51), fc.getColor(sf));
65 @Test(groups = { "Functional" })
66 public void testGetColor_simpleColour()
68 FeatureColour fc = new FeatureColour(Color.RED);
69 assertEquals(Color.RED, fc.getColor(new SequenceFeature()));
72 @Test(groups = { "Functional" })
73 public void testGetColor_colourByLabel()
75 FeatureColour fc = new FeatureColour();
76 fc.setColourByLabel(true);
77 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f,
79 Color expected = UserColourScheme.createColourFromName("desc");
80 assertEquals(expected, fc.getColor(sf));
83 @Test(groups = { "Functional" })
84 public void testGetColor_Graduated()
86 // graduated colour from score 0 to 100, gray(128, 128, 128) to red(255, 0, 0)
87 FeatureColour fc = new FeatureColour(Color.GRAY, Color.RED, 0f, 100f);
88 // feature score is 75 which is 3/4 of the way from GRAY to RED
89 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f,
91 // the colour gradient is computed in float values from 0-1 (where 1 == 255)
92 float red = 128 / 255f + 3 / 4f * (255 - 128) / 255f;
93 float green = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
94 float blue = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
95 Color expected = new Color(red, green, blue);
96 assertEquals(expected, fc.getColor(sf));
99 @Test(groups = { "Functional" })
100 public void testGetColor_belowThreshold()
102 // gradient from [50, 150] from WHITE(255, 255, 255) to BLACK(0, 0, 0)
103 FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 50f,
105 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f,
107 fc.setThreshold(100f); // ignore for now
108 assertTrue(fc.isColored(sf));
109 assertEquals(new Color(204, 204, 204), fc.getColor(sf));
111 fc.setAboveThreshold(true); // feature lies below threshold
112 assertFalse(fc.isColored(sf));
113 assertEquals(new Color(204, 204, 204), fc.getColor(sf));