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

6
  • Indexed color? Don't you need a color index table to reference colors to then? Can you not get the table? Aug 7, 2013 at 18:36
  • Honestly, I don't know. I'll see if there's a color table in POI. Aug 7, 2013 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 7, 2013 at 18:40
  • Possible duplicate of stackoverflow.com/questions/11501434/…
    – dkatzel
    Aug 7, 2013 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. Aug 7, 2013 at 18:41

1 Answer 1

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

1
  • Thanks for your answer, now the problem is locating the palette. Aug 7, 2013 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.