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));
75 * @see http://www.rtapo.com/notes/named_colors.html
77 @Test(groups = { "Functional" })
78 public void testToTkCode()
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
87 @Test(groups = { "Functional" })
88 public void testGetGraduatedColour()
90 Color minColour = new Color(100, 100, 100);
91 Color maxColour = new Color(180, 200, 220);
94 * value half-way between min and max
96 Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
98 assertEquals(140, col.getRed());
99 assertEquals(150, col.getGreen());
100 assertEquals(160, col.getBlue());
103 * value two-thirds of the way between min and max
106 .getGraduatedColour(30f, 10f, minColour, 40f, 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());
116 .getGraduatedColour(10f, 10f, minColour, 30f, maxColour);
117 assertEquals(minColour, col);
123 .getGraduatedColour(30f, 10f, minColour, 30f, maxColour);
124 assertEquals(maxColour, col);
129 col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
130 assertEquals(minColour, col);
136 .getGraduatedColour(40f, 10f, minColour, 30f, maxColour);
137 assertEquals(maxColour, col);
143 .getGraduatedColour(40f, 10f, minColour, 10f, maxColour);
144 assertEquals(minColour, col);
147 @Test(groups = { "Functional" })
148 public void testBleachColour()
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));
160 * and some 'negative fade'
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));