1 package jalview.schemes;
3 import static org.testng.Assert.assertEquals;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.GraphLine;
10 import jalview.datamodel.Sequence;
11 import jalview.datamodel.SequenceI;
13 import java.awt.Color;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.Test;
18 public class AnnotationColourGradientTest
20 final static int WIDTH = 11;
22 final static int THRESHOLD_FIVE = 5;
24 private AlignmentAnnotation ann;
26 private SequenceI seq;
28 private AlignmentI al;
30 Color minColour = new Color(50, 200, 150);
32 Color maxColour = new Color(150, 100, 250);
35 * Setup creates an annotation over 11 columns with values 0-10 and threshold
38 @BeforeClass(alwaysRun = true)
41 Annotation[] anns = new Annotation[WIDTH];
43 * set annotations with values 0-10, graded colours
45 for (int col = 0; col < WIDTH; col++)
48 Color colour = new Color(hue, hue, hue);
49 anns[col] = new Annotation("a", "a", 'a', col, colour);
52 seq = new Sequence("Seq", "");
53 al = new Alignment(new SequenceI[] { seq });
56 * AlignmentAnnotation constructor works out min-max range
58 ann = new AlignmentAnnotation("", "", anns);
59 ann.setThreshold(new GraphLine(THRESHOLD_FIVE, "", Color.RED));
60 seq.addAlignmentAnnotation(ann);
63 @Test(groups = "Functional")
64 public void testShadeCalculation_noThreshold()
66 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
67 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
68 for (int col = 0; col < WIDTH; col++)
70 Color result = testee.shadeCalculation(ann, col);
72 * column <n> is n/10 of the way from minCol to maxCol
74 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
76 assertEquals(result, expected, "for column " + col);
81 * Test the 'colour above threshold' case
83 @Test(groups = "Functional")
84 public void testShadeCalculation_aboveThreshold()
86 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
87 minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD);
88 for (int col = 0; col < WIDTH; col++)
90 Color result = testee.shadeCalculation(ann, col);
92 * colour is derived regardless of the threshold value
93 * (the renderer will suppress colouring if above/below threshold)
95 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
97 assertEquals(result, expected, "for column " + col);
101 * now make 6-10 the span of the colour range
102 * (annotation value == column number in this test)
104 testee.setThresholdIsMinMax(true);
105 for (int col = 0; col < THRESHOLD_FIVE; col++)
108 * colours below the threshold are computed as before
110 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
112 Color result = testee.shadeCalculation(ann, col);
113 assertEquals(result, expected, "for column " + col);
115 for (int col = THRESHOLD_FIVE; col < WIDTH; col++)
118 * colours for values >= threshold are graduated
119 * range is 6-10 so steps of 100/5 = 20
121 int factor = col - THRESHOLD_FIVE;
122 Color expected = new Color(50 + 20 * factor, 200 - 20 * factor,
124 Color result = testee.shadeCalculation(ann, col);
125 assertEquals(result, expected, "for column " + col);
129 * test for boundary case threshold == graphMax (JAL-3206)
131 float thresh = ann.threshold.value;
132 ann.threshold.value = ann.graphMax;
133 Color result = testee.shadeCalculation(ann, WIDTH - 1);
134 assertEquals(result, maxColour);
135 testee.setThresholdIsMinMax(false);
136 result = testee.shadeCalculation(ann, WIDTH - 1);
137 assertEquals(result, maxColour);
138 ann.threshold.value = thresh; // reset
142 * Test the 'colour below threshold' case
144 @Test(groups = "Functional")
145 public void testShadeCalculation_belowThreshold()
147 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
148 minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD);
150 for (int col = 0; col < WIDTH; col++)
152 Color result = testee.shadeCalculation(ann, col);
154 * colour is derived regardless of the threshold value
155 * (the renderer will suppress colouring if above/below threshold)
157 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
159 assertEquals(result, expected, "for column " + col);
163 * now make 0-5 the span of the colour range
164 * (annotation value == column number in this test)
166 testee.setThresholdIsMinMax(true);
167 for (int col = THRESHOLD_FIVE + 1; col < WIDTH; col++)
170 * colours above the threshold are computed as before
172 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
174 Color result = testee.shadeCalculation(ann, col);
175 assertEquals(result, expected, "for column " + col);
178 for (int col = 0; col <= THRESHOLD_FIVE; col++)
181 * colours for values <= threshold are graduated
182 * range is 0-5 so steps of 100/5 = 20
184 Color expected = new Color(50 + 20 * col, 200 - 20 * col,
186 Color result = testee.shadeCalculation(ann, col);
187 assertEquals(result, expected, "for column " + col);
191 * test for boundary case threshold == graphMin (JAL-3206)
193 float thresh = ann.threshold.value;
194 ann.threshold.value = ann.graphMin;
195 Color result = testee.shadeCalculation(ann, 0);
196 assertEquals(result, minColour);
197 testee.setThresholdIsMinMax(false);
198 result = testee.shadeCalculation(ann, 0);
199 assertEquals(result, minColour);
200 ann.threshold.value = thresh; // reset
204 * Test the 'colour above threshold' case
206 @Test(groups = "Functional")
207 public void testFindColour_aboveThreshold()
209 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
210 minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD);
211 testee = (AnnotationColourGradient) testee.getInstance(null, al);
213 for (int col = 0; col < WIDTH; col++)
215 Color result = testee.findColour('a', col, seq);
217 * expect white at or below threshold of 5
219 Color expected = col <= 5 ? Color.white
220 : new Color(50 + 10 * col, 200 - 10 * col, 150 + 10 * col);
221 assertEquals(result, expected, "for column " + col);
225 * now make 6-10 the span of the colour range
226 * (annotation value == column number in this test)
228 testee.setThresholdIsMinMax(true);
229 for (int col = 0; col < WIDTH; col++)
232 * colours for values > threshold are graduated
233 * range is 6-10 so steps of 100/5 = 20
235 int factor = col - THRESHOLD_FIVE;
236 Color expected = col <= 5 ? Color.white
237 : new Color(50 + 20 * factor, 200 - 20 * factor,
239 Color result = testee.findColour('a', col, seq);
240 assertEquals(result, expected, "for column " + col);
245 * Test the 'colour below threshold' case
247 @Test(groups = "Functional")
248 public void testFindColour_belowThreshold()
250 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
251 minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD);
252 testee = (AnnotationColourGradient) testee.getInstance(null, al);
254 for (int col = 0; col < WIDTH; col++)
256 Color result = testee.findColour('a', col, seq);
257 Color expected = col >= 5 ? Color.white
258 : new Color(50 + 10 * col, 200 - 10 * col, 150 + 10 * col);
259 assertEquals(result, expected, "for column " + col);
263 * now make 0-5 the span of the colour range
264 * (annotation value == column number in this test)
266 testee.setThresholdIsMinMax(true);
267 for (int col = 0; col < WIDTH; col++)
270 * colours for values < threshold are graduated
271 * range is 0-5 so steps of 100/5 = 20
273 Color expected = col >= 5 ? Color.white
274 : new Color(50 + 20 * col, 200 - 20 * col, 150 + 20 * col);
275 Color result = testee.findColour('a', col, seq);
276 assertEquals(result, expected, "for column " + col);
280 @Test(groups = "Functional")
281 public void testFindColour_noThreshold()
283 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
284 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
285 testee = (AnnotationColourGradient) testee.getInstance(null, al);
287 for (int col = 0; col < WIDTH; col++)
289 Color result = testee.findColour('a', col, seq);
291 * column <n> is n/10 of the way from minCol to maxCol
293 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
295 assertEquals(result, expected, "for column " + col);
299 @Test(groups = "Functional")
300 public void testFindColour_originalColours()
302 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
303 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
304 testee = (AnnotationColourGradient) testee.getInstance(null, al);
307 * flag corresponding to 'use original colours' checkbox
308 * - just use the individual annotation colours
310 testee.setPredefinedColours(true);
313 * the annotation colour is returned, except for column 0 where it is
314 * black - in this case the colour scheme colour overrides it
316 for (int col = 0; col < WIDTH; col++)
319 Color c = col == 0 ? minColour : new Color(hue, hue, hue);
320 assertEquals(testee.findColour('a', col, seq), c,
321 "for column " + col);