Mercurial > projects > dwt-mac
comparison dwt/graphics/Color.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 | 649b8e223d5a |
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.Color; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 | |
19 /** | |
20 * Instances of this class manage the operating system resources that | |
21 * implement DWT's RGB color model. To create a color you can either | |
22 * specify the individual color components as integers in the range | |
23 * 0 to 255 or provide an instance of an <code>RGB</code>. | |
24 * <p> | |
25 * Application code must explicitly invoke the <code>Color.dispose()</code> | |
26 * method to release the operating system resources managed by each instance | |
27 * when those instances are no longer required. | |
28 * </p> | |
29 * | |
30 * @see RGB | |
31 * @see Device#getSystemColor | |
32 */ | |
33 public final class Color extends Resource { | |
34 /** | |
35 * the handle to the OS color resource | |
36 * (Warning: This field is platform dependent) | |
37 * <p> | |
38 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT | |
39 * public API. It is marked public only so that it can be shared | |
40 * within the packages provided by DWT. It is not available on all | |
41 * platforms and should never be accessed from application code. | |
42 * </p> | |
43 */ | |
44 public float[] handle; | |
45 | |
46 Color(Device device) { | |
47 super(device); | |
48 } | |
49 | |
50 /** | |
51 * Constructs a new instance of this class given a device and the | |
52 * desired red, green and blue values expressed as ints in the range | |
53 * 0 to 255 (where 0 is black and 255 is full brightness). On limited | |
54 * color devices, the color instance created by this call may not have | |
55 * the same RGB values as the ones specified by the arguments. The | |
56 * RGB values on the returned instance will be the color values of | |
57 * the operating system color. | |
58 * <p> | |
59 * You must dispose the color when it is no longer required. | |
60 * </p> | |
61 * | |
62 * @param device the device on which to allocate the color | |
63 * @param red the amount of red in the color | |
64 * @param green the amount of green in the color | |
65 * @param blue the amount of blue in the color | |
66 * | |
67 * @exception IllegalArgumentException <ul> | |
68 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
69 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> | |
70 * </ul> | |
71 * | |
72 * @see #dispose | |
73 */ | |
74 public Color(Device device, int red, int green, int blue) { | |
75 super(device); | |
76 init(red, green, blue); | |
77 init(); | |
78 } | |
79 | |
80 /** | |
81 * Constructs a new instance of this class given a device and an | |
82 * <code>RGB</code> describing the desired red, green and blue values. | |
83 * On limited color devices, the color instance created by this call | |
84 * may not have the same RGB values as the ones specified by the | |
85 * argument. The RGB values on the returned instance will be the color | |
86 * values of the operating system color. | |
87 * <p> | |
88 * You must dispose the color when it is no longer required. | |
89 * </p> | |
90 * | |
91 * @param device the device on which to allocate the color | |
92 * @param rgb the RGB values of the desired color | |
93 * | |
94 * @exception IllegalArgumentException <ul> | |
95 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
96 * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> | |
97 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> | |
98 * </ul> | |
99 * | |
100 * @see #dispose | |
101 */ | |
102 public Color(Device device, RGB rgb) { | |
103 super(device); | |
104 if (rgb is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
105 init(rgb.red, rgb.green, rgb.blue); | |
106 init(); | |
107 } | |
108 | |
109 void destroy() { | |
110 handle = null; | |
111 } | |
112 | |
113 /** | |
114 * Compares the argument to the receiver, and returns true | |
115 * if they represent the <em>same</em> object using a class | |
116 * specific comparison. | |
117 * | |
118 * @param object the object to compare with this object | |
119 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
120 * | |
121 * @see #hashCode | |
122 */ | |
123 public bool equals(Object object) { | |
124 if (object is this) return true; | |
125 if (!(object instanceof Color)) return false; | |
126 Color color = (Color)object; | |
127 float[] rgbColor = color.handle; | |
128 if (handle is rgbColor) return true; | |
129 return device is color.device && | |
130 (int)(handle[0] * 255) is (int)(rgbColor[0] * 255) && | |
131 (int)(handle[1] * 255) is (int)(rgbColor[1] * 255) && | |
132 (int)(handle[2] * 255) is (int)(rgbColor[2] * 255); | |
133 } | |
134 | |
135 /** | |
136 * Returns the amount of blue in the color, from 0 to 255. | |
137 * | |
138 * @return the blue component of the color | |
139 * | |
140 * @exception DWTException <ul> | |
141 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
142 * </ul> | |
143 */ | |
144 public int getBlue() { | |
145 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
146 return (int)(handle[2] * 255); | |
147 } | |
148 | |
149 /** | |
150 * Returns the amount of green in the color, from 0 to 255. | |
151 * | |
152 * @return the green component of the color | |
153 * | |
154 * @exception DWTException <ul> | |
155 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
156 * </ul> | |
157 */ | |
158 public int getGreen() { | |
159 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
160 return (int)(handle[1] * 255); | |
161 } | |
162 | |
163 /** | |
164 * Returns the amount of red in the color, from 0 to 255. | |
165 * | |
166 * @return the red component of the color | |
167 * | |
168 * @exception DWTException <ul> | |
169 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
170 * </ul> | |
171 */ | |
172 public int getRed() { | |
173 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
174 return (int)(handle[0] * 255); | |
175 } | |
176 | |
177 /** | |
178 * Returns an integer hash code for the receiver. Any two | |
179 * objects that return <code>true</code> when passed to | |
180 * <code>equals</code> must return the same value for this | |
181 * method. | |
182 * | |
183 * @return the receiver's hash | |
184 * | |
185 * @see #equals | |
186 */ | |
187 public int hashCode() { | |
188 if (isDisposed()) return 0; | |
189 return (int)(handle[0] * 255) ^ (int)(handle[1] * 255) ^ (int)(handle[2] * 255); | |
190 } | |
191 | |
192 /** | |
193 * Returns an <code>RGB</code> representing the receiver. | |
194 * | |
195 * @return the RGB for the color | |
196 * | |
197 * @exception DWTException <ul> | |
198 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
199 * </ul> | |
200 */ | |
201 public RGB getRGB () { | |
202 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
203 return new RGB(getRed(), getGreen(), getBlue()); | |
204 } | |
205 | |
206 /** | |
207 * Invokes platform specific functionality to allocate a new color. | |
208 * <p> | |
209 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public | |
210 * API for <code>Color</code>. It is marked public only so that it | |
211 * can be shared within the packages provided by DWT. It is not | |
212 * available on all platforms, and should never be called from | |
213 * application code. | |
214 * </p> | |
215 * | |
216 * @param device the device on which to allocate the color | |
217 * @param handle the handle for the color | |
218 * | |
219 * @private | |
220 */ | |
221 public static Color cocoa_new(Device device, float[] rgbColor) { | |
222 Color color = new Color(device); | |
223 color.handle = rgbColor; | |
224 return color; | |
225 } | |
226 | |
227 void init(int red, int green, int blue) { | |
228 if ((red > 255) || (red < 0) || | |
229 (green > 255) || (green < 0) || | |
230 (blue > 255) || (blue < 0)) { | |
231 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
232 } | |
233 float[] rgbColor = new float[4]; | |
234 rgbColor[0] = red / 255f; | |
235 rgbColor[1] = green / 255f; | |
236 rgbColor[2] = blue / 255f; | |
237 rgbColor[3] = 1; | |
238 handle = rgbColor; | |
239 } | |
240 | |
241 /** | |
242 * Returns <code>true</code> if the color has been disposed, | |
243 * and <code>false</code> otherwise. | |
244 * <p> | |
245 * This method gets the dispose state for the color. | |
246 * When a color has been disposed, it is an error to | |
247 * invoke any other method using the color. | |
248 * | |
249 * @return <code>true</code> when the color is disposed and <code>false</code> otherwise | |
250 */ | |
251 public bool isDisposed() { | |
252 return handle is null; | |
253 } | |
254 | |
255 /** | |
256 * Returns a string containing a concise, human-readable | |
257 * description of the receiver. | |
258 * | |
259 * @return a string representation of the receiver | |
260 */ | |
261 public String toString () { | |
262 if (isDisposed()) return "Color {*DISPOSED*}"; | |
263 return "Color {" + getRed() + ", " + getGreen() + ", " + getBlue() + "}"; | |
264 } | |
265 | |
266 } |