JAL-3438 spotless for 2.11.2.0
[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 jalview.gui.JvOptionPane;
28
29 import java.awt.Color;
30
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
33
34 public class ColorUtilsTest
35 {
36
37   @BeforeClass(alwaysRun = true)
38   public void setUpJvOptionPane()
39   {
40     JvOptionPane.setInteractiveMode(false);
41     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
42   }
43
44   Color paleColour = new Color(97, 203, 111); // pale green
45
46   Color midColour = new Color(135, 57, 41); // mid red
47
48   Color darkColour = new Color(11, 30, 50); // dark blue
49
50   @Test(groups = { "Functional" })
51   public void testDarkerThan()
52   {
53     assertEquals("Wrong darker shade", new Color(32, 69, 37),
54             ColorUtils.darkerThan(paleColour));
55     assertEquals("Wrong darker shade", new Color(45, 18, 13),
56             ColorUtils.darkerThan(midColour));
57     assertEquals("Wrong darker shade", new Color(2, 9, 16),
58             ColorUtils.darkerThan(darkColour));
59     assertNull(ColorUtils.darkerThan(null));
60   }
61
62   @Test(groups = { "Functional" })
63   public void testBrighterThan()
64   {
65     assertEquals("Wrong brighter shade", new Color(255, 255, 255), // white
66             ColorUtils.brighterThan(paleColour));
67     assertEquals("Wrong brighter shade", new Color(255, 164, 117),
68             ColorUtils.brighterThan(midColour));
69     assertEquals("Wrong brighter shade", new Color(30, 85, 144),
70             ColorUtils.brighterThan(darkColour));
71     assertNull(ColorUtils.brighterThan(null));
72   }
73
74   /**
75    * @see http://www.rtapo.com/notes/named_colors.html
76    */
77   @Test(groups = { "Functional" })
78   public void testToTkCode()
79   {
80     assertEquals("#fffafa", ColorUtils.toTkCode(new Color(255, 250, 250))); // snow
81     assertEquals("#e6e6fa", ColorUtils.toTkCode(new Color(230, 230, 250))); // lavender
82     assertEquals("#dda0dd", ColorUtils.toTkCode(new Color(221, 160, 221))); // plum
83     assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple
84     assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime
85   }
86
87   @Test(groups = { "Functional" })
88   public void testGetGraduatedColour()
89   {
90     Color minColour = new Color(100, 100, 100);
91     Color maxColour = new Color(180, 200, 220);
92
93     /*
94      * value half-way between min and max
95      */
96     Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
97             maxColour);
98     assertEquals(140, col.getRed());
99     assertEquals(150, col.getGreen());
100     assertEquals(160, col.getBlue());
101
102     /*
103      * value two-thirds of the way between min and max
104      */
105     col = ColorUtils.getGraduatedColour(30f, 10f, minColour, 40f,
106             maxColour);
107     assertEquals(153, col.getRed());
108     // Color constructor rounds float value to nearest int
109     assertEquals(167, col.getGreen());
110     assertEquals(180, col.getBlue());
111
112     /*
113      * value = min
114      */
115     col = ColorUtils.getGraduatedColour(10f, 10f, minColour, 30f,
116             maxColour);
117     assertEquals(minColour, col);
118
119     /*
120      * value = max
121      */
122     col = ColorUtils.getGraduatedColour(30f, 10f, minColour, 30f,
123             maxColour);
124     assertEquals(maxColour, col);
125
126     /*
127      * value < min
128      */
129     col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
130     assertEquals(minColour, col);
131
132     /*
133      * value > max
134      */
135     col = ColorUtils.getGraduatedColour(40f, 10f, minColour, 30f,
136             maxColour);
137     assertEquals(maxColour, col);
138
139     /*
140      * min = max
141      */
142     col = ColorUtils.getGraduatedColour(40f, 10f, minColour, 10f,
143             maxColour);
144     assertEquals(minColour, col);
145   }
146
147   @Test(groups = { "Functional" })
148   public void testBleachColour()
149   {
150     Color colour = new Color(155, 105, 55);
151     assertSame(colour, ColorUtils.bleachColour(colour, 0));
152     assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 1));
153     assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 2));
154     assertEquals(new Color(175, 135, 95),
155             ColorUtils.bleachColour(colour, 0.2f));
156     assertEquals(new Color(225, 210, 195),
157             ColorUtils.bleachColour(colour, 0.7f));
158
159     /*
160      * and some 'negative fade'
161      */
162     assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -1));
163     assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -2));
164     assertEquals(new Color(124, 84, 44),
165             ColorUtils.bleachColour(colour, -0.2f));
166     assertEquals(new Color(46, 31, 16), // with rounding down
167             ColorUtils.bleachColour(colour, -0.7f));
168   }
169
170   @Test(groups = "Functional")
171   public void testParseColourString()
172   {
173     /*
174      * by colour name - if known to AWT, and included in
175      * 
176      * @see ColourSchemeProperty.getAWTColorFromName()
177      */
178     assertSame(Color.RED, ColorUtils.parseColourString("red"));
179     assertSame(Color.RED, ColorUtils.parseColourString("Red"));
180     assertSame(Color.RED, ColorUtils.parseColourString(" RED "));
181
182     /*
183      * by RGB hex code
184      */
185     String hexColour = Integer.toHexString(Color.RED.getRGB() & 0xffffff);
186     assertEquals("ff0000", hexColour);
187     assertEquals(Color.RED, ColorUtils.parseColourString(hexColour));
188     // 'hex' prefixes _not_ wanted here
189     assertNull(ColorUtils.parseColourString("0x" + hexColour));
190     assertNull(ColorUtils.parseColourString("#" + hexColour));
191     // out of range, but Color constructor just or's the rgb value with 0
192     assertEquals(Color.black, ColorUtils.parseColourString("1000000"));
193
194     /*
195      * by RGB triplet
196      */
197     Color c = Color.pink;
198     String rgb = String.format("%d,%d,%d", c.getRed(), c.getGreen(),
199             c.getBlue());
200     assertEquals("255,175,175", rgb);
201     assertEquals(c, ColorUtils.parseColourString(rgb));
202     assertEquals(c, ColorUtils.parseColourString("255, 175 , 175"));
203
204     /*
205      * odds and ends
206      */
207     assertNull(ColorUtils.parseColourString(null));
208     assertNull(ColorUtils.parseColourString("rubbish"));
209     assertEquals(Color.WHITE, ColorUtils.parseColourString("-1"));
210     assertNull(ColorUtils
211             .parseColourString(String.valueOf(Integer.MAX_VALUE)));
212     assertNull(ColorUtils.parseColourString("100,200,300")); // out of range
213     assertNull(ColorUtils.parseColourString("100,200")); // too few
214     assertNull(ColorUtils.parseColourString("100,200,100,200")); // too many
215   }
216
217   @Test(groups = "Functional")
218   public void testGetAWTColorFromName()
219   {
220     assertEquals(Color.white, ColorUtils.getAWTColorFromName("white"));
221     assertEquals(Color.white, ColorUtils.getAWTColorFromName("White"));
222     assertEquals(Color.white, ColorUtils.getAWTColorFromName("WHITE"));
223     assertEquals(Color.pink, ColorUtils.getAWTColorFromName("pink"));
224     assertNull(ColorUtils.getAWTColorFromName("mauve")); // no such name
225     assertNull(ColorUtils.getAWTColorFromName(""));
226     assertNull(ColorUtils.getAWTColorFromName(null));
227   }
228
229   @Test(groups = "Functional")
230   public void testCreateColourFromName()
231   {
232     assertEquals(Color.white, ColorUtils.createColourFromName(null));
233     assertEquals(new Color(20, 20, 20),
234             ColorUtils.createColourFromName(""));
235     assertEquals(new Color(98, 131, 171),
236             ColorUtils.createColourFromName("None")); // no special treatment!
237     assertEquals(new Color(123, 211, 122),
238             ColorUtils.createColourFromName("hello world"));
239     assertEquals(new Color(27, 147, 112),
240             ColorUtils.createColourFromName("HELLO WORLD"));
241     /*
242      * the algorithm makes the same values for r,g,b if 
243      * the string consists of 3 repeating substrings
244      */
245     assertEquals(new Color(184, 184, 184),
246             ColorUtils.createColourFromName("HELLO HELLO HELLO "));
247   }
248 }