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 // constant borrowed from java.awt.Color
35 private static final float FACTOR = 0.7f;
37 private static final int MAX_CACHE_SIZE = 1729;
39 * a cache for colours generated from text strings
41 static Map<String, Color> myColours = new HashMap<>();
44 * Generates a random color, will mix with input color. Code taken from
45 * http://stackoverflow
46 * .com/questions/43044/algorithm-to-randomly-generate-an-aesthetically
47 * -pleasing-color-palette
50 * @return Random color in RGB
52 public static final Color generateRandomColor(Color mix)
54 Random random = new Random();
55 int red = random.nextInt(256);
56 int green = random.nextInt(256);
57 int blue = random.nextInt(256);
62 red = (red + mix.getRed()) / 2;
63 green = (green + mix.getGreen()) / 2;
64 blue = (blue + mix.getBlue()) / 2;
67 Color color = new Color(red, green, blue);
73 * Convert to Tk colour code format
78 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html#
81 public static final String toTkCode(Color colour)
83 String colstring = "#" + ((colour.getRed() < 16) ? "0" : "")
84 + Integer.toHexString(colour.getRed())
85 + ((colour.getGreen() < 16) ? "0" : "")
86 + Integer.toHexString(colour.getGreen())
87 + ((colour.getBlue() < 16) ? "0" : "")
88 + Integer.toHexString(colour.getBlue());
93 * Returns a colour three shades darker. Note you can't guarantee that
94 * brighterThan reverses this, as darkerThan may result in black.
99 public static Color darkerThan(Color col)
101 return col == null ? null : col.darker().darker().darker();
105 * Returns a colour three shades brighter. Note you can't guarantee that
106 * darkerThan reverses this, as brighterThan may result in white.
111 public static Color brighterThan(Color col)
113 return col == null ? null : col.brighter().brighter().brighter();
117 * Returns a color between minColour and maxColour; the RGB values are in
118 * proportion to where 'value' lies between minValue and maxValue
127 public static Color getGraduatedColour(float value, float minValue,
128 Color minColour, float maxValue, Color maxColour)
130 if (minValue == maxValue)
134 if (value < minValue)
138 if (value > maxValue)
144 * prop = proportion of the way value is from minValue to maxValue
146 float prop = (value - minValue) / (maxValue - minValue);
147 float r = minColour.getRed()
148 + prop * (maxColour.getRed() - minColour.getRed());
149 float g = minColour.getGreen()
150 + prop * (maxColour.getGreen() - minColour.getGreen());
151 float b = minColour.getBlue()
152 + prop * (maxColour.getBlue() - minColour.getBlue());
153 return new Color(r / 255, g / 255, b / 255);
157 * 'Fades' the given colour towards white by the specified proportion. A
158 * factor of 1 or more results in White, a factor of 0 leaves the colour
159 * unchanged, and a factor between 0 and 1 results in a proportionate change
160 * of RGB values towards (255, 255, 255).
162 * A negative bleachFactor can be specified to darken the colour towards Black
166 * @param bleachFactor
169 public static Color bleachColour(Color colour, float bleachFactor)
171 if (bleachFactor >= 1f)
175 if (bleachFactor <= -1f)
179 if (bleachFactor == 0f)
184 int red = colour.getRed();
185 int green = colour.getGreen();
186 int blue = colour.getBlue();
188 if (bleachFactor > 0)
190 red += (255 - red) * bleachFactor;
191 green += (255 - green) * bleachFactor;
192 blue += (255 - blue) * bleachFactor;
193 return new Color(red, green, blue);
197 float factor = 1 + bleachFactor;
201 return new Color(red, green, blue);
206 * Parses a string into a Color, where the accepted formats are
208 * <li>an AWT colour name e.g. white</li>
209 * <li>a hex colour value (without prefix) e.g. ff0000</li>
210 * <li>an rgb triple e.g. 100,50,150</li>
214 * @return the parsed colour, or null if parsing fails
216 public static Color parseColourString(String colour)
222 colour = colour.trim();
227 int value = Integer.parseInt(colour, 16);
228 col = new Color(value);
229 } catch (NumberFormatException ex)
235 col = ColorUtils.getAWTColorFromName(colour);
242 String[] tokens = colour.split(",");
243 if (tokens.length == 3)
245 int r = Integer.parseInt(tokens[0].trim());
246 int g = Integer.parseInt(tokens[1].trim());
247 int b = Integer.parseInt(tokens[2].trim());
248 col = new Color(r, g, b);
250 } catch (Exception ex)
252 // non-numeric token or out of 0-255 range
260 * Constructs a colour from a text string. The hashcode of the whole string is
261 * scaled to the range 0-135. This is added to RGB values made from the
262 * hashcode of each third of the string, and scaled to the range 20-229.
267 public static Color createColourFromName(String name)
273 if (myColours.containsKey(name))
275 return myColours.get(name);
277 int lsize = name.length();
281 int rgbOffset = Math.abs(name.hashCode() % 10) * 15; // 0-135
286 int r = Math.abs(name.substring(start, end).hashCode() + rgbOffset)
296 * green: second third
298 int g = Math.abs(name.substring(start, end).hashCode() + rgbOffset)
304 int b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
306 Color color = new Color(r, g, b);
308 if (myColours.size() < MAX_CACHE_SIZE)
310 myColours.put(name, color);
317 * Returns the Color constant for a given colour name e.g. "pink", or null if
318 * the name is not recognised
323 public static Color getAWTColorFromName(String name)
330 name = name.toLowerCase();
332 // or make a static map; or use reflection on the field name
345 col = Color.darkGray;
354 col = Color.lightGray;
380 * Generates a colour that is interpolated between
381 * <code>colour.darker()</code> and <code>colour.brighter()</code> in
382 * proportion as <code>value</code> is between <code>min</code> and
383 * <code>max</code>. Note that the 'neutral point' (unchanged colour) is
384 * closer to 'brighter' than to 'darker'as this is a geometric range.
392 public static Color getGraduatedColour(float value, float min, float max,
396 * this computes the equivalent of
397 * getGraduatedColour(value, min, colour.darker(), max, colour.brighter())
398 * but avoiding object creation except for the return value
409 int r = colour.getRed();
410 int g = colour.getGreen();
411 int b = colour.getBlue();
414 * rgb for colour.darker():
416 float minR = r * FACTOR;
417 float minG = g * FACTOR;
418 float minB = b * FACTOR;
421 * rgb for colour.brighter():
423 float maxR = Math.min(255f, r / FACTOR);
424 float maxG = Math.min(255f, g / FACTOR);
425 float maxB = Math.min(255f, b / FACTOR);
430 float p = (value - min) / (max - min);
431 int newR = (int) (minR + p * (maxR - minR));
432 int newG = (int) (minG + p * (maxG - minG));
433 int newB = (int) (minB + p * (maxB - minB));
435 return new Color(newR, newG, newB, colour.getAlpha());