]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/color/Color.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / color / Color.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui.color;
13
14 import org.eclipse.swt.graphics.Device;
15 import org.eclipse.swt.graphics.RGB;
16
17 /**
18  * Color class that can use both RGB and HSV values.
19  * 
20  * @author Marko Luukkainen
21  *
22  */
23 public class Color implements Comparable<Color>{
24
25     private int r;
26
27     private int g;
28
29     private int b;
30
31     private float h;
32
33     private float s;
34
35     private float v;
36
37     public Color() {
38         r = 255;
39         g = 255;
40         b = 255;
41         updateHSV();
42     }
43
44     public Color(int r, int g, int b) {
45         this.r = r;
46         this.g = g;
47         this.b = b;
48         updateHSV();
49     }
50     
51     public Color(float h, float s, float v) {
52         this.h = h;
53         this.s = s;
54         this.v = v;
55         updateRGB();   
56     }
57     
58     public Color(double h, double s, double v) {
59         this.h = (float)h;
60         this.s = (float)s;
61         this.v = (float)v;
62         updateRGB();    
63     }
64     
65     public Color(RGB rgb) {
66         this.r = rgb.red;
67         this.g = rgb.green;
68         this.b = rgb.blue;
69         updateHSV();
70     }
71     
72     public Color(Color color) {
73         this.r = color.r;
74         this.g = color.g;
75         this.b = color.b;
76         this.h = color.h;
77         this.s = color.s;
78         this.v = color.v;
79     }
80     
81     /**
82      * Sets color form AWT color. Alpha component is omitted.
83      * @param color
84      */
85     public Color(java.awt.Color color) {
86         this.r = color.getRed();
87         this.g = color.getGreen();
88         this.b = color.getBlue();
89         updateHSV();
90     }
91     
92     public Color(org.eclipse.swt.graphics.Color color) {
93         this.r = color.getRed();
94         this.g = color.getGreen();
95         this.b = color.getBlue();
96         updateHSV();
97     }
98
99     /**
100      * @return Returns the r component of RGB color.
101      */
102     public int getR() {
103         return r;
104     }
105     
106     /**
107      * @return Returns the b component of RGB color.
108      */
109     public int getB() {
110         return b;
111     }
112
113     /**
114      * @return Returns the g component of RGB color.
115      */
116     public int getG() {
117         return g;
118     }
119
120     /**
121      * @return Returns the h component of HSV color .
122      */
123     public float getH() {
124         return h;
125     }
126
127     /**
128      * @return Returns the s component of HSV color.
129      */
130     public float getS() {
131         return s;
132     }
133
134     /**
135      * @return Returns the v component of HSV color.
136      */
137     public float getV() {
138         return v;
139     }
140
141
142     public Color getWhite() {
143         return new Color(255, 255, 255);
144     }
145
146     public Color getBlack() {
147         return new Color(0, 0, 0);
148     }
149
150     public Color getRed() {
151         return new Color(255, 0, 0);
152     }
153
154     public Color getGreen() {
155         return new Color(0, 255, 0);
156     }
157
158     public Color getBlue() {
159         return new Color(0, 0, 255);
160     }
161
162     /**
163      * Returns random color
164      * @return random color
165      */
166     public static Color getRandom() {
167         return new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
168     }
169     
170     /**
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
175      */
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);
180     }
181     
182     /**
183      * Returns AWT Color
184      * @return
185      */
186     public java.awt.Color getAWTColor() {
187         return new java.awt.Color(r,g,b);
188     }
189     
190     /**
191      * Returns SWT Color
192      * @param device
193      * @return
194      */
195     public org.eclipse.swt.graphics.Color getSWTColor(Device device) {
196         return new org.eclipse.swt.graphics.Color(device,r,g,b);
197     }
198     
199     public RGB getRgb() {
200         return new RGB(r,g,b);
201     }
202
203     /**
204      * Updates HSV values from RGB values
205      * 
206      */
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;
211
212         v = Math.max(tr, tg);
213         v = Math.max(v, tb);
214         float min = Math.min(tr, tg);
215         min = Math.min(min, tb);
216
217         float delta = v - min;
218
219         if (v < 0.01f) {
220             s = 0.f;
221         } else {
222             s = delta / v;
223         }
224
225         if (s == 0.f) {
226             //h = Float.NaN; // saturation is 0 -> achromatic color
227                 h = 0.f;
228         } else {
229             if (tr == v) {
230                 h = 60.f * (tg - tb);
231             } else if (tg == v) {
232                 h = 120.f + 60.f * (tb - tr);
233             } else {
234                 h = 240.f + 60.f * (tr - tg);
235             }
236             if (h < 0.f)
237                 h += 360.f;
238         }
239
240     }
241
242     private int floatToInt(float v) {
243         return (int) (v * 255.f);
244     }
245
246     /**
247      * Updates RGB values from HSV values
248      * 
249      */
250     private void updateRGB() {
251         if (s == 0.f) {
252             r = floatToInt(v);
253             g = floatToInt(v);
254             b = floatToInt(v);
255         } else {
256             while (h < 0.f)
257                 h+= 360.f;
258             while (h >= 360.f)
259                 h-=360.f;
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));
265             switch (hi) {
266             case 0:
267                 r = floatToInt(v);
268                 g = floatToInt(t);
269                 b = floatToInt(p);
270                 break;
271             case 1:
272                 r = floatToInt(q);
273                 g = floatToInt(v);
274                 b = floatToInt(p);
275                 break;
276             case 2:
277                 r = floatToInt(p);
278                 g = floatToInt(v);
279                 b = floatToInt(t);
280                 break;
281             case 3:
282                 r = floatToInt(p);
283                 g = floatToInt(q);
284                 b = floatToInt(v);
285                 break;
286             case 4:
287                 r = floatToInt(t);
288                 g = floatToInt(p);
289                 b = floatToInt(v);
290                 break;
291             case 5:
292                 r = floatToInt(v);
293                 g = floatToInt(p);
294                 b = floatToInt(q);
295                 break;
296             }
297         }
298
299     }
300     
301     @Override
302     public int hashCode() {
303         return r ^ (g << 8) ^ (b << 16);
304     }
305     
306     @Override
307     public boolean equals(Object obj) {
308         if (!(obj instanceof Color))
309             return false;
310         Color c = (Color) obj;
311         return (c.r==r && c.g==g && c.b==b);        
312     }
313     
314     // Uses Hue value for comparisons.
315     @Override
316     public int compareTo(Color arg0) {
317         int d = (int)((h - arg0.h)*10000.f);
318         if (d == 0) {
319                 d = r - arg0.r;
320                 if (d == 0) {
321                         d = g - arg0.g;
322                         if (d == 0) {
323                         d = b - arg0.b;
324                 }
325                 }
326         }
327         return d;
328         
329     }
330     
331     void setH(float h) {
332                 this.h = h;
333         }
334
335 }