25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.graphics.Color;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import org.eclipse.swt.internal.gtk.OS;
|
|
20 import org.eclipse.swt.graphics.Resource;
|
|
21 import org.eclipse.swt.graphics.RGB;
|
|
22 import org.eclipse.swt.graphics.Device;
|
|
23
|
|
24
|
|
25
|
|
26 /**
|
|
27 * Instances of this class manage the operating system resources that
|
|
28 * implement SWT'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 * @see <a href="http://www.eclipse.org/swt/snippets/#color">Color and RGB snippets</a>
|
|
40 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: PaintExample</a>
|
|
41 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
42 */
|
|
43 public final class Color : Resource {
|
|
44 alias Resource.init_ init_;
|
|
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 SWT
|
|
50 * public API. It is marked public only so that it can be shared
|
|
51 * within the packages provided by SWT. It is not available on all
|
|
52 * platforms and should never be accessed from application code.
|
|
53 * </p>
|
|
54 */
|
|
55 public GdkColor* handle;
|
|
56
|
|
57 this(Device device) {
|
|
58 super(device);
|
|
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 super(device);
|
|
87 init_(red, green, blue);
|
|
88 init_();
|
|
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 super(device);
|
|
115 if (rgb is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
116 init_(rgb.red, rgb.green, rgb.blue);
|
|
117 init_();
|
|
118 }
|
|
119
|
|
120 void destroy() {
|
|
121 int pixel = handle.pixel;
|
|
122 if (device.colorRefCount !is null) {
|
|
123 /* If this was the last reference, remove the color from the list */
|
|
124 if (--device.colorRefCount[pixel] is 0) {
|
|
125 device.gdkColors[pixel] = null;
|
|
126 }
|
|
127 }
|
|
128 auto colormap = OS.gdk_colormap_get_system();
|
|
129 OS.gdk_colormap_free_colors(colormap, handle, 1);
|
|
130 handle = null;
|
|
131 }
|
|
132
|
|
133 /**
|
|
134 * Compares the argument to the receiver, and returns true
|
|
135 * if they represent the <em>same</em> object using a class
|
|
136 * specific comparison.
|
|
137 *
|
|
138 * @param object the object to compare with this object
|
|
139 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
|
|
140 *
|
|
141 * @see #hashCode
|
|
142 */
|
26
|
143 public override equals_t opEquals(Object object) {
|
25
|
144 if (object is this) return true;
|
|
145 if ( auto color = cast(Color)object ){
|
|
146 GdkColor* gdkColor = color.handle;
|
|
147 if (handle is gdkColor) return true;
|
|
148 return device is color.device && handle.red is gdkColor.red &&
|
|
149 handle.green is gdkColor.green && handle.blue is gdkColor.blue;
|
|
150 }
|
|
151 return false;
|
|
152 }
|
|
153
|
|
154 /**
|
|
155 * Returns the amount of blue in the color, from 0 to 255.
|
|
156 *
|
|
157 * @return the blue component of the color
|
|
158 *
|
|
159 * @exception SWTException <ul>
|
|
160 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
161 * </ul>
|
|
162 */
|
|
163 public int getBlue() {
|
|
164 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
165 return (handle.blue >> 8) & 0xFF;
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Returns the amount of green in the color, from 0 to 255.
|
|
170 *
|
|
171 * @return the green component of the color
|
|
172 *
|
|
173 * @exception SWTException <ul>
|
|
174 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
175 * </ul>
|
|
176 */
|
|
177 public int getGreen() {
|
|
178 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
179 return (handle.green >> 8) & 0xFF;
|
|
180 }
|
|
181
|
|
182 /**
|
|
183 * Returns the amount of red in the color, from 0 to 255.
|
|
184 *
|
|
185 * @return the red component of the color
|
|
186 *
|
|
187 * @exception SWTException <ul>
|
|
188 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
189 * </ul>
|
|
190 */
|
|
191 public int getRed() {
|
|
192 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
193 return (handle.red >> 8) & 0xFF;
|
|
194 }
|
|
195
|
|
196 /**
|
|
197 * Returns an integer hash code for the receiver. Any two
|
|
198 * objects that return <code>true</code> when passed to
|
|
199 * <code>equals</code> must return the same value for this
|
|
200 * method.
|
|
201 *
|
|
202 * @return the receiver's hash
|
|
203 *
|
|
204 * @see #equals
|
|
205 */
|
|
206 public override hash_t toHash() {
|
|
207 if (isDisposed()) return 0;
|
|
208 return handle.red ^ handle.green ^ handle.blue;
|
|
209 }
|
|
210
|
|
211 /**
|
|
212 * Returns an <code>RGB</code> representing the receiver.
|
|
213 *
|
|
214 * @return the RGB for the color
|
|
215 *
|
|
216 * @exception SWTException <ul>
|
|
217 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
218 * </ul>
|
|
219 */
|
|
220 public RGB getRGB () {
|
|
221 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
222 return new RGB(getRed(), getGreen(), getBlue());
|
|
223 }
|
|
224
|
|
225 /**
|
|
226 * Invokes platform specific functionality to allocate a new color.
|
|
227 * <p>
|
|
228 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
229 * API for <code>Color</code>. It is marked public only so that it
|
|
230 * can be shared within the packages provided by SWT. It is not
|
|
231 * available on all platforms, and should never be called from
|
|
232 * application code.
|
|
233 * </p>
|
|
234 *
|
|
235 * @param device the device on which to allocate the color
|
|
236 * @param handle the handle for the color
|
|
237 *
|
|
238 * @private
|
|
239 */
|
|
240 public static Color gtk_new(Device device, GdkColor* gdkColor) {
|
|
241 Color color = new Color(device);
|
|
242 color.handle = gdkColor;
|
|
243 return color;
|
|
244 }
|
|
245
|
|
246 void init_(int red, int green, int blue) {
|
|
247 if ((red > 255) || (red < 0) ||
|
|
248 (green > 255) || (green < 0) ||
|
|
249 (blue > 255) || (blue < 0)) {
|
|
250 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
251 }
|
|
252 GdkColor* gdkColor = new GdkColor();
|
|
253 gdkColor.red = cast(short)((red & 0xFF) | ((red & 0xFF) << 8));
|
|
254 gdkColor.green = cast(short)((green & 0xFF) | ((green & 0xFF) << 8));
|
|
255 gdkColor.blue = cast(short)((blue & 0xFF) | ((blue & 0xFF) << 8));
|
|
256 auto colormap = OS.gdk_colormap_get_system();
|
|
257 if (!OS.gdk_colormap_alloc_color(colormap, gdkColor, true, true)) {
|
|
258 /* Allocate black. */
|
|
259 gdkColor = new GdkColor();
|
|
260 OS.gdk_colormap_alloc_color(colormap, gdkColor, true, true);
|
|
261 }
|
|
262 handle = gdkColor;
|
|
263 if (device.colorRefCount !is null) {
|
|
264 /* Make a copy of the color to put in the colors array */
|
|
265 GdkColor* colorCopy = new GdkColor();
|
|
266 colorCopy.red = handle.red;
|
|
267 colorCopy.green = handle.green;
|
|
268 colorCopy.blue = handle.blue;
|
|
269 colorCopy.pixel = handle.pixel;
|
|
270 device.gdkColors[colorCopy.pixel] = colorCopy;
|
|
271 device.colorRefCount[colorCopy.pixel]++;
|
|
272 }
|
|
273 }
|
|
274
|
|
275 /**
|
|
276 * Returns <code>true</code> if the color has been disposed,
|
|
277 * and <code>false</code> otherwise.
|
|
278 * <p>
|
|
279 * This method gets the dispose state for the color.
|
|
280 * When a color has been disposed, it is an error to
|
|
281 * invoke any other method using the color.
|
|
282 *
|
|
283 * @return <code>true</code> when the color is disposed and <code>false</code> otherwise
|
|
284 */
|
|
285 public override bool isDisposed() {
|
|
286 return handle is null;
|
|
287 }
|
|
288
|
|
289 /**
|
|
290 * Returns a string containing a concise, human-readable
|
|
291 * description of the receiver.
|
|
292 *
|
|
293 * @return a string representation of the receiver
|
|
294 */
|
|
295 public override String toString () {
|
|
296 if (isDisposed()) return "Color {*DISPOSED*}";
|
|
297 return Format( "Color {{{}, {}, {}}", getRed(), getGreen(), getBlue());
|
|
298 }
|
|
299
|
|
300 }
|