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.Random;
30 public class ColorUtils
34 * Generates a random color, will mix with input color. Code taken from
35 * http://stackoverflow
36 * .com/questions/43044/algorithm-to-randomly-generate-an-aesthetically
37 * -pleasing-color-palette
40 * @return Random color in RGB
42 public static final Color generateRandomColor(Color mix)
44 Random random = new Random();
45 int red = random.nextInt(256);
46 int green = random.nextInt(256);
47 int blue = random.nextInt(256);
52 red = (red + mix.getRed()) / 2;
53 green = (green + mix.getGreen()) / 2;
54 blue = (blue + mix.getBlue()) / 2;
57 Color color = new Color(red, green, blue);
63 * Convert to Tk colour code format
68 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html#
71 public static final String toTkCode(Color colour)
73 String colstring = "#" + ((colour.getRed() < 16) ? "0" : "")
74 + Integer.toHexString(colour.getRed())
75 + ((colour.getGreen() < 16) ? "0" : "")
76 + Integer.toHexString(colour.getGreen())
77 + ((colour.getBlue() < 16) ? "0" : "")
78 + Integer.toHexString(colour.getBlue());
83 * Returns a colour three shades darker. Note you can't guarantee that
84 * brighterThan reverses this, as darkerThan may result in black.
89 public static Color darkerThan(Color col)
91 return col == null ? null : col.darker().darker().darker();
95 * Returns a colour three shades brighter. Note you can't guarantee that
96 * darkerThan reverses this, as brighterThan may result in white.
101 public static Color brighterThan(Color col)
103 return col == null ? null : col.brighter().brighter().brighter();
107 * Returns a color between minColour and maxColour; the RGB values are in
108 * proportion to where 'value' lies between minValue and maxValue
117 public static Color getGraduatedColour(float value, float minValue,
118 Color minColour, float maxValue, Color maxColour)
120 if (minValue == maxValue)
124 if (value < minValue)
128 if (value > maxValue)
134 * prop = proportion of the way value is from minValue to maxValue
136 float prop = (value - minValue) / (maxValue - minValue);
137 float r = minColour.getRed() + prop
138 * (maxColour.getRed() - minColour.getRed());
139 float g = minColour.getGreen() + prop
140 * (maxColour.getGreen() - minColour.getGreen());
141 float b = minColour.getBlue() + prop
142 * (maxColour.getBlue() - minColour.getBlue());
143 return new Color(r / 255, g / 255, b / 255);
147 * 'Fades' the given colour towards white by the specified proportion. A
148 * factor of 1 or more results in White, a factor of 0 leaves the colour
149 * unchanged, and a factor between 0 and 1 results in a proportionate change
150 * of RGB values towards (255, 255, 255).
152 * A negative bleachFactor can be specified to darken the colour towards Black
156 * @param bleachFactor
159 public static Color bleachColour(Color colour, float bleachFactor)
161 if (bleachFactor >= 1f)
165 if (bleachFactor <= -1f)
169 if (bleachFactor == 0f)
174 int red = colour.getRed();
175 int green = colour.getGreen();
176 int blue = colour.getBlue();
178 if (bleachFactor > 0)
180 red += (255 - red) * bleachFactor;
181 green += (255 - green) * bleachFactor;
182 blue += (255 - blue) * bleachFactor;
183 return new Color(red, green, blue);
187 float factor = 1 + bleachFactor;
191 return new Color(red, green, blue);
196 * Parses a string into a Color, where the accepted formats are
198 * <li>an AWT colour name e.g. white</li>
199 * <li>a hex colour value (without prefix) e.g. ff0000</li>
200 * <li>an rgb triple e.g. 100,50,150</li>
204 * @return the parsed colour, or null if parsing fails
206 public static Color parseColourString(String colour)
212 colour = colour.trim();
217 int value = Integer.parseInt(colour, 16);
218 col = new Color(value);
219 } catch (NumberFormatException ex)
225 col = ColorUtils.getAWTColorFromName(colour);
232 String[] tokens = colour.split(",");
233 if (tokens.length == 3)
235 int r = Integer.parseInt(tokens[0].trim());
236 int g = Integer.parseInt(tokens[1].trim());
237 int b = Integer.parseInt(tokens[2].trim());
238 col = new Color(r, g, b);
240 } catch (Exception ex)
242 // non-numeric token or out of 0-255 range
250 * Constructs a colour from a text string. The hashcode of the whole string is
251 * scaled to the range 0-135. This is added to RGB values made from the
252 * hashcode of each third of the string, and scaled to the range 20-229.
257 public static Color createColourFromName(String name)
263 int lsize = name.length();
267 int rgbOffset = Math.abs(name.hashCode() % 10) * 15; // 0-135
272 int r = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
281 * green: second third
283 int g = Math.abs(name.substring(start, end).hashCode() + rgbOffset) % 210 + 20;
288 int b = Math.abs(name.substring(end).hashCode() + rgbOffset) % 210 + 20;
290 Color color = new Color(r, g, b);
296 * Returns the Color constant for a given colour name e.g. "pink", or null if
297 * the name is not recognised
302 public static Color getAWTColorFromName(String name)
309 name = name.toLowerCase();
311 // or make a static map; or use reflection on the field name
324 col = Color.darkGray;
333 col = Color.lightGray;