KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > HSSFPalette


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

16
17 package org.apache.poi.hssf.usermodel;
18
19 import org.apache.poi.hssf.record.PaletteRecord;
20 import org.apache.poi.hssf.util.HSSFColor;
21
22 /**
23  * Represents a workbook color palette.
24  * Internally, the XLS format refers to colors using an offset into the palette
25  * record. Thus, the first color in the palette has the index 0x8, the second
26  * has the index 0x9, etc. through 0x40
27  *
28  * @author Brian Sanders (bsanders at risklabs dot com)
29  */

30 public class HSSFPalette
31 {
32     private PaletteRecord palette;
33     
34     protected HSSFPalette(PaletteRecord palette)
35     {
36         this.palette = palette;
37     }
38     
39     /**
40      * Retrieves the color at a given index
41      *
42      * @param index the palette index, between 0x8 to 0x40 inclusive
43      * @return the color, or null if the index is not populated
44      */

45     public HSSFColor getColor(short index)
46     {
47         byte[] b = palette.getColor(index);
48         if (b != null)
49         {
50             return new CustomColor(index, b);
51         }
52         return null;
53     }
54     
55     /**
56      * Finds the first occurance of a given color
57      *
58      * @param red the RGB red component, between 0 and 255 inclusive
59      * @param green the RGB green component, between 0 and 255 inclusive
60      * @param blue the RGB blue component, between 0 and 255 inclusive
61      * @return the color, or null if the color does not exist in this palette
62      */

63     public HSSFColor findColor(byte red, byte green, byte blue)
64     {
65         byte[] b = palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
66         for (short i = (short) PaletteRecord.FIRST_COLOR_INDEX; b != null;
67             b = palette.getColor(++i))
68         {
69             if (b[0] == red && b[1] == green && b[2] == blue)
70             {
71                 return new CustomColor(i, b);
72             }
73         }
74         return null;
75     }
76
77     /**
78      * Finds the closest matching color in the custom palette. The
79      * method for finding the distance between the colors is fairly
80      * primative.
81      *
82      * @param red The red component of the color to match.
83      * @param green The green component of the color to match.
84      * @param blue The blue component of the color to match.
85      * @return The closest color or null if there are no custom
86      * colors currently defined.
87      */

88     public HSSFColor findSimilarColor(byte red, byte green, byte blue)
89     {
90         HSSFColor result = null;
91         int minColorDistance = Integer.MAX_VALUE;
92         byte[] b = palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
93         for (short i = (short) PaletteRecord.FIRST_COLOR_INDEX; b != null;
94             b = palette.getColor(++i))
95         {
96             int colorDistance = red - b[0] + green - b[1] + blue - b[2];
97             if (colorDistance < minColorDistance)
98             {
99                 result = getColor(i);
100             }
101         }
102         return result;
103     }
104
105     /**
106      * Sets the color at the given offset
107      *
108      * @param index the palette index, between 0x8 to 0x40 inclusive
109      * @param red the RGB red component, between 0 and 255 inclusive
110      * @param green the RGB green component, between 0 and 255 inclusive
111      * @param blue the RGB blue component, between 0 and 255 inclusive
112      */

113     public void setColorAtIndex(short index, byte red, byte green, byte blue)
114     {
115         palette.setColor(index, red, green, blue);
116     }
117
118     /**
119      * Adds a new color into an empty color slot.
120      * @param red The red component
121      * @param green The green component
122      * @param blue The blue component
123      *
124      * @return The new custom color.
125      *
126      * @throws RuntimeException if there are more more free color indexes.
127      */

128     public HSSFColor addColor( byte red, byte green, byte blue )
129     {
130         byte[] b = palette.getColor(PaletteRecord.FIRST_COLOR_INDEX);
131         short i;
132         for (i = (short) PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = palette.getColor(++i))
133         {
134             if (b == null)
135             {
136                 setColorAtIndex( i, red, green, blue );
137                 return getColor(i);
138             }
139         }
140         throw new RuntimeException JavaDoc("Could not find free color index");
141     }
142
143     private static class CustomColor extends HSSFColor
144     {
145         private short byteOffset;
146         private byte red;
147         private byte green;
148         private byte blue;
149         
150         private CustomColor(short byteOffset, byte[] colors)
151         {
152             this(byteOffset, colors[0], colors[1], colors[2]);
153         }
154         
155         private CustomColor(short byteOffset, byte red, byte green, byte blue)
156         {
157             this.byteOffset = byteOffset;
158             this.red = red;
159             this.green = green;
160             this.blue = blue;
161         }
162         
163         public short getIndex()
164         {
165             return byteOffset;
166         }
167         
168         public short[] getTriplet()
169         {
170             return new short[]
171             {
172                 (short) (red & 0xff),
173                 (short) (green & 0xff),
174                 (short) (blue & 0xff)
175             };
176         }
177         
178         public String JavaDoc getHexString()
179         {
180             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
181             sb.append(getGnumericPart(red));
182             sb.append(':');
183             sb.append(getGnumericPart(green));
184             sb.append(':');
185             sb.append(getGnumericPart(blue));
186             return sb.toString();
187         }
188         
189         private String JavaDoc getGnumericPart(byte color)
190         {
191             String JavaDoc s;
192             if (color == 0)
193             {
194                 s = "0";
195             }
196             else
197             {
198                 int c = color & 0xff; //as unsigned
199
c = (c << 8) | c; //pad to 16-bit
200
s = Integer.toHexString(c).toUpperCase();
201                 while (s.length() < 4)
202                 {
203                     s = "0" + s;
204                 }
205             }
206             return s;
207         }
208     }
209 }
210
Popular Tags