HSSFWorkbook hwb = new HSSFWorkbook(); HSSFPalette palette = hwb.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(255, 0, 0); // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here style.setFillForegroundColor(palIndex);
/** * 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) { return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue)); } /**
private HSSFFont matchFont( Font matchFont ) { HSSFColor hssfColor = workbook.getCustomPalette() .findColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue()); if (hssfColor == null) hssfColor = workbook.getCustomPalette().findSimilarColor((byte)foreground.getRed(), (byte)foreground.getGreen(), (byte)foreground.getBlue()); boolean bold = (matchFont.getStyle() & Font.BOLD) != 0; boolean italic = (matchFont.getStyle() & Font.ITALIC) != 0; HSSFFont hssfFont = workbook.findFont(bold, hssfColor.getIndex(), (short)(matchFont.getSize() * 20), matchFont.getName(), italic, false, (short)0, (byte)0); if (hssfFont == null) { hssfFont = workbook.createFont(); hssfFont.setBold(bold); hssfFont.setColor(hssfColor.getIndex()); hssfFont.setFontHeight((short)(matchFont.getSize() * 20)); hssfFont.setFontName(matchFont.getName()); hssfFont.setItalic(italic); hssfFont.setStrikeout(false); hssfFont.setTypeOffset((short) 0); hssfFont.setUnderline((byte) 0); } return hssfFont; }
if (facetBackground != null) { color = Color.decode(facetBackground); HSSFColor backgroundColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFCellStyle) facetStyle).setFillForegroundColor(backgroundColor.getIndex()); facetStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); if (facetFontColor != null) { color = Color.decode(facetFontColor); HSSFColor facetColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFFont) facetFont).setColor(facetColor.getIndex());
HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); Color color = Color.decode(cellFontColor); HSSFColor cellColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFFont) cellFont).setColor(cellColor.getIndex());
private static HSSFColor getSimilarColor(HSSFWorkbook workbook, Color color) { HSSFPalette palette = workbook.getCustomPalette(); HSSFColor result = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); return result == null ? HSSFColor.HSSFColorPredefined.AUTOMATIC.getColor() : result; }
private HSSFFont setFontColor(HSSFWorkbook wb, HSSFFont font, String colorCode) { HSSFPalette palette = wb.getCustomPalette(); // if format is rgb(x,y,z) form, retrieve the 3 numbers within the // parentheses colorCode = colorCode.trim(); if (colorCode.toLowerCase().startsWith("rgb")) { String rgbNumString = colorCode.substring(3, colorCode.length()).trim(); rgbNumString = rgbNumString.substring(1, rgbNumString.length()-1).trim(); String[] rgbNums = StringUtils.split(rgbNumString, ","); int[] rgbInts = { Integer.parseInt(rgbNums[0].trim()), Integer.parseInt(rgbNums[1].trim()), Integer.parseInt(rgbNums[2].trim()) }; HSSFColor color = palette.findSimilarColor(rgbInts[0], rgbInts[1], rgbInts[2]); short palIndex = color.getIndex(); font.setColor(palIndex); return font; } return font; }
/** * parse color * @param workBook work book * @param color string color * @return HSSFColor */ public static HSSFColor parseColor(HSSFWorkbook workBook, String color) { HSSFColor poiColor = null; if (StringUtils.isNotBlank(color)) { Color awtColor = Color.decode(color); if (awtColor != null) { int r = awtColor.getRed(); int g = awtColor.getGreen(); int b = awtColor.getBlue(); HSSFPalette palette = workBook.getCustomPalette(); poiColor = palette.findColor((byte) r, (byte) g, (byte) b); if (poiColor == null) { poiColor = palette.findSimilarColor(r, g, b); } } } return poiColor; }
/** * parse color * * @param workBook work book * @param color string color * @return HSSFColor */ public static HSSFColor parseColor(HSSFWorkbook workBook, String color) { HSSFColor poiColor = null; if (StringUtils.isNotBlank(color)) { Color awtColor = getAwtColor(color); if (awtColor != null) { int r = awtColor.getRed(); int g = awtColor.getGreen(); int b = awtColor.getBlue(); HSSFPalette palette = workBook.getCustomPalette(); poiColor = palette.findColor((byte) r, (byte) g, (byte) b); if (poiColor == null) { poiColor = palette.findSimilarColor(r, g, b); } } } return poiColor; }
/** * 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) { return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue)); } /**
/** * 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) { return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue)); } /**
/** * 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) { return findSimilarColor(unsignedInt(red), unsignedInt(green), unsignedInt(blue)); } /**
private static short getOrAddColorIndex(short[] rgb, HSSFWorkbook wb) { HSSFPalette palette = wb.getCustomPalette(); HSSFColor color = palette.findColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); if (color == null) { try { color = palette.addColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); } catch (RuntimeException e) { // Could not find free color index color = palette.findSimilarColor(rgb[0], rgb[1], rgb[2]); } } return color.getIndex(); }
public short getColorIndex(Color color) { Workbook workbook = getWorkbook(true); if (workbook instanceof HSSFWorkbook) { HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette(); byte r = toRgb(color.getRed()); byte g = toRgb(color.getGreen()); byte b = toRgb(color.getBlue()); HSSFColor index = palette.findColor(r, g, b); if (index == null) { index = palette.findSimilarColor(r, g, b); } return index.getIndex(); } throw new IllegalStateException("Unexpected workbook type: " + workbook.getClass()); }
public short getColorIndex(Color color) { Workbook workbook = getWorkbook(true); if (workbook instanceof HSSFWorkbook) { HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette(); byte r = toRgb(color.getRed()); byte g = toRgb(color.getGreen()); byte b = toRgb(color.getBlue()); HSSFColor index = palette.findColor(r, g, b); if (index == null) { index = palette.findSimilarColor(r, g, b); } return index.getIndex(); } throw new IllegalStateException("Unexpected workbook type: " + workbook.getClass()); }
public short getColorIndex(Color color) { Workbook workbook = getWorkbook(true); if (workbook instanceof HSSFWorkbook) { HSSFPalette palette = ((HSSFWorkbook) workbook).getCustomPalette(); byte r = toRgb(color.getRed()); byte g = toRgb(color.getGreen()); byte b = toRgb(color.getBlue()); HSSFColor index = palette.findColor(r, g, b); if (index == null) { index = palette.findSimilarColor(r, g, b); } return index.getIndex(); } throw new IllegalStateException("Unexpected workbook type: " + workbook.getClass()); }
private Short findIndexedColor(short[] rgb) { HSSFPalette palette = ((HSSFWorkbook) getSheet().getWorkbook()).getCustomPalette(); HSSFColor color = palette.findColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); if (color == null) { Set<Short> usedColors = sheetSource.getWorkbookSource().getWorkbookColors(); short fromIndex = PaletteRecord.FIRST_COLOR_INDEX; short toIndex = (short) (PaletteRecord.STANDARD_PALETTE_SIZE + fromIndex); for (short colorIndex = fromIndex; colorIndex < toIndex; colorIndex++) { if (!usedColors.contains(colorIndex)) { palette.setColorAtIndex(colorIndex, (byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); color = palette.getColor(colorIndex); usedColors.add(colorIndex); break; } } if (color == null) { color = palette.findSimilarColor((byte) rgb[0], (byte) rgb[1], (byte) rgb[2]); } } return color == null ? null : color.getIndex(); }
private HSSFCellStyle createHeaderStyle(HSSFWorkbook myWorkBook) { Font headerFont = myWorkBook.createFont(); headerFont.setColor(Font.COLOR_NORMAL); HSSFPalette palette = myWorkBook.getCustomPalette(); HSSFColor headerColor = palette.findSimilarColor((byte) 149, (byte) 179, (byte) 215); HSSFCellStyle headerStyle = myWorkBook.createCellStyle(); headerStyle.setFillForegroundColor(headerColor.getIndex()); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); headerStyle.setFont(headerFont); headerStyle.setWrapText(true); return headerStyle; }
static private short getColorIndex(short[] rgbColor, Workbook workbook) { if (rgbColor != null) { HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; HSSFPalette palette = hssfWorkbook.getCustomPalette(); HSSFColor hssfColor = palette.findColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); if (hssfColor == null ) { try { hssfColor = palette.addColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); } catch (Exception e) { HSSFColor similarColor = palette.findSimilarColor((int)rgbColor[0], (int)rgbColor[1], (int)rgbColor[2]); palette.setColorAtIndex(similarColor.getIndex(), (byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); hssfColor = palette.getColor(similarColor.getIndex()); } } return hssfColor.getIndex(); } return HSSFColor.WHITE.index; }
static private short getColorIndex(short[] rgbColor, Workbook workbook) { if (rgbColor != null) { HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; HSSFPalette palette = hssfWorkbook.getCustomPalette(); HSSFColor hssfColor = palette.findColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); if (hssfColor == null ) { try { hssfColor = palette.addColor((byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); } catch (Exception e) { HSSFColor similarColor = palette.findSimilarColor((int)rgbColor[0], (int)rgbColor[1], (int)rgbColor[2]); palette.setColorAtIndex(similarColor.getIndex(), (byte) rgbColor[0], (byte) rgbColor[1], (byte) rgbColor[2]); hssfColor = palette.getColor(similarColor.getIndex()); } } return hssfColor.getIndex(); } return HSSFColor.HSSFColorPredefined.WHITE.getIndex(); }