1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.ui.color;
\r
14 import org.eclipse.swt.graphics.Device;
\r
15 import org.eclipse.swt.graphics.RGB;
\r
18 * Color class that can use both RGB and HSV values.
\r
20 * @author Marko Luukkainen
\r
23 public class Color implements Comparable<Color>{
\r
44 public Color(int r, int g, int b) {
\r
51 public Color(float h, float s, float v) {
\r
58 public Color(double h, double s, double v) {
\r
65 public Color(RGB rgb) {
\r
72 public Color(Color color) {
\r
82 * Sets color form AWT color. Alpha component is omitted.
\r
85 public Color(java.awt.Color color) {
\r
86 this.r = color.getRed();
\r
87 this.g = color.getGreen();
\r
88 this.b = color.getBlue();
\r
92 public Color(org.eclipse.swt.graphics.Color color) {
\r
93 this.r = color.getRed();
\r
94 this.g = color.getGreen();
\r
95 this.b = color.getBlue();
\r
100 * @return Returns the r component of RGB color.
\r
102 public int getR() {
\r
107 * @return Returns the b component of RGB color.
\r
109 public int getB() {
\r
114 * @return Returns the g component of RGB color.
\r
116 public int getG() {
\r
121 * @return Returns the h component of HSV color .
\r
123 public float getH() {
\r
128 * @return Returns the s component of HSV color.
\r
130 public float getS() {
\r
135 * @return Returns the v component of HSV color.
\r
137 public float getV() {
\r
142 public Color getWhite() {
\r
143 return new Color(255, 255, 255);
\r
146 public Color getBlack() {
\r
147 return new Color(0, 0, 0);
\r
150 public Color getRed() {
\r
151 return new Color(255, 0, 0);
\r
154 public Color getGreen() {
\r
155 return new Color(0, 255, 0);
\r
158 public Color getBlue() {
\r
159 return new Color(0, 0, 255);
\r
163 * Returns random color
\r
164 * @return random color
\r
166 public static Color getRandom() {
\r
167 return new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
\r
171 * Returns random color
\r
172 * @param minSaturation minimum satuation of random color
\r
173 * @param minValue minimum value of random color
\r
174 * @return random color with specified mimimum allowed saturation and value
\r
176 public static Color getRandomHSV(float minSaturation, float minValue) {
\r
177 float randS = (float)Math.random() * (1.f - minSaturation) + minSaturation;
\r
178 float randV = (float)Math.random() * (1.f - minValue) + minValue;
\r
179 return new Color((float)Math.random() * 360.f, randS, randV);
\r
183 * Returns AWT Color
\r
186 public java.awt.Color getAWTColor() {
\r
187 return new java.awt.Color(r,g,b);
\r
191 * Returns SWT Color
\r
195 public org.eclipse.swt.graphics.Color getSWTColor(Device device) {
\r
196 return new org.eclipse.swt.graphics.Color(device,r,g,b);
\r
199 public RGB getRgb() {
\r
200 return new RGB(r,g,b);
\r
204 * Updates HSV values from RGB values
\r
207 private void updateHSV() {
\r
208 float tr = (float) r / 255.f;
\r
209 float tg = (float) g / 255.f;
\r
210 float tb = (float) b / 255.f;
\r
212 v = Math.max(tr, tg);
\r
213 v = Math.max(v, tb);
\r
214 float min = Math.min(tr, tg);
\r
215 min = Math.min(min, tb);
\r
217 float delta = v - min;
\r
226 h = Float.NaN; // saturation is 0 -> achromatic color
\r
229 h = 60.f * (tg - tb);
\r
230 } else if (tg == v) {
\r
231 h = 120.f + 60.f * (tb - tr);
\r
233 h = 240.f + 60.f * (tr - tg);
\r
241 private int floatToInt(float v) {
\r
242 return (int) (v * 255.f);
\r
246 * Updates RGB values from HSV values
\r
249 private void updateRGB() {
\r
251 //if (Float.isNaN(h)) {
\r
257 // throw new RuntimeException("Saturation is 0 -> Hue must be undefined");
\r
264 int hi = (int) Math.floor(h / 60.f);
\r
265 float f = h / 60.f - hi;
\r
266 float p = v * (1.f - s);
\r
267 float q = v * (1.f - (f * s));
\r
268 float t = v * (1.f - ((1.f - f) * s));
\r
306 public int hashCode() {
\r
307 return r ^ (g << 8) ^ (b << 16);
\r
311 public boolean equals(Object obj) {
\r
312 if (!(obj instanceof Color))
\r
314 Color c = (Color) obj;
\r
315 return (c.r==r && c.g==g && c.b==b);
\r
318 // Uses Hue value for comparisons.
\r
320 public int compareTo(Color arg0) {
\r
321 int d = (int)((h - arg0.h)*10000.f);
\r
335 void setH(float h) {
\r