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