JAL-2094 first pass with jalview.api.ColorI interface
[jalview.git] / src / jalview / util / ColorUtils.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 /**
22  * author: Lauren Michelle Lui
23  */
24
25 package jalview.util;
26
27 import jalview.api.ColorI;
28
29 import java.awt.Color;
30 import java.util.Random;
31
32 public class ColorUtils
33 {
34
35   public static Color getColor(ColorI c)
36   {
37     return new Color(c.getRed(), c.getGreen(), c.getBlue());
38   }
39   /**
40    * Generates a random color, will mix with input color. Code taken from
41    * http://stackoverflow
42    * .com/questions/43044/algorithm-to-randomly-generate-an-aesthetically
43    * -pleasing-color-palette
44    * 
45    * @param mix
46    * @return Random color in RGB
47    */
48   public static final Color generateRandomColor(Color mix)
49   {
50     Random random = new Random();
51     int red = random.nextInt(256);
52     int green = random.nextInt(256);
53     int blue = random.nextInt(256);
54
55     // mix the color
56     if (mix != null)
57     {
58       red = (red + mix.getRed()) / 2;
59       green = (green + mix.getGreen()) / 2;
60       blue = (blue + mix.getBlue()) / 2;
61     }
62
63     Color color = new Color(red, green, blue);
64     return color;
65
66   }
67
68   /**
69    * Convert to Tk colour code format
70    * 
71    * @param colour
72    * @return
73    * @see http
74    *      ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html#
75    *      tkcode
76    */
77   public static final String toTkCode(ColorI colour)
78   {
79     String colstring = "#" + ((colour.getRed() < 16) ? "0" : "")
80             + Integer.toHexString(colour.getRed())
81             + ((colour.getGreen() < 16) ? "0" : "")
82             + Integer.toHexString(colour.getGreen())
83             + ((colour.getBlue() < 16) ? "0" : "")
84             + Integer.toHexString(colour.getBlue());
85     return colstring;
86   }
87
88   /**
89    * Returns a colour three shades darker. Note you can't guarantee that
90    * brighterThan reverses this, as darkerThan may result in black.
91    * 
92    * @param col
93    * @return
94    */
95   public static Color darkerThan(Color col)
96   {
97     return col == null ? null : col.darker().darker().darker();
98   }
99
100   /**
101    * Returns a colour three shades brighter. Note you can't guarantee that
102    * darkerThan reverses this, as brighterThan may result in white.
103    * 
104    * @param col
105    * @return
106    */
107   public static Color brighterThan(Color col)
108   {
109     return col == null ? null : col.brighter().brighter().brighter();
110   }
111
112   /**
113    * Returns a color between minColour and maxColour; the RGB values are in
114    * proportion to where 'value' lies between minValue and maxValue
115    * 
116    * @param value
117    * @param minValue
118    * @param minColour
119    * @param maxValue
120    * @param maxColour
121    * @return
122    */
123   public static Color getGraduatedColour(float value, float minValue,
124           Color minColour, float maxValue, Color maxColour)
125   {
126     if (minValue == maxValue)
127     {
128       return minColour;
129     }
130     if (value < minValue)
131     {
132       value = minValue;
133     }
134     if (value > maxValue)
135     {
136       value = maxValue;
137     }
138
139     /*
140      * prop = proportion of the way value is from minValue to maxValue
141      */
142     float prop = (value - minValue) / (maxValue - minValue);
143     float r = minColour.getRed() + prop
144             * (maxColour.getRed() - minColour.getRed());
145     float g = minColour.getGreen() + prop
146             * (maxColour.getGreen() - minColour.getGreen());
147     float b = minColour.getBlue() + prop
148             * (maxColour.getBlue() - minColour.getBlue());
149     return new Color(r / 255, g / 255, b / 255);
150   }
151 }