1 19 20 package jxl.biff; 21 22 import jxl.read.biff.Record; 23 import jxl.format.Colour; 24 import jxl.format.RGB; 25 26 29 public class PaletteRecord extends WritableRecordData 30 { 31 34 private RGB[] rgbColours = new RGB[numColours]; 35 36 40 private boolean dirty; 41 42 45 private boolean read; 46 47 50 private boolean initialized; 51 52 55 private static final int numColours = 56; 56 57 62 public PaletteRecord(Record t) 63 { 64 super(t); 65 66 initialized = false; 67 dirty = false; 68 read = true; 69 } 70 71 74 public PaletteRecord() 75 { 76 super(Type.PALETTE); 77 78 initialized = true; 79 dirty = false; 80 read = false; 81 82 Colour[] colours = Colour.getAllColours(); 84 85 for (int i = 0; i < colours.length; i++) 86 { 87 Colour c = colours[i]; 88 setColourRGB(c, 89 c.getDefaultRGB().getRed(), 90 c.getDefaultRGB().getGreen(), 91 c.getDefaultRGB().getBlue()); 92 } 93 } 94 95 100 public byte[] getData() 101 { 102 if (read && !dirty) 104 { 105 return getRecord().getData(); 106 } 107 108 byte[] data = new byte[numColours * 4 + 2]; 109 int pos = 0; 110 111 IntegerHelper.getTwoBytes(numColours, data, pos); 113 114 for (int i = 0; i < numColours; i++) 116 { 117 pos = i * 4 + 2; 118 data[pos] = (byte) rgbColours[i].getRed(); 119 data[pos + 1] = (byte) rgbColours[i].getGreen(); 120 data[pos + 2] = (byte) rgbColours[i].getBlue(); 121 } 122 123 return data; 124 } 125 126 129 private void initialize() 130 { 131 byte[] data = getRecord().getData(); 132 133 int numrecords = IntegerHelper.getInt(data[0], data[1]); 134 135 for (int i = 0; i < numrecords; i++) 136 { 137 int pos = i * 4 + 2; 138 int red = IntegerHelper.getInt(data[pos], (byte) 0); 139 int green = IntegerHelper.getInt(data[pos + 1], (byte) 0); 140 int blue = IntegerHelper.getInt(data[pos + 2], (byte) 0); 141 rgbColours[i] = new RGB(red, green, blue); 142 } 143 144 initialized = true; 145 } 146 147 153 public boolean isDirty() 154 { 155 return dirty; 156 } 157 158 166 public void setColourRGB(Colour c, int r, int g, int b) 167 { 168 int pos = c.getValue() - 8; 170 if (pos < 0 || pos >= numColours) 171 { 172 return; 173 } 174 175 if (!initialized) 176 { 177 initialize(); 178 } 179 180 r = setValueRange(r, 0, 0xff); 182 g = setValueRange(g, 0, 0xff); 183 b = setValueRange(b, 0, 0xff); 184 185 rgbColours[pos] = new RGB(r, g, b); 186 187 dirty = true; 189 } 190 191 197 public RGB getColourRGB(Colour c) 198 { 199 int pos = c.getValue() - 8; 201 if (pos < 0 || pos >= numColours) 202 { 203 return c.getDefaultRGB(); 204 } 205 206 if (!initialized) 207 { 208 initialize(); 209 } 210 211 return rgbColours[pos]; 212 } 213 214 222 private int setValueRange(int val, int min, int max) 223 { 224 val = Math.max(val, min); 225 val = Math.min(val, max); 226 return val; 227 } 228 } 229 | Popular Tags |