}
private static String tripletToHTML(short[] triplet) {
return triplet == null ? null : "#"+ toHex(triplet[0])+ toHex(triplet[1])+ toHex(triplet[2]);
}
public static short rgbToIndex(Book book, String color) {
HSSFPalette palette = ((HSSFWorkbook)book).getCustomPalette();
short red = Short.parseShort(color.substring(1,3), 16); //red
short green = Short.parseShort(color.substring(3,5), 16); //green
short blue = Short.parseShort(color.substring(5), 16); //blue
byte r = (byte)Math.abs((byte)red);
byte g = (byte)Math.abs((byte)green);
byte b = (byte)Math.abs((byte)blue);
HSSFColor pcolor = palette.findColor(r, g, b);
if (pcolor != null) { //find default palette
return pcolor.getIndex();
} else {
final Hashtable<short[], HSSFColor> colors = HSSFColor.getRgbHash();
HSSFColor tcolor = colors.get(new short[] {red, green, blue});
if (tcolor != null)
return tcolor.getIndex();
else {
try {
HSSFColor ncolor = palette.addColor(r, g, b);
return ncolor.getIndex();
} catch (RuntimeException ex) {
//return similar color if can't add new color to palette
/*
* TODO: find a better solution for fix this issue
*
* While there is no space for adding a color into palette
* return similar color cause return inexact color
*/
return palette.findSimilarColor(red, green, blue).getIndex();
}
}
}
}