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