1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 17 31 32 public final class Color extends Resource { 33 34 44 public int handle; 45 46 49 Color() { 50 } 51 52 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 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 118 public void dispose() { 119 if (handle == -1) return; 120 if (device.isDisposed()) return; 121 122 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 151 public boolean equals (Object 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 167 public int getBlue () { 168 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 169 return (handle & 0xFF0000) >> 16; 170 } 171 172 181 public int getGreen () { 182 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 183 return (handle & 0xFF00) >> 8 ; 184 } 185 186 195 public int getRed () { 196 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 197 return handle & 0xFF; 198 } 199 200 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 224 public int hashCode () { 225 return handle; 226 } 227 228 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 251 int hPal = device.hPalette; 252 if (hPal == 0) return; 253 254 int[] colorRefCount = device.colorRefCount; 255 256 257 int index = OS.GetNearestPaletteIndex(hPal, handle); 258 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 264 colorRefCount[index]++; 265 return; 266 } 267 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 278 279 handle = (entry[0] & 0xFF) | ((entry[1] & 0xFF) << 8) | 280 ((entry[2] & 0xFF) << 16); 281 } else { 282 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 299 public boolean isDisposed() { 300 return handle == -1; 301 } 302 303 309 public String toString () { 310 if (isDisposed()) return "Color {*DISPOSED*}"; return "Color {" + getRed() + ", " + getGreen() + ", " + getBlue() + "}"; } 313 314 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 |