KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > RGB


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.graphics;
12
13 import org.eclipse.swt.internal.SerializableCompatibility;
14 import org.eclipse.swt.*;
15
16 /**
17  * Instances of this class are descriptions of colors in
18  * terms of the primary additive color model (red, green and
19  * blue). A color may be described in terms of the relative
20  * intensities of these three primary colors. The brightness
21  * of each color is specified by a value in the range 0 to 255,
22  * where 0 indicates no color (blackness) and 255 indicates
23  * maximum intensity.
24  * <p>
25  * The hashCode() method in this class uses the values of the public
26  * fields to compute the hash value. When storing instances of the
27  * class in hashed collections, do not modify these fields after the
28  * object has been inserted.
29  * </p>
30  * <p>
31  * Application code does <em>not</em> need to explicitly release the
32  * resources managed by each instance when those instances are no longer
33  * required, and thus no <code>dispose()</code> method is provided.
34  * </p>
35  *
36  * @see Color
37  */

38
39 public final class RGB implements SerializableCompatibility {
40     
41     /**
42      * the red component of the RGB
43      */

44     public int red;
45     
46     /**
47      * the green component of the RGB
48      */

49     public int green;
50     
51     /**
52      * the blue component of the RGB
53      */

54     public int blue;
55     
56     static final long serialVersionUID = 3258415023461249074L;
57     
58 /**
59  * Constructs an instance of this class with the given
60  * red, green and blue values.
61  *
62  * @param red the red component of the new instance
63  * @param green the green component of the new instance
64  * @param blue the blue component of the new instance
65  *
66  * @exception IllegalArgumentException <ul>
67  * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
68  * </ul>
69  */

70 public RGB(int red, int green, int blue) {
71     if ((red > 255) || (red < 0) ||
72         (green > 255) || (green < 0) ||
73         (blue > 255) || (blue < 0))
74             SWT.error(SWT.ERROR_INVALID_ARGUMENT);
75     this.red = red;
76     this.green = green;
77     this.blue = blue;
78 }
79
80 /**
81 * Constructs an instance of this class with the given
82 * hue, saturation, and brightness.
83 *
84 * @param hue the hue value for the HSB color (from 0 to 360)
85 * @param saturation the saturation value for the HSB color (from 0 to 1)
86 * @param brightness the brightness value for the HSB color (from 0 to 1)
87 *
88 * @exception IllegalArgumentException <ul>
89 * <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
90 * the saturation or brightness is not between 0 and 1</li>
91 * </ul>
92 *
93 * @since 3.2
94 */

95 public RGB(float hue, float saturation, float brightness) {
96     if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1 ||
97         brightness < 0 || brightness > 1) {
98         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
99     }
100     float r, g, b;
101     if (saturation == 0) {
102         r = g = b = brightness;
103     } else {
104         if (hue == 360) hue = 0;
105         hue /= 60;
106         int i = (int)hue;
107         float f = hue - i;
108         float p = brightness * (1 - saturation);
109         float q = brightness * (1 - saturation * f);
110         float t = brightness * (1 - saturation * (1 - f));
111         switch(i) {
112             case 0:
113                 r = brightness;
114                 g = t;
115                 b = p;
116                 break;
117             case 1:
118                 r = q;
119                 g = brightness;
120                 b = p;
121                 break;
122             case 2:
123                 r = p;
124                 g = brightness;
125                 b = t;
126                 break;
127             case 3:
128                 r = p;
129                 g = q;
130                 b = brightness;
131                 break;
132             case 4:
133                 r = t;
134                 g = p;
135                 b = brightness;
136                 break;
137             case 5:
138             default:
139                 r = brightness;
140                 g = p;
141                 b = q;
142                 break;
143         }
144     }
145     red = (int)(r * 255 + 0.5);
146     green = (int)(g * 255 + 0.5);
147     blue = (int)(b * 255 + 0.5);
148 }
149
150 /**
151  * Returns the hue, saturation, and brightness of the color.
152  *
153  * @return color space values in float format (hue, saturation, brightness)
154  *
155  * @since 3.2
156  */

157 public float[] getHSB() {
158     float r = red / 255f;
159     float g = green / 255f;
160     float b = blue / 255f;
161     float max = Math.max(Math.max(r, g), b);
162     float min = Math.min(Math.min(r, g), b);
163     float delta = max - min;
164     float hue = 0;
165     float brightness = max;
166     float saturation = max == 0 ? 0 : (max - min) / max;
167     if (delta != 0) {
168         if (r == max) {
169             hue = (g - b) / delta;
170         } else {
171             if (g == max) {
172                 hue = 2 + (b - r) / delta;
173             } else {
174                 hue = 4 + (r - g) / delta;
175             }
176         }
177         hue *= 60;
178         if (hue < 0) hue += 360;
179     }
180     return new float[] {hue, saturation, brightness};
181 }
182
183 /**
184  * Compares the argument to the receiver, and returns true
185  * if they represent the <em>same</em> object using a class
186  * specific comparison.
187  *
188  * @param object the object to compare with this object
189  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
190  *
191  * @see #hashCode()
192  */

193 public boolean equals(Object JavaDoc object) {
194     if (object == this) return true;
195     if (!(object instanceof RGB)) return false;
196     RGB rgb = (RGB)object;
197     return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue);
198 }
199
200 /**
201  * Returns an integer hash code for the receiver. Any two
202  * objects that return <code>true</code> when passed to
203  * <code>equals</code> must return the same value for this
204  * method.
205  *
206  * @return the receiver's hash
207  *
208  * @see #equals(Object)
209  */

210 public int hashCode() {
211     return (blue << 16) | (green << 8) | red;
212 }
213
214 /**
215  * Returns a string containing a concise, human-readable
216  * description of the receiver.
217  *
218  * @return a string representation of the <code>RGB</code>
219  */

220 public String JavaDoc toString() {
221     return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
222
}
223
224 }
225
Popular Tags