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