Lines 63-68
Link Here
|
63 |
/** |
63 |
/** |
64 |
* PaletteRecord - Supports custom palettes. |
64 |
* PaletteRecord - Supports custom palettes. |
65 |
* @author Andrew C. Oliver (acoliver at apache dot org) |
65 |
* @author Andrew C. Oliver (acoliver at apache dot org) |
|
|
66 |
* @author Brian Sanders (bsanders at risklabs dot com) - custom palette editing |
66 |
* @version 2.0-pre |
67 |
* @version 2.0-pre |
67 |
*/ |
68 |
*/ |
68 |
|
69 |
|
Lines 70-82
Link Here
|
70 |
extends Record |
71 |
extends Record |
71 |
{ |
72 |
{ |
72 |
public final static short sid = 0x92; |
73 |
public final static short sid = 0x92; |
73 |
|
74 |
/** The standard size of an XLS palette */ |
|
|
75 |
public final static byte STANDARD_PALETTE_SIZE = (byte) 56; |
76 |
/** The byte index of the first color */ |
77 |
public final static short FIRST_COLOR_INDEX = (short) 0x8; |
78 |
|
74 |
private short field_1_numcolors; |
79 |
private short field_1_numcolors; |
75 |
private List field_2_colors; |
80 |
private List field_2_colors; |
76 |
|
81 |
|
77 |
public PaletteRecord() |
82 |
public PaletteRecord() |
78 |
{ |
83 |
{ |
79 |
} |
84 |
} |
|
|
85 |
|
86 |
/** |
87 |
* Constructs a custom palette with the default set of colors |
88 |
*/ |
89 |
public PaletteRecord(short id) |
90 |
{ |
91 |
super(id, STANDARD_PALETTE_SIZE, getDefaultData()); |
92 |
} |
80 |
|
93 |
|
81 |
/** |
94 |
/** |
82 |
* Constructs a PaletteRecord record and sets its fields appropriately. |
95 |
* Constructs a PaletteRecord record and sets its fields appropriately. |
Lines 131-137
Link Here
|
131 |
{ |
144 |
{ |
132 |
StringBuffer buffer = new StringBuffer(); |
145 |
StringBuffer buffer = new StringBuffer(); |
133 |
|
146 |
|
134 |
buffer.append("[Palette]\n"); |
147 |
buffer.append("[PALETTE]\n"); |
135 |
buffer.append(" numcolors = ").append(field_1_numcolors) |
148 |
buffer.append(" numcolors = ").append(field_1_numcolors) |
136 |
.append('\n'); |
149 |
.append('\n'); |
137 |
for (int k = 0; k < field_1_numcolors; k++) { |
150 |
for (int k = 0; k < field_1_numcolors; k++) { |
Lines 142-157
Link Here
|
142 |
buffer.append("/*colornum = ").append(k) |
155 |
buffer.append("/*colornum = ").append(k) |
143 |
.append('\n'); |
156 |
.append('\n'); |
144 |
} |
157 |
} |
145 |
buffer.append("[/Palette]\n"); |
158 |
buffer.append("[/PALETTE]\n"); |
146 |
return buffer.toString(); |
159 |
return buffer.toString(); |
147 |
} |
160 |
} |
148 |
|
161 |
|
149 |
public int serialize(int offset, byte [] data) |
162 |
public int serialize(int offset, byte [] data) |
150 |
{ |
163 |
{ |
151 |
LittleEndian.putShort(data, 0 + offset, sid); |
164 |
LittleEndian.putShort(data, 0 + offset, sid); |
|
|
165 |
LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4)); |
166 |
LittleEndian.putShort(data, 4 + offset, field_1_numcolors); |
152 |
for (int k = 0; k < field_1_numcolors; k++) { |
167 |
for (int k = 0; k < field_1_numcolors; k++) { |
153 |
PColor c = (PColor)field_2_colors.get(k); |
168 |
PColor c = (PColor)field_2_colors.get(k); |
154 |
c.serialize(data, (2+offset+(k*4))); |
169 |
c.serialize(data, (6+offset+(k*4))); |
155 |
} |
170 |
} |
156 |
|
171 |
|
157 |
return getRecordSize(); |
172 |
return getRecordSize(); |
Lines 159-165
Link Here
|
159 |
|
174 |
|
160 |
public int getRecordSize() |
175 |
public int getRecordSize() |
161 |
{ |
176 |
{ |
162 |
return 2 + (field_1_numcolors * 4); |
177 |
return 4 + 2 + (field_1_numcolors * 4); |
163 |
} |
178 |
} |
164 |
|
179 |
|
165 |
public short getSid() |
180 |
public short getSid() |
Lines 167-172
Link Here
|
167 |
return this.sid; |
182 |
return this.sid; |
168 |
} |
183 |
} |
169 |
|
184 |
|
|
|
185 |
/** |
186 |
* Returns the color value at a given index |
187 |
* |
188 |
* @return the RGB triplet for the color, or null if the specified index |
189 |
* does not exist |
190 |
*/ |
191 |
public byte[] getColor(short byteIndex) |
192 |
{ |
193 |
int i = byteIndex - FIRST_COLOR_INDEX; |
194 |
if (i < 0 || i >= field_2_colors.size()) |
195 |
{ |
196 |
return null; |
197 |
} |
198 |
PColor color = (PColor) field_2_colors.get(i); |
199 |
return new byte[] { color.red, color.green, color.blue }; |
200 |
} |
201 |
|
202 |
/** |
203 |
* Sets the color value at a given index |
204 |
* |
205 |
* If the given index is greater than the current last color index, |
206 |
* then black is inserted at every index required to make the palette continuous. |
207 |
* |
208 |
* @param i the index to set; if this index is less than 0x8 or greater than |
209 |
* 0x40, then no modification is made |
210 |
*/ |
211 |
public void setColor(short byteIndex, byte red, byte green, byte blue) |
212 |
{ |
213 |
int i = byteIndex - FIRST_COLOR_INDEX; |
214 |
if (i < 0 || i >= STANDARD_PALETTE_SIZE) |
215 |
{ |
216 |
return; |
217 |
} |
218 |
while (field_2_colors.size() <= i) |
219 |
{ |
220 |
field_2_colors.add(new PColor((byte) 0, (byte) 0, (byte) 0)); |
221 |
} |
222 |
PColor custColor = new PColor(red, green, blue); |
223 |
field_2_colors.set(i, custColor); |
224 |
} |
225 |
|
226 |
/** |
227 |
* Returns the default palette as PaletteRecord binary data |
228 |
* |
229 |
* @see org.apache.poi.hssf.model.Workbook#createPalette |
230 |
*/ |
231 |
public static byte[] getDefaultData() |
232 |
{ |
233 |
return new byte[] |
234 |
{ |
235 |
STANDARD_PALETTE_SIZE, (byte) 0, |
236 |
(byte) 0, (byte) 0, (byte) 0, (byte) 0, //color 0... |
237 |
(byte) 255, (byte) 255, (byte) 255, (byte) 0, |
238 |
(byte) 255, (byte) 0, (byte) 0, (byte) 0, |
239 |
(byte) 0, (byte) 255, (byte) 0, (byte) 0, |
240 |
(byte) 0, (byte) 0, (byte) 255, (byte) 0, |
241 |
(byte) 255, (byte) 255, (byte) 0, (byte) 0, |
242 |
(byte) 255, (byte) 0, (byte) 255, (byte) 0, |
243 |
(byte) 0, (byte) 255, (byte) 255, (byte) 0, |
244 |
(byte) 128, (byte) 0, (byte) 0, (byte) 0, |
245 |
(byte) 0, (byte) 128, (byte) 0, (byte) 0, |
246 |
(byte) 0, (byte) 0, (byte) 128, (byte) 0, |
247 |
(byte) 128, (byte) 128, (byte) 0, (byte) 0, |
248 |
(byte) 128, (byte) 0, (byte) 128, (byte) 0, |
249 |
(byte) 0, (byte) 128, (byte) 128, (byte) 0, |
250 |
(byte) 192, (byte) 192, (byte) 192, (byte) 0, |
251 |
(byte) 128, (byte) 128, (byte) 128, (byte) 0, |
252 |
(byte) 153, (byte) 153, (byte) 255, (byte) 0, |
253 |
(byte) 153, (byte) 51, (byte) 102, (byte) 0, |
254 |
(byte) 255, (byte) 255, (byte) 204, (byte) 0, |
255 |
(byte) 204, (byte) 255, (byte) 255, (byte) 0, |
256 |
(byte) 102, (byte) 0, (byte) 102, (byte) 0, |
257 |
(byte) 255, (byte) 128, (byte) 128, (byte) 0, |
258 |
(byte) 0, (byte) 102, (byte) 204, (byte) 0, |
259 |
(byte) 204, (byte) 204, (byte) 255, (byte) 0, |
260 |
(byte) 0, (byte) 0, (byte) 128, (byte) 0, |
261 |
(byte) 255, (byte) 0, (byte) 255, (byte) 0, |
262 |
(byte) 255, (byte) 255, (byte) 0, (byte) 0, |
263 |
(byte) 0, (byte) 255, (byte) 255, (byte) 0, |
264 |
(byte) 128, (byte) 0, (byte) 128, (byte) 0, |
265 |
(byte) 128, (byte) 0, (byte) 0, (byte) 0, |
266 |
(byte) 0, (byte) 128, (byte) 128, (byte) 0, |
267 |
(byte) 0, (byte) 0, (byte) 255, (byte) 0, |
268 |
(byte) 0, (byte) 204, (byte) 255, (byte) 0, |
269 |
(byte) 204, (byte) 255, (byte) 255, (byte) 0, |
270 |
(byte) 204, (byte) 255, (byte) 204, (byte) 0, |
271 |
(byte) 255, (byte) 255, (byte) 153, (byte) 0, |
272 |
(byte) 153, (byte) 204, (byte) 255, (byte) 0, |
273 |
(byte) 255, (byte) 153, (byte) 204, (byte) 0, |
274 |
(byte) 204, (byte) 153, (byte) 255, (byte) 0, |
275 |
(byte) 255, (byte) 204, (byte) 153, (byte) 0, |
276 |
(byte) 51, (byte) 102, (byte) 255, (byte) 0, |
277 |
(byte) 51, (byte) 204, (byte) 204, (byte) 0, |
278 |
(byte) 153, (byte) 204, (byte) 0, (byte) 0, |
279 |
(byte) 255, (byte) 204, (byte) 0, (byte) 0, |
280 |
(byte) 255, (byte) 153, (byte) 0, (byte) 0, |
281 |
(byte) 255, (byte) 102, (byte) 0, (byte) 0, |
282 |
(byte) 102, (byte) 102, (byte) 153, (byte) 0, |
283 |
(byte) 150, (byte) 150, (byte) 150, (byte) 0, |
284 |
(byte) 0, (byte) 51, (byte) 102, (byte) 0, |
285 |
(byte) 51, (byte) 153, (byte) 102, (byte) 0, |
286 |
(byte) 0, (byte) 51, (byte) 0, (byte) 0, |
287 |
(byte) 51, (byte) 51, (byte) 0, (byte) 0, |
288 |
(byte) 153, (byte) 51, (byte) 0, (byte) 0, |
289 |
(byte) 153, (byte) 51, (byte) 102, (byte) 0, |
290 |
(byte) 51, (byte) 51, (byte) 153, (byte) 0, |
291 |
(byte) 51, (byte) 51, (byte) 51, (byte) 0 |
292 |
}; |
293 |
} |
170 |
} |
294 |
} |
171 |
|
295 |
|
172 |
/** |
296 |
/** |
Lines 191-199
Link Here
|
191 |
|
315 |
|
192 |
public String toString() { |
316 |
public String toString() { |
193 |
StringBuffer buffer = new StringBuffer(); |
317 |
StringBuffer buffer = new StringBuffer(); |
194 |
buffer.append(" red = ").append(red).append('\n'); |
318 |
buffer.append(" red = ").append(red & 0xff).append('\n'); |
195 |
buffer.append(" green = ").append(green).append('\n'); |
319 |
buffer.append(" green = ").append(green & 0xff).append('\n'); |
196 |
buffer.append(" blue = ").append(blue).append('\n'); |
320 |
buffer.append(" blue = ").append(blue & 0xff).append('\n'); |
197 |
return buffer.toString(); |
321 |
return buffer.toString(); |
198 |
} |
322 |
} |
199 |
} |
323 |
} |