Mercurial > projects > dwt-win
annotate dwt/graphics/Color.d @ 48:9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Feb 2008 01:14:54 +0100 |
parents | f5482da87ed8 |
children | 184ab53b7785 |
rev | line source |
---|---|
18 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 | |
23 | 10 * Port to the D programming language: |
11 * Frank Benoit <benoit@tionex.de> | |
18 | 12 *******************************************************************************/ |
13 module dwt.graphics.Color; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.internal.win32.OS; | |
19 | |
20 import dwt.graphics.Resource; | |
21 import dwt.graphics.RGB; | |
22 import dwt.graphics.Device; | |
23 | |
24 import tango.text.convert.Format; | |
25 | |
26 /** | |
27 * Instances of this class manage the operating system resources that | |
28 * implement DWT's RGB color model. To create a color you can either | |
29 * specify the individual color components as integers in the range | |
30 * 0 to 255 or provide an instance of an <code>RGB</code>. | |
31 * <p> | |
32 * Application code must explicitly invoke the <code>Color.dispose()</code> | |
33 * method to release the operating system resources managed by each instance | |
34 * when those instances are no longer required. | |
35 * </p> | |
36 * | |
37 * @see RGB | |
38 * @see Device#getSystemColor | |
39 */ | |
40 | |
41 public final class Color : Resource { | |
42 | |
43 /** | |
44 * the handle to the OS color resource | |
45 * (Warning: This field is platform dependent) | |
46 * <p> | |
47 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT | |
48 * public API. It is marked public only so that it can be shared | |
49 * within the packages provided by DWT. It is not available on all | |
50 * platforms and should never be accessed from application code. | |
51 * </p> | |
52 */ | |
53 public COLORREF handle; | |
54 | |
55 /** | |
56 * Prevents uninitialized instances from being created outside the package. | |
57 */ | |
58 this() { | |
59 } | |
60 | |
61 /** | |
62 * Constructs a new instance of this class given a device and the | |
63 * desired red, green and blue values expressed as ints in the range | |
64 * 0 to 255 (where 0 is black and 255 is full brightness). On limited | |
65 * color devices, the color instance created by this call may not have | |
66 * the same RGB values as the ones specified by the arguments. The | |
67 * RGB values on the returned instance will be the color values of | |
68 * the operating system color. | |
69 * <p> | |
70 * You must dispose the color when it is no longer required. | |
71 * </p> | |
72 * | |
73 * @param device the device on which to allocate the color | |
74 * @param red the amount of red in the color | |
75 * @param green the amount of green in the color | |
76 * @param blue the amount of blue in the color | |
77 * | |
78 * @exception IllegalArgumentException <ul> | |
79 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
80 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> | |
81 * </ul> | |
82 * | |
83 * @see #dispose | |
84 */ | |
85 public this (Device device, int red, int green, int blue) { | |
86 if (device is null) device = Device.getDevice(); | |
87 if (device is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
88 init(device, red, green, blue); | |
89 if (device.tracking) device.new_Object(this); | |
90 } | |
91 | |
92 /** | |
93 * Constructs a new instance of this class given a device and an | |
94 * <code>RGB</code> describing the desired red, green and blue values. | |
95 * On limited color devices, the color instance created by this call | |
96 * may not have the same RGB values as the ones specified by the | |
97 * argument. The RGB values on the returned instance will be the color | |
98 * values of the operating system color. | |
99 * <p> | |
100 * You must dispose the color when it is no longer required. | |
101 * </p> | |
102 * | |
103 * @param device the device on which to allocate the color | |
104 * @param rgb the RGB values of the desired color | |
105 * | |
106 * @exception IllegalArgumentException <ul> | |
107 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> | |
108 * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> | |
109 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> | |
110 * </ul> | |
111 * | |
112 * @see #dispose | |
113 */ | |
114 public this (Device device, RGB rgb) { | |
115 if (device is null) device = Device.getDevice(); | |
116 if (device is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
117 if (rgb is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); | |
118 init(device, rgb.red, rgb.green, rgb.blue); | |
119 if (device.tracking) device.new_Object(this); | |
120 } | |
121 | |
122 /** | |
123 * Disposes of the operating system resources associated with | |
124 * the color. Applications must dispose of all colors which | |
125 * they allocate. | |
126 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
127 override public void dispose() { |
18 | 128 if (handle is -1) return; |
129 if (device.isDisposed()) return; | |
130 | |
131 /* | |
132 * If this is a palette-based device, | |
133 * Decrease the reference count for this color. | |
134 * If the reference count reaches 0, the slot may | |
135 * be reused when another color is allocated. | |
136 */ | |
137 auto hPal = device.hPalette; | |
138 if (hPal !is null) { | |
139 int index = OS.GetNearestPaletteIndex(hPal, handle); | |
140 int[] colorRefCount = device.colorRefCount; | |
141 if (colorRefCount[index] > 0) { | |
142 colorRefCount[index]--; | |
143 } | |
144 } | |
145 handle = -1; | |
146 if (device.tracking) device.dispose_Object(this); | |
147 device = null; | |
148 } | |
149 | |
150 /** | |
151 * Compares the argument to the receiver, and returns true | |
152 * if they represent the <em>same</em> object using a class | |
153 * specific comparison. | |
154 * | |
155 * @param object the object to compare with this object | |
156 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise | |
157 * | |
158 * @see #hashCode | |
159 */ | |
160 public bool equals (Object object) { | |
161 if (object is this) return true; | |
162 if (!(cast(Color)object)) return false; | |
163 Color color = cast(Color) object; | |
164 return device is color.device && (handle & 0xFFFFFF) is (color.handle & 0xFFFFFF); | |
165 } | |
166 | |
167 /** | |
168 * Returns the amount of blue in the color, from 0 to 255. | |
169 * | |
170 * @return the blue component of the color | |
171 * | |
172 * @exception DWTException <ul> | |
173 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
174 * </ul> | |
175 */ | |
176 public int getBlue () { | |
177 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
178 return (handle & 0xFF0000) >> 16; | |
179 } | |
180 | |
181 /** | |
182 * Returns the amount of green in the color, from 0 to 255. | |
183 * | |
184 * @return the green component of the color | |
185 * | |
186 * @exception DWTException <ul> | |
187 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
188 * </ul> | |
189 */ | |
190 public int getGreen () { | |
191 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
192 return (handle & 0xFF00) >> 8 ; | |
193 } | |
194 | |
195 /** | |
196 * Returns the amount of red in the color, from 0 to 255. | |
197 * | |
198 * @return the red component of the color | |
199 * | |
200 * @exception DWTException <ul> | |
201 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
202 * </ul> | |
203 */ | |
204 public int getRed () { | |
205 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
206 return handle & 0xFF; | |
207 } | |
208 | |
209 /** | |
210 * Returns an <code>RGB</code> representing the receiver. | |
211 * | |
212 * @return the RGB for the color | |
213 * | |
214 * @exception DWTException <ul> | |
215 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> | |
216 * </ul> | |
217 */ | |
218 public RGB getRGB () { | |
219 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); | |
220 return new RGB(cast(int)handle & 0xFF,cast(int) (handle & 0xFF00) >> 8,cast(int) (handle & 0xFF0000) >> 16); | |
221 } | |
222 | |
223 /** | |
224 * Returns an integer hash code for the receiver. Any two | |
225 * objects that return <code>true</code> when passed to | |
226 * <code>equals</code> must return the same value for this | |
227 * method. | |
228 * | |
229 * @return the receiver's hash | |
230 * | |
231 * @see #equals | |
232 */ | |
233 public int hashCode () { | |
234 return handle; | |
235 } | |
236 | |
237 /** | |
238 * Allocates the operating system resources associated | |
239 * with the receiver. | |
240 * | |
241 * @param device the device on which to allocate the color | |
242 * @param red the amount of red in the color | |
243 * @param green the amount of green in the color | |
244 * @param blue the amount of blue in the color | |
245 * | |
246 * @exception IllegalArgumentException <ul> | |
247 * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> | |
248 * </ul> | |
249 * | |
250 * @see #dispose | |
251 */ | |
252 void init(Device device, int red, int green, int blue) { | |
253 if (red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0) { | |
254 DWT.error(DWT.ERROR_INVALID_ARGUMENT); | |
255 } | |
256 this.device = device; | |
257 handle = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16); | |
258 | |
259 /* If this is not a palette-based device, return */ | |
260 auto hPal = device.hPalette; | |
261 if (hPal is null) return; | |
262 | |
263 int[] colorRefCount = device.colorRefCount; | |
264 /* Add this color to the default palette now */ | |
265 /* First find out if the color already exists */ | |
266 int index = OS.GetNearestPaletteIndex(hPal, handle); | |
267 /* See if the nearest color actually is the color */ | |
268 PALETTEENTRY entry; | |
269 OS.GetPaletteEntries(hPal, index, 1, &entry); | |
270 if ((entry.peRed is cast(byte)red) && (entry.peGreen is cast(byte)green) && | |
271 (entry.peBlue is cast(byte)blue)) { | |
272 /* Found the color. Increment the ref count and return */ | |
273 colorRefCount[index]++; | |
274 return; | |
275 } | |
276 /* Didn't find the color, allocate it now. Find the first free entry */ | |
277 int i = 0; | |
278 while (i < colorRefCount.length) { | |
279 if (colorRefCount[i] is 0) { | |
280 index = i; | |
281 break; | |
282 } | |
283 i++; | |
284 } | |
285 if (i is colorRefCount.length) { | |
286 /* No free entries, use the closest one */ | |
287 /* Remake the handle from the actual rgbs */ | |
288 handle = (entry.peRed & 0xFF) | ((entry.peGreen & 0xFF) << 8) | | |
289 ((entry.peBlue & 0xFF) << 16); | |
290 } else { | |
291 /* Found a free entry */ | |
292 entry.peRed = cast(BYTE)(red & 0xFF); | |
293 entry.peGreen = cast(BYTE)(green & 0xFF); | |
294 entry.peBlue = cast(BYTE)(blue & 0xFF); | |
295 entry.peFlags = 0; | |
296 OS.SetPaletteEntries(hPal, index, 1, &entry); | |
297 } | |
298 colorRefCount[index]++; | |
299 } | |
300 | |
301 /** | |
302 * Returns <code>true</code> if the color has been disposed, | |
303 * and <code>false</code> otherwise. | |
304 * <p> | |
305 * This method gets the dispose state for the color. | |
306 * When a color has been disposed, it is an error to | |
307 * invoke any other method using the color. | |
308 * | |
309 * @return <code>true</code> when the color is disposed and <code>false</code> otherwise | |
310 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
311 override public bool isDisposed() { |
18 | 312 return handle is -1; |
313 } | |
314 | |
315 /** | |
316 * Returns a string containing a concise, human-readable | |
317 * description of the receiver. | |
318 * | |
319 * @return a string representation of the receiver | |
320 */ | |
48
9a64a7781bab
Added override and alias, first chunk. Thanks torhu for doing this patch.
Frank Benoit <benoit@tionex.de>
parents:
23
diff
changeset
|
321 override public char[] toString () { |
18 | 322 if (isDisposed()) return "Color {*DISPOSED*}"; //$NON-NLS-1$ |
323 return Format( "Color {{{}, {}, {}}", getRed(), getGreen(), getBlue()); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
324 } | |
325 | |
326 /** | |
327 * Invokes platform specific functionality to allocate a new color. | |
328 * <p> | |
329 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public | |
330 * API for <code>Color</code>. It is marked public only so that it | |
331 * can be shared within the packages provided by DWT. It is not | |
332 * available on all platforms, and should never be called from | |
333 * application code. | |
334 * </p> | |
335 * | |
336 * @param device the device on which to allocate the color | |
337 * @param handle the handle for the color | |
338 * @return a new color object containing the specified device and handle | |
339 */ | |
340 public static Color win32_new(Device device, int handle) { | |
341 if (device is null) device = Device.getDevice(); | |
342 Color color = new Color(); | |
343 color.handle = handle; | |
344 color.device = device; | |
345 return color; | |
346 } | |
347 | |
348 } |