comparison dwt/graphics/RGB.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children ab8b5765e3d1
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 module dwt.graphics.RGB;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.internal.SerializableCompatibility;
17
18 /**
19 * Instances of this class are descriptions of colors in
20 * terms of the primary additive color model (red, green and
21 * blue). A color may be described in terms of the relative
22 * intensities of these three primary colors. The brightness
23 * of each color is specified by a value in the range 0 to 255,
24 * where 0 indicates no color (blackness) and 255 indicates
25 * maximum intensity.
26 * <p>
27 * The hashCode() method in this class uses the values of the public
28 * fields to compute the hash value. When storing instances of the
29 * class in hashed collections, do not modify these fields after the
30 * object has been inserted.
31 * </p>
32 * <p>
33 * Application code does <em>not</em> need to explicitly release the
34 * resources managed by each instance when those instances are no longer
35 * required, and thus no <code>dispose()</code> method is provided.
36 * </p>
37 *
38 * @see Color
39 */
40
41 public final class RGB implements SerializableCompatibility {
42
43 /**
44 * the red component of the RGB
45 */
46 public int red;
47
48 /**
49 * the green component of the RGB
50 */
51 public int green;
52
53 /**
54 * the blue component of the RGB
55 */
56 public int blue;
57
58 static final long serialVersionUID = 3258415023461249074L;
59
60 /**
61 * Constructs an instance of this class with the given
62 * red, green and blue values.
63 *
64 * @param red the red component of the new instance
65 * @param green the green component of the new instance
66 * @param blue the blue component of the new instance
67 *
68 * @exception IllegalArgumentException <ul>
69 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
70 * </ul>
71 */
72 public RGB(int red, int green, int blue) {
73 if ((red > 255) || (red < 0) ||
74 (green > 255) || (green < 0) ||
75 (blue > 255) || (blue < 0))
76 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
77 this.red = red;
78 this.green = green;
79 this.blue = blue;
80 }
81
82 /**
83 * Constructs an instance of this class with the given
84 * hue, saturation, and brightness.
85 *
86 * @param hue the hue value for the HSB color (from 0 to 360)
87 * @param saturation the saturation value for the HSB color (from 0 to 1)
88 * @param brightness the brightness value for the HSB color (from 0 to 1)
89 *
90 * @exception IllegalArgumentException <ul>
91 * <li>ERROR_INVALID_ARGUMENT - if the hue is not between 0 and 360 or
92 * the saturation or brightness is not between 0 and 1</li>
93 * </ul>
94 *
95 * @since 3.2
96 */
97 public RGB(float hue, float saturation, float brightness) {
98 if (hue < 0 || hue > 360 || saturation < 0 || saturation > 1 ||
99 brightness < 0 || brightness > 1) {
100 DWT.error(DWT.ERROR_INVALID_ARGUMENT);
101 }
102 float r, g, b;
103 if (saturation is 0) {
104 r = g = b = brightness;
105 } else {
106 if (hue is 360) hue = 0;
107 hue /= 60;
108 int i = (int)hue;
109 float f = hue - i;
110 float p = brightness * (1 - saturation);
111 float q = brightness * (1 - saturation * f);
112 float t = brightness * (1 - saturation * (1 - f));
113 switch(i) {
114 case 0:
115 r = brightness;
116 g = t;
117 b = p;
118 break;
119 case 1:
120 r = q;
121 g = brightness;
122 b = p;
123 break;
124 case 2:
125 r = p;
126 g = brightness;
127 b = t;
128 break;
129 case 3:
130 r = p;
131 g = q;
132 b = brightness;
133 break;
134 case 4:
135 r = t;
136 g = p;
137 b = brightness;
138 break;
139 case 5:
140 default:
141 r = brightness;
142 g = p;
143 b = q;
144 break;
145 }
146 }
147 red = (int)(r * 255 + 0.5);
148 green = (int)(g * 255 + 0.5);
149 blue = (int)(b * 255 + 0.5);
150 }
151
152 /**
153 * Returns the hue, saturation, and brightness of the color.
154 *
155 * @return color space values in float format (hue, saturation, brightness)
156 *
157 * @since 3.2
158 */
159 public float[] getHSB() {
160 float r = red / 255f;
161 float g = green / 255f;
162 float b = blue / 255f;
163 float max = Math.max(Math.max(r, g), b);
164 float min = Math.min(Math.min(r, g), b);
165 float delta = max - min;
166 float hue = 0;
167 float brightness = max;
168 float saturation = max is 0 ? 0 : (max - min) / max;
169 if (delta !is 0) {
170 if (r is max) {
171 hue = (g - b) / delta;
172 } else {
173 if (g is max) {
174 hue = 2 + (b - r) / delta;
175 } else {
176 hue = 4 + (r - g) / delta;
177 }
178 }
179 hue *= 60;
180 if (hue < 0) hue += 360;
181 }
182 return new float[] {hue, saturation, brightness};
183 }
184
185 /**
186 * Compares the argument to the receiver, and returns true
187 * if they represent the <em>same</em> object using a class
188 * specific comparison.
189 *
190 * @param object the object to compare with this object
191 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
192 *
193 * @see #hashCode()
194 */
195 public bool equals(Object object) {
196 if (object is this) return true;
197 if (!(object instanceof RGB)) return false;
198 RGB rgb = (RGB)object;
199 return (rgb.red is this.red) && (rgb.green is this.green) && (rgb.blue is this.blue);
200 }
201
202 /**
203 * Returns an integer hash code for the receiver. Any two
204 * objects that return <code>true</code> when passed to
205 * <code>equals</code> must return the same value for this
206 * method.
207 *
208 * @return the receiver's hash
209 *
210 * @see #equals(Object)
211 */
212 public int hashCode() {
213 return (blue << 16) | (green << 8) | red;
214 }
215
216 /**
217 * Returns a string containing a concise, human-readable
218 * description of the receiver.
219 *
220 * @return a string representation of the <code>RGB</code>
221 */
222 public String toString() {
223 return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
224 }
225
226 }