root/dwt/graphics/RGB.d

Revision 246:fd9c62a2998e, 6.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

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