Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 17:27:16 GMT
  2. Package jalview.util

File ColorUtilsTest.java

 

Code metrics

0
79
9
1
248
152
9
0.11
8.78
9
1

Classes

Class Line # Actions
ColorUtilsTest 34 79 9
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

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