FileDocCategorySizeDatePackage
PaletteRecord.javaAPI DocApache Poi 3.0.110100Mon Jan 01 12:39:40 GMT 2007org.apache.poi.hssf.record

PaletteRecord

public class PaletteRecord extends Record
PaletteRecord - Supports custom palettes.
author
Andrew C. Oliver (acoliver at apache dot org)
author
Brian Sanders (bsanders at risklabs dot com) - custom palette editing
version
2.0-pre

Fields Summary
public static final short
sid
public static final byte
STANDARD_PALETTE_SIZE
The standard size of an XLS palette
public static final short
FIRST_COLOR_INDEX
The byte index of the first color
private short
field_1_numcolors
private List
field_2_colors
Constructors Summary
public PaletteRecord()


     
    
      createDefaultPalette();
    
public PaletteRecord(RecordInputStream in)
Constructs a PaletteRecord record and sets its fields appropriately.

param
id id must be 0x92 or an exception will be throw upon validation
param
size the size of the data area of the record
param
data data of the record (should not contain sid/len)

        super(in);
    
Methods Summary
private voidcreateDefaultPalette()
Creates the default palette as PaletteRecord binary data

see
org.apache.poi.hssf.model.Workbook#createPalette

      field_1_numcolors = STANDARD_PALETTE_SIZE;
      field_2_colors    = new ArrayList(field_1_numcolors);
      byte[] palette = new byte[]
        {
            (byte) 0, (byte) 0, (byte) 0, (byte) 0, //color 0...
            (byte) 255, (byte) 255, (byte) 255, (byte) 0,
            (byte) 255, (byte) 0, (byte) 0, (byte) 0,
            (byte) 0, (byte) 255, (byte) 0, (byte) 0,
            (byte) 0, (byte) 0, (byte) 255, (byte) 0,
            (byte) 255, (byte) 255, (byte) 0, (byte) 0,
            (byte) 255, (byte) 0, (byte) 255, (byte) 0,
            (byte) 0, (byte) 255, (byte) 255, (byte) 0,
            (byte) 128, (byte) 0, (byte) 0, (byte) 0,
            (byte) 0, (byte) 128, (byte) 0, (byte) 0,
            (byte) 0, (byte) 0, (byte) 128, (byte) 0,
            (byte) 128, (byte) 128, (byte) 0, (byte) 0,
            (byte) 128, (byte) 0, (byte) 128, (byte) 0,
            (byte) 0, (byte) 128, (byte) 128, (byte) 0,
            (byte) 192, (byte) 192, (byte) 192, (byte) 0,
            (byte) 128, (byte) 128, (byte) 128, (byte) 0,
            (byte) 153, (byte) 153, (byte) 255, (byte) 0,
            (byte) 153, (byte) 51, (byte) 102, (byte) 0,
            (byte) 255, (byte) 255, (byte) 204, (byte) 0,
            (byte) 204, (byte) 255, (byte) 255, (byte) 0,
            (byte) 102, (byte) 0, (byte) 102, (byte) 0,
            (byte) 255, (byte) 128, (byte) 128, (byte) 0,
            (byte) 0, (byte) 102, (byte) 204, (byte) 0,
            (byte) 204, (byte) 204, (byte) 255, (byte) 0,
            (byte) 0, (byte) 0, (byte) 128, (byte) 0,
            (byte) 255, (byte) 0, (byte) 255, (byte) 0,
            (byte) 255, (byte) 255, (byte) 0, (byte) 0,
            (byte) 0, (byte) 255, (byte) 255, (byte) 0, 
            (byte) 128, (byte) 0, (byte) 128, (byte) 0,
            (byte) 128, (byte) 0, (byte) 0, (byte) 0,
            (byte) 0, (byte) 128, (byte) 128, (byte) 0,
            (byte) 0, (byte) 0, (byte) 255, (byte) 0,
            (byte) 0, (byte) 204, (byte) 255, (byte) 0,
            (byte) 204, (byte) 255, (byte) 255, (byte) 0,
            (byte) 204, (byte) 255, (byte) 204, (byte) 0,
            (byte) 255, (byte) 255, (byte) 153, (byte) 0,
            (byte) 153, (byte) 204, (byte) 255, (byte) 0,
            (byte) 255, (byte) 153, (byte) 204, (byte) 0,
            (byte) 204, (byte) 153, (byte) 255, (byte) 0,
            (byte) 255, (byte) 204, (byte) 153, (byte) 0,
            (byte) 51, (byte) 102, (byte) 255, (byte) 0,
            (byte) 51, (byte) 204, (byte) 204, (byte) 0,
            (byte) 153, (byte) 204, (byte) 0, (byte) 0,
            (byte) 255, (byte) 204, (byte) 0, (byte) 0,
            (byte) 255, (byte) 153, (byte) 0, (byte) 0,
            (byte) 255, (byte) 102, (byte) 0, (byte) 0,
            (byte) 102, (byte) 102, (byte) 153, (byte) 0,
            (byte) 150, (byte) 150, (byte) 150, (byte) 0,
            (byte) 0, (byte) 51, (byte) 102, (byte) 0,
            (byte) 51, (byte) 153, (byte) 102, (byte) 0,
            (byte) 0, (byte) 51, (byte) 0, (byte) 0,
            (byte) 51, (byte) 51, (byte) 0, (byte) 0,
            (byte) 153, (byte) 51, (byte) 0, (byte) 0,
            (byte) 153, (byte) 51, (byte) 102, (byte) 0,
            (byte) 51, (byte) 51, (byte) 153, (byte) 0,
            (byte) 51, (byte) 51, (byte) 51, (byte) 0
        };
      
      for (int k = 0; k < field_1_numcolors; k++) {
          field_2_colors.add(new PColor(
                                        palette[k*4],
                                        palette[k*4+1],
                                        palette[k*4+2]
                                       )
                             );
      }
      
    
protected voidfillFields(org.apache.poi.hssf.record.RecordInputStream in)

       field_1_numcolors = in.readShort();
       field_2_colors    = new ArrayList(field_1_numcolors);
       for (int k = 0; k < field_1_numcolors; k++) {
           field_2_colors.add(new PColor(
                                         in.readByte(),
                                         in.readByte(),
                                         in.readByte()
                                        )
                              );
           //Read unused byte.
           in.readByte();
       } 
    
public byte[]getColor(short byteIndex)
Returns the color value at a given index

return
the RGB triplet for the color, or null if the specified index does not exist

        int i = byteIndex - FIRST_COLOR_INDEX;
        if (i < 0 || i >= field_2_colors.size())
        {
            return null;
        }
        PColor color = (PColor) field_2_colors.get(i);
        return new byte[] { color.red, color.green, color.blue };
    
public intgetRecordSize()

        return 4 + 2 + (field_1_numcolors * 4);
    
public shortgetSid()

        return sid;
    
public intserialize(int offset, byte[] data)

        LittleEndian.putShort(data, 0 + offset, sid);
        LittleEndian.putShort(data, 2 + offset, (short) (getRecordSize() - 4));
        LittleEndian.putShort(data, 4 + offset, field_1_numcolors);
        for (int k = 0; k < field_1_numcolors; k++) {
          PColor c = (PColor)field_2_colors.get(k);
          c.serialize(data, (6+offset+(k*4)));
        }

        return getRecordSize();
    
public voidsetColor(short byteIndex, byte red, byte green, byte blue)
Sets the color value at a given index If the given index is greater than the current last color index, then black is inserted at every index required to make the palette continuous.

param
byteIndex the index to set; if this index is less than 0x8 or greater than 0x40, then no modification is made

        int i = byteIndex - FIRST_COLOR_INDEX;
        if (i < 0 || i >= STANDARD_PALETTE_SIZE)
        {
            return;
        }
        while (field_2_colors.size() <= i)
        {
            field_2_colors.add(new PColor((byte) 0, (byte) 0, (byte) 0));
        }
        PColor custColor = new PColor(red, green, blue);
        field_2_colors.set(i, custColor);
    
public java.lang.StringtoString()

        StringBuffer buffer = new StringBuffer();

        buffer.append("[PALETTE]\n");
        buffer.append("  numcolors     = ").append(field_1_numcolors)
              .append('\n");
        for (int k = 0; k < field_1_numcolors; k++) {
        PColor c = (PColor) field_2_colors.get(k);
        buffer.append("* colornum      = ").append(k)
              .append('\n");
        buffer.append(c.toString());
        buffer.append("/*colornum      = ").append(k)
              .append('\n");
        }
        buffer.append("[/PALETTE]\n");
        return buffer.toString();
    
protected voidvalidateSid(short id)

        if (id != sid)
        {
            throw new RecordFormatException("NOT An Palette RECORD");
        }