Bug 48274 - HSSFPalette.findSimilarColor treats 0 and 255 as adjacent
Summary: HSSFPalette.findSimilarColor treats 0 and 255 as adjacent
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2009-11-24 07:03 UTC by Jeremy Michelson
Modified: 2009-11-25 03:46 UTC (History)
0 users

patch for HSSFPalette.java and TestHSSFPalette.java (3.75 KB, application/octet-stream)
2009-11-24 07:03 UTC, Jeremy Michelson

Note You need to log in before you can comment on or make changes to this bug.
Description Jeremy Michelson 2009-11-24 07:03:07 UTC
Created attachment 24605 [details]
patch for HSSFPalette.java and TestHSSFPalette.java

HSSFPalette.findSimilarColor considers a color value of 0 to be closer than 255 it is to 127.  Thus, for example,

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFPalette palette = workbook.getCustomPalette();
HSSFColor color = palette.findSimilarColor(0,102,255);
short[] rgb = color.getTriplet();

prints "[255,102,0]".  The input color is not unlike turquoise, but the output color is a light red!

The reason findSimilarColor behaves this way is because it implements the Manhattan distance in byte space.  byte is signed, and thus 255 is really -1, which is adjacent to 0.

A patch is enclosed which first converts the bytes to positive integers so that 255 is maximally far from 0.  Then the above example returns [0,102,204] which is much closer to the original [0,102,255].  The patch also includes additional tests for TestHSSFPalette.
Comment 1 Nick Burch 2009-11-25 03:46:03 UTC
Thanks for this, applied to trunk in r884061, with a few slight tweaks