1 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 30 public class HSSFPalette 31 { 32 private PaletteRecord palette; 33 34 protected HSSFPalette(PaletteRecord palette) 35 { 36 this.palette = palette; 37 } 38 39 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 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 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 113 public void setColorAtIndex(short index, byte red, byte green, byte blue) 114 { 115 palette.setColor(index, red, green, blue); 116 } 117 118 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 ("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 getHexString() 179 { 180 StringBuffer sb = new StringBuffer (); 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 getGnumericPart(byte color) 190 { 191 String s; 192 if (color == 0) 193 { 194 s = "0"; 195 } 196 else 197 { 198 int c = color & 0xff; c = (c << 8) | c; 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 |