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 jalview.api.ColorI;
29 import java.awt.Color;
30 import java.util.Random;
32 public class ColorUtils
35 public static Color getColor(ColorI c)
37 return new Color(c.getRed(), c.getGreen(), c.getBlue());
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
46 * @return Random color in RGB
48 public static final Color generateRandomColor(Color mix)
50 Random random = new Random();
51 int red = random.nextInt(256);
52 int green = random.nextInt(256);
53 int blue = random.nextInt(256);
58 red = (red + mix.getRed()) / 2;
59 green = (green + mix.getGreen()) / 2;
60 blue = (blue + mix.getBlue()) / 2;
63 Color color = new Color(red, green, blue);
69 * Convert to Tk colour code format
74 * ://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/colortool.html#
77 public static final String toTkCode(ColorI colour)
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());
89 * Returns a colour three shades darker. Note you can't guarantee that
90 * brighterThan reverses this, as darkerThan may result in black.
95 public static Color darkerThan(Color col)
97 return col == null ? null : col.darker().darker().darker();
101 * Returns a colour three shades brighter. Note you can't guarantee that
102 * darkerThan reverses this, as brighterThan may result in white.
107 public static Color brighterThan(Color col)
109 return col == null ? null : col.brighter().brighter().brighter();
113 * Returns a color between minColour and maxColour; the RGB values are in
114 * proportion to where 'value' lies between minValue and maxValue
123 public static Color getGraduatedColour(float value, float minValue,
124 Color minColour, float maxValue, Color maxColour)
126 if (minValue == maxValue)
130 if (value < minValue)
134 if (value > maxValue)
140 * prop = proportion of the way value is from minValue to maxValue
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);