Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 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
0.00%
 

Contributing tests

No tests hitting this source file were found.

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