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);