1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.*; 15 16 54 55 public final class PaletteData { 56 57 61 public boolean isDirect; 62 63 67 public RGB[] colors; 68 69 72 public int redMask; 73 74 77 public int greenMask; 78 79 82 public int blueMask; 83 84 87 public int redShift; 88 89 92 public int greenShift; 93 94 97 public int blueShift; 98 99 108 public PaletteData(RGB[] colors) { 109 if (colors == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 110 this.colors = colors; 111 this.isDirect = false; 112 } 113 114 121 public PaletteData(int redMask, int greenMask, int blueMask) { 122 this.redMask = redMask; 123 this.greenMask = greenMask; 124 this.blueMask = blueMask; 125 this.isDirect = true; 126 this.redShift = shiftForMask(redMask); 127 this.greenShift = shiftForMask(greenMask); 128 this.blueShift = shiftForMask(blueMask); 129 } 130 131 142 public int getPixel(RGB rgb) { 143 if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 144 if (isDirect) { 145 int pixel = 0; 146 pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask; 147 pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask; 148 pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask; 149 return pixel; 150 } else { 151 for (int i = 0; i < colors.length; i++) { 152 if (colors[i].equals(rgb)) return i; 153 } 154 155 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 156 return 0; 157 } 158 } 159 160 171 public RGB getRGB(int pixel) { 172 if (isDirect) { 173 int r = pixel & redMask; 174 r = (redShift < 0) ? r >>> -redShift : r << redShift; 175 int g = pixel & greenMask; 176 g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; 177 int b = pixel & blueMask; 178 b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; 179 return new RGB(r, g, b); 180 } else { 181 if (pixel < 0 || pixel >= colors.length) { 182 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 183 } 184 return colors[pixel]; 185 } 186 } 187 188 194 public RGB[] getRGBs() { 195 return colors; 196 } 197 198 206 int shiftForMask(int mask) { 207 for (int i = 31; i >= 0; i--) { 208 if (((mask >> i) & 0x1) != 0) return 7 - i; 209 } 210 return 32; 211 } 212 213 } 214 | Popular Tags |