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