PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/eclipse/swt/graphics/Color.d

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