KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > PaletteRecord


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff;
21
22 import jxl.read.biff.Record;
23 import jxl.format.Colour;
24 import jxl.format.RGB;
25
26 /**
27  * A record representing the RGB colour palette
28  */

29 public class PaletteRecord extends WritableRecordData
30 {
31   /**
32    * The list of bespoke rgb colours used by this sheet
33    */

34   private RGB[] rgbColours = new RGB[numColours];
35
36   /**
37    * A dirty flag indicating that this palette has been tampered with
38    * in some way
39    */

40   private boolean dirty;
41
42   /**
43    * Flag indicating that the palette was read in
44    */

45   private boolean read;
46
47   /**
48    * Initialized flag
49    */

50   private boolean initialized;
51
52   /**
53    * The number of colours in the palette
54    */

55   private static final int numColours = 56;
56
57   /**
58    * Constructor
59    *
60    * @param t the raw bytes
61    */

62   public PaletteRecord(Record t)
63   {
64     super(t);
65
66     initialized = false;
67     dirty = false;
68     read = true;
69   }
70
71   /**
72    * Default constructor - used when there is no palette specified
73    */

74   public PaletteRecord()
75   {
76     super(Type.PALETTE);
77
78     initialized = true;
79     dirty = false;
80     read = false;
81
82     // Initialize the array with all the default colours
83
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   /**
96    * Accessor for the binary data - used when copying
97    *
98    * @return the binary data
99    */

100   public byte[] getData()
101   {
102     // Palette was read in, but has not been changed
103
if (read && !dirty)
104     {
105       return getRecord().getData();
106     }
107
108     byte[] data = new byte[numColours * 4 + 2];
109     int pos = 0;
110
111     // Set the number of records
112
IntegerHelper.getTwoBytes(numColours, data, pos);
113
114     // Set the rgb content
115
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   /**
127    * Initialize the record data
128    */

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   /**
148    * Accessor for the dirty flag, which indicates if this palette has been
149    * modified
150    *
151    * @return TRUE if the palette has been modified, FALSE if it is the default
152    */

153   public boolean isDirty()
154   {
155     return dirty;
156   }
157
158   /**
159    * Sets the RGB value for the specified colour for this workbook
160    *
161    * @param c the colour whose RGB value is to be overwritten
162    * @param r the red portion to set (0-255)
163    * @param g the green portion to set (0-255)
164    * @param b the blue portion to set (0-255)
165    */

166   public void setColourRGB(Colour c, int r, int g, int b)
167   {
168     // Only colours on the standard palette with values 8-64 are acceptable
169
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     // Force the colours into the range 0-255
181
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     // Indicate that the palette has been modified
188
dirty = true;
189   }
190
191   /**
192    * Gets the colour RGB from the palette
193    *
194    * @param c the colour
195    * @return an RGB structure
196    */

197   public RGB getColourRGB(Colour c)
198   {
199     // Only colours on the standard palette with values 8-64 are acceptable
200
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   /**
215    * Forces the value passed in to be between the range passed in
216    *
217    * @param val the value to constrain
218    * @param min the minimum acceptable value
219    * @param max the maximum acceptable value
220    * @return the constrained value
221    */

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