2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertSame;
27 import jalview.gui.JvOptionPane;
29 import java.awt.Color;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 public class ColorUtilsTest
37 @BeforeClass(alwaysRun = true)
38 public void setUpJvOptionPane()
40 JvOptionPane.setInteractiveMode(false);
41 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44 Color paleColour = new Color(97, 203, 111); // pale green
46 Color midColour = new Color(135, 57, 41); // mid red
48 Color darkColour = new Color(11, 30, 50); // dark blue
50 @Test(groups = { "Functional" })
51 public void testDarkerThan()
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));
62 @Test(groups = { "Functional" })
63 public void testBrighterThan()
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));
74 @Test(groups = { "Functional" })
75 public void testToTkCode()
77 assertEquals("#fffafa", ColorUtils.toTkCode(new Color(255, 250, 250))); // snow
78 assertEquals("#e6e6fa", ColorUtils.toTkCode(new Color(230, 230, 250))); // lavender
79 assertEquals("#dda0dd", ColorUtils.toTkCode(new Color(221, 160, 221))); // plum
80 assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple
81 assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime
84 @Test(groups = { "Functional" })
85 public void testGetGraduatedColour()
87 Color minColour = new Color(100, 100, 100);
88 Color maxColour = new Color(180, 200, 220);
91 * value half-way between min and max
93 Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
95 assertEquals(140, col.getRed());
96 assertEquals(150, col.getGreen());
97 assertEquals(160, col.getBlue());
100 * value two-thirds of the way between min and max
103 .getGraduatedColour(30f, 10f, minColour, 40f, maxColour);
104 assertEquals(153, col.getRed());
105 // Color constructor rounds float value to nearest int
106 assertEquals(167, col.getGreen());
107 assertEquals(180, col.getBlue());
113 .getGraduatedColour(10f, 10f, minColour, 30f, maxColour);
114 assertEquals(minColour, col);
120 .getGraduatedColour(30f, 10f, minColour, 30f, maxColour);
121 assertEquals(maxColour, col);
126 col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
127 assertEquals(minColour, col);
133 .getGraduatedColour(40f, 10f, minColour, 30f, maxColour);
134 assertEquals(maxColour, col);
140 .getGraduatedColour(40f, 10f, minColour, 10f, maxColour);
141 assertEquals(minColour, col);
144 @Test(groups = { "Functional" })
145 public void testBleachColour()
147 Color colour = new Color(155, 105, 55);
148 assertSame(colour, ColorUtils.bleachColour(colour, 0));
149 assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 1));
150 assertEquals(Color.WHITE, ColorUtils.bleachColour(colour, 2));
151 assertEquals(new Color(175, 135, 95),
152 ColorUtils.bleachColour(colour, 0.2f));
153 assertEquals(new Color(225, 210, 195),
154 ColorUtils.bleachColour(colour, 0.7f));
157 * and some 'negative fade'
159 assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -1));
160 assertEquals(Color.BLACK, ColorUtils.bleachColour(colour, -2));
161 assertEquals(new Color(124, 84, 44),
162 ColorUtils.bleachColour(colour, -0.2f));
163 assertEquals(new Color(46, 31, 16), // with rounding down
164 ColorUtils.bleachColour(colour, -0.7f));
167 @Test(groups = "Functional")
168 public void testParseColourString()
171 * by colour name - if known to AWT, and included in
173 * @see ColourSchemeProperty.getAWTColorFromName()
175 assertSame(Color.RED, ColorUtils.parseColourString("red"));
176 assertSame(Color.RED, ColorUtils.parseColourString("Red"));
177 assertSame(Color.RED, ColorUtils.parseColourString(" RED "));
182 String hexColour = Integer.toHexString(Color.RED.getRGB() & 0xffffff);
183 assertEquals("ff0000", hexColour);
184 assertEquals(Color.RED, ColorUtils.parseColourString(hexColour));
185 // 'hex' prefixes _not_ wanted here
186 assertNull(ColorUtils.parseColourString("0x" + hexColour));
187 assertNull(ColorUtils.parseColourString("#" + hexColour));
188 // hex values must be 6 hex digits
189 assertNull(ColorUtils.parseColourString("1000000"));
190 assertNull(ColorUtils.parseColourString("0ff00"));
195 Color c = Color.pink;
196 String rgb = String.format("%d,%d,%d", c.getRed(), c.getGreen(),
198 assertEquals("255,175,175", rgb);
199 assertEquals(c, ColorUtils.parseColourString(rgb));
200 assertEquals(c, ColorUtils.parseColourString("255, 175 , 175"));
205 assertNull(ColorUtils.parseColourString(null));
206 assertNull(ColorUtils.parseColourString("rubbish"));
207 assertNull(ColorUtils.parseColourString("-1"));
208 assertNull(ColorUtils.parseColourString(String
209 .valueOf(Integer.MAX_VALUE)));
210 assertNull(ColorUtils.parseColourString("100,200,300")); // out of range
211 assertNull(ColorUtils.parseColourString("100,200")); // too few
212 assertNull(ColorUtils.parseColourString("100,200,100,200")); // too many
215 @Test(groups = "Functional")
216 public void testGetAWTColorFromName() {
217 assertEquals(Color.white, ColorUtils.getColorFromName("white"));
218 assertEquals(Color.white, ColorUtils.getColorFromName("White"));
219 assertEquals(Color.white, ColorUtils.getColorFromName("WHITE"));
220 assertEquals(Color.pink, ColorUtils.getColorFromName("pink"));
221 assertNull(ColorUtils.getColorFromName("mauve")); // no such name
222 assertNull(ColorUtils.getColorFromName(""));
223 assertNull(ColorUtils.getColorFromName(null));
226 @Test(groups = "Functional")
227 public void testCreateColourFromName()
229 assertEquals(Color.white, ColorUtils.createColourFromName(null));
230 assertEquals(new Color(20, 20, 20), ColorUtils.createColourFromName(""));
231 assertEquals(new Color(98, 131, 171),
232 ColorUtils.createColourFromName("None")); // no special treatment!
233 assertEquals(new Color(123, 211, 122),
234 ColorUtils.createColourFromName("hello world"));
235 assertEquals(new Color(27, 147, 112),
236 ColorUtils.createColourFromName("HELLO WORLD"));
238 * the algorithm makes the same values for r,g,b if
239 * the string consists of 3 repeating substrings
241 assertEquals(new Color(184, 184, 184),
242 ColorUtils.createColourFromName("HELLO HELLO HELLO "));