KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > graphics > Color


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
10  *******************************************************************************/

11 package org.eclipse.swt.graphics;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16
17 /**
18  * Instances of this class manage the operating system resources that
19  * implement SWT's RGB color model. To create a color you can either
20  * specify the individual color components as integers in the range
21  * 0 to 255 or provide an instance of an <code>RGB</code>.
22  * <p>
23  * Application code must explicitly invoke the <code>Color.dispose()</code>
24  * method to release the operating system resources managed by each instance
25  * when those instances are no longer required.
26  * </p>
27  *
28  * @see RGB
29  * @see Device#getSystemColor
30  */

31
32 public final class Color extends Resource {
33     
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 SWT
39      * public API. It is marked public only so that it can be shared
40      * within the packages provided by SWT. It is not available on all
41      * platforms and should never be accessed from application code.
42      * </p>
43      */

44     public int handle;
45
46 /**
47  * Prevents uninitialized instances from being created outside the package.
48  */

49 Color() {
50 }
51
52 /**
53  * Constructs a new instance of this class given a device and the
54  * desired red, green and blue values expressed as ints in the range
55  * 0 to 255 (where 0 is black and 255 is full brightness). On limited
56  * color devices, the color instance created by this call may not have
57  * the same RGB values as the ones specified by the arguments. The
58  * RGB values on the returned instance will be the color values of
59  * the operating system color.
60  * <p>
61  * You must dispose the color when it is no longer required.
62  * </p>
63  *
64  * @param device the device on which to allocate the color
65  * @param red the amount of red in the color
66  * @param green the amount of green in the color
67  * @param blue the amount of blue in the color
68  *
69  * @exception IllegalArgumentException <ul>
70  * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
71  * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li>
72  * </ul>
73  *
74  * @see #dispose
75  */

76 public Color (Device device, int red, int green, int blue) {
77     if (device == null) device = Device.getDevice();
78     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
79     init(device, red, green, blue);
80     if (device.tracking) device.new_Object(this);
81 }
82
83 /**
84  * Constructs a new instance of this class given a device and an
85  * <code>RGB</code> describing the desired red, green and blue values.
86  * On limited color devices, the color instance created by this call
87  * may not have the same RGB values as the ones specified by the
88  * argument. The RGB values on the returned instance will be the color
89  * values of the operating system color.
90  * <p>
91  * You must dispose the color when it is no longer required.
92  * </p>
93  *
94  * @param device the device on which to allocate the color
95  * @param rgb the RGB values of the desired color
96  *
97  * @exception IllegalArgumentException <ul>
98  * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
99  * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li>
100  * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li>
101  * </ul>
102  *
103  * @see #dispose
104  */

105 public Color (Device device, RGB rgb) {
106     if (device == null) device = Device.getDevice();
107     if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
108     if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
109     init(device, rgb.red, rgb.green, rgb.blue);
110     if (device.tracking) device.new_Object(this);
111 }
112
113 /**
114  * Disposes of the operating system resources associated with
115  * the color. Applications must dispose of all colors which
116  * they allocate.
117  */

118 public void dispose() {
119     if (handle == -1) return;
120     if (device.isDisposed()) return;
121
122     /*
123      * If this is a palette-based device,
124      * Decrease the reference count for this color.
125      * If the reference count reaches 0, the slot may
126      * be reused when another color is allocated.
127      */

128     int hPal = device.hPalette;
129     if (hPal != 0) {
130         int index = OS.GetNearestPaletteIndex(hPal, handle);
131         int[] colorRefCount = device.colorRefCount;
132         if (colorRefCount[index] > 0) {
133             colorRefCount[index]--;
134         }
135     }
136     handle = -1;
137     if (device.tracking) device.dispose_Object(this);
138     device = null;
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 boolean equals (Object JavaDoc object) {
152     if (object == this) return true;
153     if (!(object instanceof Color)) return false;
154     Color color = (Color) object;
155     return device == color.device && (handle & 0xFFFFFF) == (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 SWTException <ul>
164  * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
165  * </ul>
166  */

167 public int getBlue () {
168     if (isDisposed()) SWT.error(SWT.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 SWTException <ul>
178  * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
179  * </ul>
180  */

181 public int getGreen () {
182     if (isDisposed()) SWT.error(SWT.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 SWTException <ul>
192  * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
193  * </ul>
194  */

195 public int getRed () {
196     if (isDisposed()) SWT.error(SWT.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 SWTException <ul>
206  * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
207  * </ul>
208  */

209 public RGB getRGB () {
210     if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
211     return new RGB(handle & 0xFF, (handle & 0xFF00) >> 8, (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  */

243 void init(Device device, int red, int green, int blue) {
244     if (red > 255 || red < 0 || green > 255 || green < 0 || blue > 255 || blue < 0) {
245         SWT.error(SWT.ERROR_INVALID_ARGUMENT);
246     }
247     this.device = device;
248     handle = (red & 0xFF) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 16);
249     
250     /* If this is not a palette-based device, return */
251     int hPal = device.hPalette;
252     if (hPal == 0) return;
253     
254     int[] colorRefCount = device.colorRefCount;
255     /* Add this color to the default palette now */
256     /* First find out if the color already exists */
257     int index = OS.GetNearestPaletteIndex(hPal, handle);
258     /* See if the nearest color actually is the color */
259     byte[] entry = new byte[4];
260     OS.GetPaletteEntries(hPal, index, 1, entry);
261     if ((entry[0] == (byte)red) && (entry[1] == (byte)green) &&
262         (entry[2] == (byte)blue)) {
263             /* Found the color. Increment the ref count and return */
264             colorRefCount[index]++;
265             return;
266     }
267     /* Didn't find the color, allocate it now. Find the first free entry */
268     int i = 0;
269     while (i < colorRefCount.length) {
270         if (colorRefCount[i] == 0) {
271             index = i;
272             break;
273         }
274         i++;
275     }
276     if (i == colorRefCount.length) {
277         /* No free entries, use the closest one */
278         /* Remake the handle from the actual rgbs */
279         handle = (entry[0] & 0xFF) | ((entry[1] & 0xFF) << 8) |
280                  ((entry[2] & 0xFF) << 16);
281     } else {
282         /* Found a free entry */
283         entry = new byte[] { (byte)(red & 0xFF), (byte)(green & 0xFF), (byte)(blue & 0xFF), 0 };
284         OS.SetPaletteEntries(hPal, index, 1, entry);
285     }
286     colorRefCount[index]++;
287 }
288
289 /**
290  * Returns <code>true</code> if the color has been disposed,
291  * and <code>false</code> otherwise.
292  * <p>
293  * This method gets the dispose state for the color.
294  * When a color has been disposed, it is an error to
295  * invoke any other method using the color.
296  *
297  * @return <code>true</code> when the color is disposed and <code>false</code> otherwise
298  */

299 public boolean isDisposed() {
300     return handle == -1;
301 }
302
303 /**
304  * Returns a string containing a concise, human-readable
305  * description of the receiver.
306  *
307  * @return a string representation of the receiver
308  */

309 public String JavaDoc toString () {
310     if (isDisposed()) return "Color {*DISPOSED*}"; //$NON-NLS-1$
311
return "Color {" + getRed() + ", " + getGreen() + ", " + getBlue() + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
312
}
313
314 /**
315  * Invokes platform specific functionality to allocate a new color.
316  * <p>
317  * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
318  * API for <code>Color</code>. It is marked public only so that it
319  * can be shared within the packages provided by SWT. It is not
320  * available on all platforms, and should never be called from
321  * application code.
322  * </p>
323  *
324  * @param device the device on which to allocate the color
325  * @param handle the handle for the color
326  * @return a new color object containing the specified device and handle
327  */

328 public static Color win32_new(Device device, int handle) {
329     if (device == null) device = Device.getDevice();
330     Color color = new Color();
331     color.handle = handle;
332     color.device = device;
333     return color;
334 }
335
336 }
337
Popular Tags