KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > PaletteRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.record;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.apache.poi.util.LittleEndian;
25
26 /**
27  * PaletteRecord - Supports custom palettes.
28  * @author Andrew C. Oliver (acoliver at apache dot org)
29  * @author Brian Sanders (bsanders at risklabs dot com) - custom palette editing
30  * @version 2.0-pre
31  */

32
33 public class PaletteRecord
34     extends Record
35 {
36     public final static short sid = 0x92;
37     /** The standard size of an XLS palette */
38     public final static byte STANDARD_PALETTE_SIZE = (byte) 56;
39     /** The byte index of the first color */
40     public final static short FIRST_COLOR_INDEX = (short) 0x8;
41     
42     private short field_1_numcolors;
43     private List JavaDoc field_2_colors;
44
45     public PaletteRecord()
46     {
47     }
48     
49     /**
50      * Constructs a custom palette with the default set of colors
51      */

52     public PaletteRecord(short id)
53     {
54         super(id, STANDARD_PALETTE_SIZE, getDefaultData());
55     }
56
57     /**
58      * Constructs a PaletteRecord record and sets its fields appropriately.
59      *
60      * @param id id must be 0x92 or an exception will be throw upon validation
61      * @param size the size of the data area of the record
62      * @param data data of the record (should not contain sid/len)
63      */

64
65     public PaletteRecord(short id, short size, byte [] data)
66     {
67         super(id, size, data);
68     }
69
70     /**
71      * Constructs a PaletteRecord record and sets its fields appropriately.
72      *
73      * @param id id must be 0x0A or an exception will be throw upon validation
74      * @param size the size of the data area of the record
75      * @param data data of the record (should not contain sid/len)
76      * @param offset of the record's data
77      */

78
79     public PaletteRecord(short id, short size, byte [] data, int offset)
80     {
81         super(id, size, data, offset);
82     }
83
84     protected void validateSid(short id)
85     {
86         if (id != sid)
87         {
88             throw new RecordFormatException("NOT An Palette RECORD");
89         }
90     }
91
92     protected void fillFields(byte [] data, short size, int offset)
93     {
94        field_1_numcolors = LittleEndian.getShort(data,offset+0);
95        field_2_colors = new ArrayList JavaDoc(field_1_numcolors);
96        for (int k = 0; k < field_1_numcolors; k++) {
97            field_2_colors.add(new PColor(
98                                          data[2+ offset+(k * 4) +0],
99                                          data[2+ offset+(k * 4) +1],
100                                          data[2+ offset+(k * 4) +2]
101                                         )
102                               );
103        }
104     }
105
106     public String JavaDoc toString()
107     {
108         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
109
110         buffer.append("[PALETTE]\n");
111         buffer.append(" numcolors = ").append(field_1_numcolors)
112               .append('\n');
113         for (int k = 0; k < field_1_numcolors; k++) {
114         PColor c = (PColor) field_2_colors.get(k);
115         buffer.append("* colornum = ").append(k)
116               .append('\n');
117         buffer.append(c.toString());
118         buffer.append("/*colornum = ").append(k)
119               .append('\n');
120         }
121         buffer.append("[/PALETTE]\n");
122         return buffer.toString();
123     }
124
125     public int serialize(int offset, byte [] data)
126     {
127         LittleEndian.putShort(data, 0 + offset, sid);
128         LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4));
129         LittleEndian.putShort(data, 4 + offset, field_1_numcolors);
130         for (int k = 0; k < field_1_numcolors; k++) {
131           PColor c = (PColor)field_2_colors.get(k);
132           c.serialize(data, (6+offset+(k*4)));
133         }
134
135         return getRecordSize();
136     }
137
138     public int getRecordSize()
139     {
140         return 4 + 2 + (field_1_numcolors * 4);
141     }
142
143     public short getSid()
144     {
145         return this.sid;
146     }
147
148     /**
149      * Returns the color value at a given index
150      *
151      * @return the RGB triplet for the color, or null if the specified index
152      * does not exist
153      */

154     public byte[] getColor(short byteIndex)
155     {
156         int i = byteIndex - FIRST_COLOR_INDEX;
157         if (i < 0 || i >= field_2_colors.size())
158         {
159             return null;
160         }
161         PColor color = (PColor) field_2_colors.get(i);
162         return new byte[] { color.red, color.green, color.blue };
163     }
164     
165     /**
166      * Sets the color value at a given index
167      *
168      * If the given index is greater than the current last color index,
169      * then black is inserted at every index required to make the palette continuous.
170      *
171      * @param byteIndex the index to set; if this index is less than 0x8 or greater than
172      * 0x40, then no modification is made
173      */

174     public void setColor(short byteIndex, byte red, byte green, byte blue)
175     {
176         int i = byteIndex - FIRST_COLOR_INDEX;
177         if (i < 0 || i >= STANDARD_PALETTE_SIZE)
178         {
179             return;
180         }
181         while (field_2_colors.size() <= i)
182         {
183             field_2_colors.add(new PColor((byte) 0, (byte) 0, (byte) 0));
184         }
185         PColor custColor = new PColor(red, green, blue);
186         field_2_colors.set(i, custColor);
187     }
188     
189     /**
190      * Returns the default palette as PaletteRecord binary data
191      *
192      * @see org.apache.poi.hssf.model.Workbook#createPalette
193      */

194     public static byte[] getDefaultData()
195     {
196         return new byte[]
197         {
198             STANDARD_PALETTE_SIZE, (byte) 0,
199             (byte) 0, (byte) 0, (byte) 0, (byte) 0, //color 0...
200
(byte) 255, (byte) 255, (byte) 255, (byte) 0,
201             (byte) 255, (byte) 0, (byte) 0, (byte) 0,
202             (byte) 0, (byte) 255, (byte) 0, (byte) 0,
203             (byte) 0, (byte) 0, (byte) 255, (byte) 0,
204             (byte) 255, (byte) 255, (byte) 0, (byte) 0,
205             (byte) 255, (byte) 0, (byte) 255, (byte) 0,
206             (byte) 0, (byte) 255, (byte) 255, (byte) 0,
207             (byte) 128, (byte) 0, (byte) 0, (byte) 0,
208             (byte) 0, (byte) 128, (byte) 0, (byte) 0,
209             (byte) 0, (byte) 0, (byte) 128, (byte) 0,
210             (byte) 128, (byte) 128, (byte) 0, (byte) 0,
211             (byte) 128, (byte) 0, (byte) 128, (byte) 0,
212             (byte) 0, (byte) 128, (byte) 128, (byte) 0,
213             (byte) 192, (byte) 192, (byte) 192, (byte) 0,
214             (byte) 128, (byte) 128, (byte) 128, (byte) 0,
215             (byte) 153, (byte) 153, (byte) 255, (byte) 0,
216             (byte) 153, (byte) 51, (byte) 102, (byte) 0,
217             (byte) 255, (byte) 255, (byte) 204, (byte) 0,
218             (byte) 204, (byte) 255, (byte) 255, (byte) 0,
219             (byte) 102, (byte) 0, (byte) 102, (byte) 0,
220             (byte) 255, (byte) 128, (byte) 128, (byte) 0,
221             (byte) 0, (byte) 102, (byte) 204, (byte) 0,
222             (byte) 204, (byte) 204, (byte) 255, (byte) 0,
223             (byte) 0, (byte) 0, (byte) 128, (byte) 0,
224             (byte) 255, (byte) 0, (byte) 255, (byte) 0,
225             (byte) 255, (byte) 255, (byte) 0, (byte) 0,
226             (byte) 0, (byte) 255, (byte) 255, (byte) 0,
227             (byte) 128, (byte) 0, (byte) 128, (byte) 0,
228             (byte) 128, (byte) 0, (byte) 0, (byte) 0,
229             (byte) 0, (byte) 128, (byte) 128, (byte) 0,
230             (byte) 0, (byte) 0, (byte) 255, (byte) 0,
231             (byte) 0, (byte) 204, (byte) 255, (byte) 0,
232             (byte) 204, (byte) 255, (byte) 255, (byte) 0,
233             (byte) 204, (byte) 255, (byte) 204, (byte) 0,
234             (byte) 255, (byte) 255, (byte) 153, (byte) 0,
235             (byte) 153, (byte) 204, (byte) 255, (byte) 0,
236             (byte) 255, (byte) 153, (byte) 204, (byte) 0,
237             (byte) 204, (byte) 153, (byte) 255, (byte) 0,
238             (byte) 255, (byte) 204, (byte) 153, (byte) 0,
239             (byte) 51, (byte) 102, (byte) 255, (byte) 0,
240             (byte) 51, (byte) 204, (byte) 204, (byte) 0,
241             (byte) 153, (byte) 204, (byte) 0, (byte) 0,
242             (byte) 255, (byte) 204, (byte) 0, (byte) 0,
243             (byte) 255, (byte) 153, (byte) 0, (byte) 0,
244             (byte) 255, (byte) 102, (byte) 0, (byte) 0,
245             (byte) 102, (byte) 102, (byte) 153, (byte) 0,
246             (byte) 150, (byte) 150, (byte) 150, (byte) 0,
247             (byte) 0, (byte) 51, (byte) 102, (byte) 0,
248             (byte) 51, (byte) 153, (byte) 102, (byte) 0,
249             (byte) 0, (byte) 51, (byte) 0, (byte) 0,
250             (byte) 51, (byte) 51, (byte) 0, (byte) 0,
251             (byte) 153, (byte) 51, (byte) 0, (byte) 0,
252             (byte) 153, (byte) 51, (byte) 102, (byte) 0,
253             (byte) 51, (byte) 51, (byte) 153, (byte) 0,
254             (byte) 51, (byte) 51, (byte) 51, (byte) 0
255         };
256     }
257 }
258
259 /**
260  * PColor - element in the list of colors - consider it a "struct"
261  */

262 class PColor {
263   public byte red;
264   public byte green;
265   public byte blue;
266   public PColor(byte red, byte green, byte blue) {
267     this.red=red;
268     this.green=green;
269     this.blue=blue;
270   }
271
272   public void serialize(byte[] data, int offset) {
273      data[offset + 0] = red;
274      data[offset + 1] = green;
275      data[offset + 2] = blue;
276      data[offset + 3] = 0;
277   }
278
279   public String JavaDoc toString() {
280         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
281         buffer.append(" red = ").append(red & 0xff).append('\n');
282         buffer.append(" green = ").append(green & 0xff).append('\n');
283         buffer.append(" blue = ").append(blue & 0xff).append('\n');
284         return buffer.toString();
285   }
286 }
287
Popular Tags