2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
22 * author: Lauren Michelle Lui
27 import java.awt.Color;
28 import java.util.HashMap;
30 import java.util.Random;
32 public class ColorUtils
34 private static final int MAX_CACHE_SIZE = 1729;
36 * a cache for colours generated from text strings
38 static Map<String, Color> myColours = new HashMap<>();
41 * Generates a random color, will mix with input color. Code taken from
42 * http://stackoverflow
43 * .com/questions/43044/algorithm-to-randomly-generate-an-aesthetically
44 * -pleasing-color-palette
47 * @return Random color in RGB
49 public static final Color generateRandomColor(Color mix)
51 Random random = new Random();
52 int red = random.nextInt(256);
53 int green = random.nextInt(256);
54 int blue = random.nextInt(256);
59 red = (red + mix.getRed()) / 2;
60 green = (green + mix.getGreen()) / 2;
61 blue = (blue + mix.getBlue()) / 2;
64 Color color = new Color(red, green, blue);
70 * Convert to Tk colour code format
75 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html#
78 public static final String toTkCode(Color colour)
80 String colstring = "#" + ((colour.getRed() < 16) ? "0" : "")
81 + Integer.toHexString(colour.getRed())
82 + ((colour.getGreen() < 16) ? "0" : "")
83 + Integer.toHexString(colour.getGreen())
84 + ((colour.getBlue() < 16) ? "0" : "")
85 + Integer.toHexString(colour.getBlue());
90 * Returns a colour three shades darker. Note you can't guarantee that
91 * brighterThan reverses this, as darkerThan may result in black.
96 public static Color darkerThan(Color col)
98 return col == null ? null : col.darker().darker().darker();
102 * Returns a colour three shades brighter. Note you can't guarantee that
103 * darkerThan reverses this, as brighterThan may result in white.
108 public static Color brighterThan(Color col)
110 return col == null ? null : col.brighter().brighter().brighter();
114 * Returns a color between minColour and maxColour; the RGB values are in
115 * proportion to where 'value' lies between minValue and maxValue
124 public static Color getGraduatedColour(float value, float minValue,
125 Color minColour, float maxValue, Color maxColour)
127 if (minValue == maxValue)
131 if (value < minValue)
135 if (value > maxValue)
141 * prop = proportion of the way value is from minValue to maxValue
143 float prop = (value - minValue) / (maxValue - minValue);
144 float r = minColour.getRed()
145 + prop * (maxColour.getRed() - minColour.getRed());
146 float g = minColour.getGreen()
147 + prop * (maxColour.getGreen() - minColour.getGreen());
148 float b = minColour.getBlue()
149 + prop * (maxColour.getBlue() - minColour.getBlue());
150 return new Color(r / 255, g / 255, b / 255);
154 * 'Fades' the given colour towards white by the specified proportion. A
155 * factor of 1 or more results in White, a factor of 0 leaves the colour
156 * unchanged, and a factor between 0 and 1 results in a proportionate change
157 * of RGB values towards (255, 255, 255).
159 * A negative bleachFactor can be specified to darken the colour towards Black
163 * @param bleachFactor
166 public static Color bleachColour(Color colour, float bleachFactor)
168 if (bleachFactor >= 1f)
172 if (bleachFactor <= -1f)
176 if (bleachFactor == 0f)
181 int red = colour.getRed();
182 int green = colour.getGreen();
183 int blue = colour.getBlue();
185 if (bleachFactor > 0)
187 red += (255 - red) * bleachFactor;
188 green += (255 - green) * bleachFactor;
189 blue += (255 - blue) * bleachFactor;
190 return new Color(red, green, blue);
194 float factor = 1 + bleachFactor;
198 return new Color(red, green, blue);
203 * Parses a string into a Color, where the accepted formats are
205 * <li>an AWT colour name e.g. white</li>
206 * <li>a hex colour value (without prefix) e.g. ff0000</li>
207 * <li>an rgb triple e.g. 100,50,150</li>
211 * @return the parsed colour, or null if parsing fails
213 public static Color parseColourString(String colour)
219 colour = colour.trim();
224 int value = Integer.parseInt(colour, 16);
225 col = new Color(value);
226 } catch (NumberFormatException ex)
232 col = ColorUtils.getAWTColorFromName(colour);
239 String[] tokens = colour.split(",");
240 if (tokens.length == 3)
242 int r = Integer.parseInt(tokens[0].trim());
243 int g = Integer.parseInt(tokens[1].trim());
244 int b = Integer.parseInt(tokens[2].trim());
245 col = new Color(r, g, b);
247 } catch (Exception ex)
249 // non-numeric token or out of 0-255 range
257 * Constructs a colour from a text string. The hashcode of the whole string is
258 * scaled to the range 0-135. This is added to RGB values made from the
259 * hashcode of each third of the string, and scaled to the range 20-229.
264 public static Color createColourFromName(String name)
270 if (myColours.containsKey(name))
272 return myColours.get(name);
274 int lsize = name.length();
278 int rgbOffset = Math.abs(name.hashCode() % 10) * 15; // 0-135
283 int r = Math.abs(name.substring(start, end).hashCode() + rgbOffset)
293 * green: second third
295 int g = Math.abs(name.substring(start, end).hashCode() + rgbOffset)
301 int b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
303 Color color = new Color(r, g, b);
305 if (myColours.size() < MAX_CACHE_SIZE)
307 myColours.put(name, color);
314 * Returns the Color constant for a given colour name e.g. "pink", or null if
315 * the name is not recognised
320 public static Color getAWTColorFromName(String name)
327 name = name.toLowerCase();
329 // or make a static map; or use reflection on the field name
342 col = Color.darkGray;
351 col = Color.lightGray;