JAL-2089 patch broken merge to master for Release 2.10.0b1
[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
26 import java.awt.Color;
27
28 import org.testng.annotations.Test;
29
30 public class ColorUtilsTest
31 {
32
33   Color paleColour = new Color(97, 203, 111); // pale green
34
35   Color midColour = new Color(135, 57, 41); // mid red
36
37   Color darkColour = new Color(11, 30, 50); // dark blue
38
39   @Test(groups = { "Functional" })
40   public void testDarkerThan()
41   {
42     assertEquals("Wrong darker shade", new Color(32, 69, 37),
43             ColorUtils.darkerThan(paleColour));
44     assertEquals("Wrong darker shade", new Color(45, 18, 13),
45             ColorUtils.darkerThan(midColour));
46     assertEquals("Wrong darker shade", new Color(2, 9, 16),
47             ColorUtils.darkerThan(darkColour));
48     assertNull(ColorUtils.darkerThan(null));
49   }
50
51   @Test(groups = { "Functional" })
52   public void testBrighterThan()
53   {
54     assertEquals("Wrong brighter shade", new Color(255, 255, 255), // white
55             ColorUtils.brighterThan(paleColour));
56     assertEquals("Wrong brighter shade", new Color(255, 164, 117),
57             ColorUtils.brighterThan(midColour));
58     assertEquals("Wrong brighter shade", new Color(30, 85, 144),
59             ColorUtils.brighterThan(darkColour));
60     assertNull(ColorUtils.brighterThan(null));
61   }
62
63   /**
64    * @see http://www.rtapo.com/notes/named_colors.html
65    */
66   @Test(groups = { "Functional" })
67   public void testToTkCode()
68   {
69     assertEquals("#fffafa", ColorUtils.toTkCode(new Color(255, 250, 250))); // snow
70     assertEquals("#e6e6fa", ColorUtils.toTkCode(new Color(230, 230, 250))); // lavender
71     assertEquals("#dda0dd", ColorUtils.toTkCode(new Color(221, 160, 221))); // plum
72     assertEquals("#800080", ColorUtils.toTkCode(new Color(128, 0, 128))); // purple
73     assertEquals("#00ff00", ColorUtils.toTkCode(new Color(0, 255, 0))); // lime
74   }
75
76   @Test(groups = { "Functional" })
77   public void testGetGraduatedColour()
78   {
79     Color minColour = new Color(100, 100, 100);
80     Color maxColour = new Color(180, 200, 220);
81
82     /*
83      * value half-way between min and max
84      */
85     Color col = ColorUtils.getGraduatedColour(20f, 10f, minColour, 30f,
86             maxColour);
87     assertEquals(140, col.getRed());
88     assertEquals(150, col.getGreen());
89     assertEquals(160, col.getBlue());
90
91     /*
92      * value two-thirds of the way between min and max
93      */
94     col = ColorUtils
95             .getGraduatedColour(30f, 10f, minColour, 40f, maxColour);
96     assertEquals(153, col.getRed());
97     // Color constructor rounds float value to nearest int
98     assertEquals(167, col.getGreen());
99     assertEquals(180, col.getBlue());
100
101     /*
102      * value = min
103      */
104     col = ColorUtils
105             .getGraduatedColour(10f, 10f, minColour, 30f, maxColour);
106     assertEquals(minColour, col);
107
108     /*
109      * value = max
110      */
111     col = ColorUtils
112             .getGraduatedColour(30f, 10f, minColour, 30f, maxColour);
113     assertEquals(maxColour, col);
114
115     /*
116      * value < min
117      */
118     col = ColorUtils.getGraduatedColour(0f, 10f, minColour, 30f, maxColour);
119     assertEquals(minColour, col);
120
121     /*
122      * value > max
123      */
124     col = ColorUtils
125             .getGraduatedColour(40f, 10f, minColour, 30f, maxColour);
126     assertEquals(maxColour, col);
127
128     /*
129      * min = max
130      */
131     col = ColorUtils
132             .getGraduatedColour(40f, 10f, minColour, 10f, maxColour);
133     assertEquals(minColour, col);
134   }
135 }