JAL-98 refactor bleachColour() to ColorUtils
[jalview.git] / test / jalview / util / ColorUtilsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.util;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertSame;
26
27 import java.awt.Color;
28
29 import org.testng.annotations.Test;
30
31 public class ColorUtilsTest
32 {
33
34   Color paleColour = new Color(97, 203, 111); // pale green
35
36   Color midColour = new Color(135, 57, 41); // mid red
37
38   Color darkColour = new Color(11, 30, 50); // dark blue
39
40   @Test(groups = { "Functional" })
41   public void testDarkerThan()
42   {
43     assertEquals("Wrong darker shade", new Color(32, 69, 37),
44             ColorUtils.darkerThan(paleColour));
45     assertEquals("Wrong darker shade", new Color(45, 18, 13),
46             ColorUtils.darkerThan(midColour));
47     assertEquals("Wrong darker shade", new Color(2, 9, 16),
48             ColorUtils.darkerThan(darkColour));
49     assertNull(ColorUtils.darkerThan(null));
50   }
51
52   @Test(groups = { "Functional" })
53   public void testBrighterThan()
54   {
55     assertEquals("Wrong brighter shade", new Color(255, 255, 255), // white
56             ColorUtils.brighterThan(paleColour));
57     assertEquals("Wrong brighter shade", new Color(255, 164, 117),
58             ColorUtils.brighterThan(midColour));
59     assertEquals("Wrong brighter shade", new Color(30, 85, 144),
60             ColorUtils.brighterThan(darkColour));
61     assertNull(ColorUtils.brighterThan(null));
62   }
63
64   /**
65    * @see http://www.rtapo.com/notes/named_colors.html
66    */
67   @Test(groups = { "Functional" })
68   public void testToTkCode()
69   {
70     assertEquals("#fffafa", ColorUtils.toTkCode(new Color(255, 250, 250))); // snow
71     assertEquals("#e6e6fa", ColorUtils.toTkCode(new Color(230, 230, 250))); // lavender
72     assertEquals("#dda0dd", ColorUtils.toTkCode(new Color(221, 160, 221))); // plum
73     assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple
74     assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime
75   }
76
77   @Test(groups = { "Functional" })
78   public void testGetGraduatedColour()
79   {
80     Color minColour = new Color(100, 100, 100);
81     Color maxColour = new Color(180, 200, 220);
82
83     /*
84      * value half-way between min and max
85      */
86     Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
87             maxColour);
88     assertEquals(140, col.getRed());
89     assertEquals(150, col.getGreen());
90     assertEquals(160, col.getBlue());
91
92     /*
93      * value two-thirds of the way between min and max
94      */
95     col = ColorUtils
96             .getGraduatedColour(30f, 10f, minColour, 40f, maxColour);
97     assertEquals(153, col.getRed());
98     // Color constructor rounds float value to nearest int
99     assertEquals(167, col.getGreen());
100     assertEquals(180, col.getBlue());
101
102     /*
103      * value = min
104      */
105     col = ColorUtils
106             .getGraduatedColour(10f, 10f, minColour, 30f, maxColour);
107     assertEquals(minColour, col);
108
109     /*
110      * value = max
111      */
112     col = ColorUtils
113             .getGraduatedColour(30f, 10f, minColour, 30f, maxColour);
114     assertEquals(maxColour, col);
115
116     /*
117      * value < min
118      */
119     col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
120     assertEquals(minColour, col);
121
122     /*
123      * value > max
124      */
125     col = ColorUtils
126             .getGraduatedColour(40f, 10f, minColour, 30f, maxColour);
127     assertEquals(maxColour, col);
128
129     /*
130      * min = max
131      */
132     col = ColorUtils
133             .getGraduatedColour(40f, 10f, minColour, 10f, maxColour);
134     assertEquals(minColour, col);
135   }
136
137   @Test(groups = { "Functional" })
138   public void testBleachColour()
139   {
140     Color colour = new Color(155, 105, 55);
141     assertSame(colour, ColorUtils.bleachColour(colour, 0));
142     assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 1));
143     assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 2));
144     assertEquals(new Color(175, 135, 95),
145             ColorUtils.bleachColour(colour, 0.2f));
146     assertEquals(new Color(225, 210, 195),
147             ColorUtils.bleachColour(colour, 0.7f));
148
149     /*
150      * and some 'negative fade'
151      */
152     assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -1));
153     assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -2));
154     assertEquals(new Color(124, 84, 44),
155             ColorUtils.bleachColour(colour, -0.2f));
156     assertEquals(new Color(46, 31, 16), // with rounding down
157             ColorUtils.bleachColour(colour, -0.7f));
158   }
159 }