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