]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/graphics/RGB.java
ebcb2d7c028028948fdea15af83e10e0537adfe5
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / graphics / RGB.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.graphics;
15
16 import java.io.*;
17
18 import org.eclipse.swt.*;
19
20 /**
21  * Instances of this class are descriptions of colors in
22  * terms of the primary additive color model (red, green and
23  * blue). A color may be described in terms of the relative
24  * intensities of these three primary colors. The brightness
25  * of each color is specified by a value in the range 0 to 255,
26  * where 0 indicates no color (blackness) and 255 indicates
27  * maximum intensity.
28  * <p>
29  * The hashCode() method in this class uses the values of the public
30  * fields to compute the hash value. When storing instances of the
31  * class in hashed collections, do not modify these fields after the
32  * object has been inserted.
33  * </p>
34  * <p>
35  * Application code does <em>not</em> need to explicitly release the
36  * resources managed by each instance when those instances are no longer
37  * required, and thus no <code>dispose()</code> method is provided.
38  * </p>
39  *
40  * @see Color
41  * @see <a href="http://www.eclipse.org/swt/snippets/#color">Color and RGB snippets</a>
42  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
43  */
44
45 public final class RGB implements Serializable {
46
47         /**
48          * the red component of the RGB
49          */
50         public int red;
51
52         /**
53          * the green component of the RGB
54          */
55         public int green;
56
57         /**
58          * the blue component of the RGB
59          */
60         public int blue;
61
62         static final long serialVersionUID = 3258415023461249074L;
63
64 /**
65  * Constructs an instance of this class with the given
66  * red, green and blue values.
67  *
68  * @param red the red component of the new instance
69  * @param green the green component of the new instance
70  * @param blue the blue component of the new instance
71  *
72  * @exception IllegalArgumentException <ul>
73  *    <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
74  * </ul>
75  */
76 public RGB(int red, int green, int blue) {
77         if ((red > 255) || (red < 0) ||
78                 (green > 255) || (green < 0) ||
79                 (blue > 255) || (blue < 0))
80                         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
81         this.red = red;
82         this.green = green;
83         this.blue = blue;
84 }
85
86 /**
87 * Constructs an instance of this class with the given
88 * hue, saturation, and brightness.
89 *
90 * @param hue the hue value for the HSB color (from 0 to 360)
91 * @param saturation the saturation value for the HSB color (from 0 to 1)
92 * @param brightness the brightness value for the HSB color (from 0 to 1)
93 *
94 * @exception IllegalArgumentException <ul>
95 *    <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
96 *    the saturation or brightness is not between 0 and 1</li>
97 * </ul>
98 *
99 * @since 3.2
100 */
101 public RGB(float hue, float saturation, float brightness) {
102         if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1 ||
103                 brightness < 0 || brightness > 1) {
104                 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
105         }
106         float r, g, b;
107         if (saturation == 0) {
108                 r = g = b = brightness;
109         } else {
110                 if (hue == 360) hue = 0;
111                 hue /= 60;
112                 int i = (int)hue;
113                 float f = hue - i;
114                 float p = brightness * (1 - saturation);
115                 float q = brightness * (1 - saturation * f);
116                 float t = brightness * (1 - saturation * (1 - f));
117                 switch(i) {
118                         case 0:
119                                 r = brightness;
120                                 g = t;
121                                 b = p;
122                                 break;
123                         case 1:
124                                 r = q;
125                                 g = brightness;
126                                 b = p;
127                                 break;
128                         case 2:
129                                 r = p;
130                                 g = brightness;
131                                 b = t;
132                                 break;
133                         case 3:
134                                 r = p;
135                                 g = q;
136                                 b = brightness;
137                                 break;
138                         case 4:
139                                 r = t;
140                                 g = p;
141                                 b = brightness;
142                                 break;
143                         case 5:
144                         default:
145                                 r = brightness;
146                                 g = p;
147                                 b = q;
148                                 break;
149                 }
150         }
151         red = (int)(r * 255 + 0.5);
152         green = (int)(g * 255 + 0.5);
153         blue = (int)(b * 255 + 0.5);
154 }
155
156 /**
157  * Returns the hue, saturation, and brightness of the color.
158  *
159  * @return color space values in float format:<ul>
160  *             <li>hue (from 0 to 360)</li>
161  *             <li>saturation (from 0 to 1)</li>
162  *             <li>brightness (from 0 to 1)</li>
163  *             </ul>
164  * @see #RGB(float, float, float)
165  *
166  * @since 3.2
167  */
168 public float[] getHSB() {
169         float r = red / 255f;
170         float g = green / 255f;
171         float b = blue / 255f;
172         float max = Math.max(Math.max(r, g), b);
173         float min = Math.min(Math.min(r, g), b);
174         float delta = max - min;
175         float hue = 0;
176         float brightness = max;
177         float saturation = max == 0 ? 0 : (max - min) / max;
178         if (delta != 0) {
179                 if (r == max) {
180                         hue = (g  - b) / delta;
181                 } else {
182                         if (g == max) {
183                                 hue = 2 + (b - r) / delta;
184                         } else {
185                                 hue = 4 + (r - g) / delta;
186                         }
187                 }
188                 hue *= 60;
189                 if (hue < 0) hue += 360;
190         }
191         return new float[] {hue, saturation, brightness};
192 }
193
194 /**
195  * Compares the argument to the receiver, and returns true
196  * if they represent the <em>same</em> object using a class
197  * specific comparison.
198  *
199  * @param object the object to compare with this object
200  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
201  *
202  * @see #hashCode()
203  */
204 @Override
205 public boolean equals(Object object) {
206         if (object == this) return true;
207         if (!(object instanceof RGB)) return false;
208         RGB rgb = (RGB)object;
209         return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue);
210 }
211
212 /**
213  * Returns an integer hash code for the receiver. Any two
214  * objects that return <code>true</code> when passed to
215  * <code>equals</code> must return the same value for this
216  * method.
217  *
218  * @return the receiver's hash
219  *
220  * @see #equals(Object)
221  */
222 @Override
223 public int hashCode() {
224         return (blue << 16) | (green << 8) | red;
225 }
226
227 /**
228  * Returns a string containing a concise, human-readable
229  * description of the receiver.
230  *
231  * @return a string representation of the <code>RGB</code>
232  */
233 @Override
234 public String toString() {
235         return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
236 }
237
238 }