JAL-2015 JAL-1956 rollout of FeatureColourI in place of
[jalview.git] / test / jalview / schemes / FeatureColourTest.java
1 package jalview.schemes;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
6
7 import jalview.datamodel.SequenceFeature;
8
9 import java.awt.Color;
10
11 import org.testng.annotations.Test;
12
13 public class FeatureColourTest
14 {
15   @Test(groups = { "Functional" })
16   public void testIsColored_simpleColour()
17   {
18     FeatureColour fc = new FeatureColour(Color.RED);
19     assertTrue(fc.isColored(new SequenceFeature()));
20   }
21
22   @Test(groups = { "Functional" })
23   public void testIsColored_colourByLabel()
24   {
25     FeatureColour fc = new FeatureColour();
26     fc.setColourByLabel(true);
27     assertTrue(fc.isColored(new SequenceFeature()));
28   }
29
30   @Test(groups = { "Functional" })
31   public void testIsColored_aboveThreshold()
32   {
33     // graduated colour range from score 20 to 100
34     FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 20f,
35             100f);
36
37     // score 0 is adjusted to bottom of range
38     SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 0f,
39             null);
40     assertTrue(fc.isColored(sf));
41     assertEquals(Color.WHITE, fc.getColor(sf));
42
43     // score 120 is adjusted to top of range
44     sf.setScore(120f);
45     assertEquals(Color.BLACK, fc.getColor(sf));
46
47     // value below threshold is still rendered
48     // setting threshold has no effect yet...
49     fc.setThreshold(60f);
50     sf.setScore(36f);
51     assertTrue(fc.isColored(sf));
52     assertEquals(new Color(204, 204, 204), fc.getColor(sf));
53
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));
59
60     sf.setScore(84); // above threshold now
61     assertTrue(fc.isColored(sf));
62     assertEquals(new Color(51, 51, 51), fc.getColor(sf));
63   }
64
65   @Test(groups = { "Functional" })
66   public void testGetColor_simpleColour()
67   {
68     FeatureColour fc = new FeatureColour(Color.RED);
69     assertEquals(Color.RED, fc.getColor(new SequenceFeature()));
70   }
71
72   @Test(groups = { "Functional" })
73   public void testGetColor_colourByLabel()
74   {
75     FeatureColour fc = new FeatureColour();
76     fc.setColourByLabel(true);
77     SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f,
78             null);
79     Color expected = UserColourScheme.createColourFromName("desc");
80     assertEquals(expected, fc.getColor(sf));
81   }
82
83   @Test(groups = { "Functional" })
84   public void testGetColor_Graduated()
85   {
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,
90             null);
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));
97   }
98
99   @Test(groups = { "Functional" })
100   public void testGetColor_belowThreshold()
101   {
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,
104             150f);
105     SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f,
106             null);
107     fc.setThreshold(100f); // ignore for now
108     assertTrue(fc.isColored(sf));
109     assertEquals(new Color(204, 204, 204), fc.getColor(sf));
110
111     fc.setAboveThreshold(true); // feature lies below threshold
112     assertFalse(fc.isColored(sf));
113     assertEquals(new Color(204, 204, 204), fc.getColor(sf));
114   }
115 }