1

I cannot find a conversion routine for converting 8-bit indexed color to RGB. For some background details, I am using POI to read an xlsx file and one of the cells has a background color indexed as value 64. When I attempt to create a PdfPCell in iText with this value for the background BaseColor, I get a Navy Blue and the correct color should be Black. So I need a routine that will convert the value of 64 to rgb(0, 0, 0).

This is the code that sets the background to Navy Blue

short c = ((XSSFColor) color).getIndexed();
BaseColor base = new BaseColor(c);

I found a similar question here on SO but the "packed" routine failed with "Color value outside range 0-255".

short packed = ((XSSFColor) color).getIndexed();
log.debug("Indexed {}", packed);
int r = (packed >> 5) * 32;
int g = ((packed >> 2) << 3) * 32;
int b = (packed << 6) * 64;
BaseColor base = new BaseColor(r, g, b);

Update 1: It seems that there is a Palette somewhere in the document, in my case an XSSFPalette. Once I find the answer I'll update it here.

Update 2: XSSFWorkbook doesn't provide access to the palette, hence my follow question: Access to the color palette in an XSSFWorkbook

Community
  • 1
  • 1
Paul Gregoire
  • 9,715
  • 11
  • 67
  • 131
  • Indexed color? Don't you need a color index table to reference colors to then? Can you not get the table? – William Morrison Aug 07 '13 at 18:36
  • Honestly, I don't know. I'll see if there's a color table in POI. – Paul Gregoire Aug 07 '13 at 18:38
  • Your indexed color can be anything, can't it? Isn't the idea of this scheme to reduce memory usage by storing the actual colors in a pallette? Or am I missing something? 64 could be black on one image, and navy blue on the next ... ? – John Aug 07 '13 at 18:40
  • Possible duplicate of http://stackoverflow.com/questions/11501434/apache-poi-getting-exact-font-color-from-excel-in-java – dkatzel Aug 07 '13 at 18:41
  • I don't either haha. That'd be the right way to go about it. Indexed colors usually refer to a data structure which stores the actual color. – William Morrison Aug 07 '13 at 18:41
  • @dkatzel similar but not a dupe; no rgb nor triplet is returned, both are null. Index color value is only non null return – Paul Gregoire Aug 07 '13 at 18:43

1 Answers1

3

There isn't a mathematical relationship between color index and RGB values. It's a lookup.

Eight-bit indexed color means that each pixel's color is represented by the number 0-255. What those colors actually are depends on your pallette (just like a painter would use!) The eight bits, therefore, allow you to have 256 separate colors in your picture.

If your image displays in color, then you have a pallette somewhere that will tell you what index corresponds to what RGB triplet.

http://en.wikipedia.org/wiki/Indexed_color

John
  • 15,990
  • 10
  • 70
  • 110