diff options
author | Glen Stampoultzis <glens@apache.org> | 2004-04-09 11:45:38 +0000 |
---|---|---|
committer | Glen Stampoultzis <glens@apache.org> | 2004-04-09 11:45:38 +0000 |
commit | b6ea214cc1e42332b7198954c63a783935b4bdf4 (patch) | |
tree | 9ffdd5aea352c9c258ed2df5fa1a4deb4d63792e /src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java | |
parent | 0873a633af2dc09fd9eab9ee1b2d5835e0d7fc2d (diff) | |
download | poi-b6ea214cc1e42332b7198954c63a783935b4bdf4.tar.gz poi-b6ea214cc1e42332b7198954c63a783935b4bdf4.zip |
Ported the drawing stuff from the rel_2_branch. Given the effort this took I'm really really wanting to move to subversion.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353542 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java')
-rw-r--r-- | src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java index 863533d455..d1eb55a08d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPalette.java @@ -110,7 +110,60 @@ public class HSSFPalette } return null; } - + + /** + * Finds the closest matching color in the custom palette. The + * method for finding the distance between the colors is fairly + * primative. + * + * @param red The red component of the color to match. + * @param green The green component of the color to match. + * @param blue The blue component of the color to match. + * @return The closest color or null if there are no custom + * colors currently defined. + */ + public HSSFColor findSimilarColor(byte red, byte green, byte blue) + { + HSSFColor result = null; + int minColorDistance = Integer.MAX_VALUE; + byte[] b = palette.getColor(PaletteRecord.FIRST_COLOR_INDEX); + for (short i = (short) PaletteRecord.FIRST_COLOR_INDEX; b != null; + b = palette.getColor(++i)) + { + int colorDistance = red - b[0] + green - b[1] + blue - b[2]; + if (colorDistance < minColorDistance) + { + result = getColor(i); + } + } + return result; + } + + /** + * Adds a new color into an empty color slot. + * @param red The red component + * @param green The green component + * @param blue The blue component + * + * @return The new custom color. + * + * @throws RuntimeException if there are more more free color indexes. + */ + public HSSFColor addColor( byte red, byte green, byte blue ) + { + byte[] b = palette.getColor(PaletteRecord.FIRST_COLOR_INDEX); + short i; + for (i = (short) PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = palette.getColor(++i)) + { + if (b == null) + { + setColorAtIndex( i, red, green, blue ); + return getColor(i); + } + } + throw new RuntimeException("Could not find free color index"); + } + /** * Sets the color at the given offset * |