public HSSFColor setColor(HSSFWorkbook workbook, byte r,byte g, byte b){ HSSFPalette palette = workbook.getCustomPalette(); HSSFColor hssfColor = null; try { hssfColor= palette.findColor(r, g, b); if (hssfColor == null ){ palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b); hssfColor = palette.getColor(HSSFColor.LAVENDER.index); } } catch (Exception e) { logger.error(e); } return hssfColor; }
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; }
private HSSFColor buildHSSFColor(HSSFWorkbook wb,String colorStr){ String[] color=colorStr.split(","); HSSFPalette palette=wb.getCustomPalette(); byte r=BigInteger.valueOf(Integer.valueOf(color[0])).byteValue(); byte g=BigInteger.valueOf(Integer.valueOf(color[1])).byteValue(); byte b=BigInteger.valueOf(Integer.valueOf(color[2])).byteValue(); HSSFColor targetColor=palette.findColor(r,g,b); if(targetColor==null){ palette.setColorAtIndex(HSSFColorPredefined.LAVENDER.getIndex(), r, g,b); targetColor=palette.getColor(HSSFColorPredefined.LAVENDER.getIndex()); } return targetColor; }
/** * 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; }
private HSSFColor buildHSSFColor(HSSFWorkbook wb,String colorStr){ String[] color=colorStr.split(","); HSSFPalette palette=wb.getCustomPalette(); byte r=BigInteger.valueOf(Integer.valueOf(color[0])).byteValue(); byte g=BigInteger.valueOf(Integer.valueOf(color[1])).byteValue(); byte b=BigInteger.valueOf(Integer.valueOf(color[2])).byteValue(); HSSFColor targetColor=palette.findColor(r,g,b); if(targetColor==null){ palette.setColorAtIndex(HSSFColorPredefined.LAVENDER.getIndex(), r, g,b); targetColor=palette.getColor(HSSFColorPredefined.LAVENDER.getIndex()); } return targetColor; }
public short getNearestColor( final Color awtColor ) { if ( lastUsedColor > 64 ) { // we ran out of palette... try to get nearest color then return StaticExcelColorSupport.getNearestColor( awtColor, usedTripplets ); } final HSSFPalette palette = workbook.getCustomPalette(); final HSSFColor hssfColor = palette.findColor( (byte) awtColor.getRed(), (byte) awtColor.getGreen(), (byte) awtColor.getBlue() ); if ( hssfColor != null && hssfColor.getIndex() < lastUsedColor ) { return hssfColor.getIndex(); } else { palette.setColorAtIndex( lastUsedColor, (byte) awtColor.getRed(), (byte) awtColor.getGreen(), (byte) awtColor .getBlue() ); final HSSFColor color = palette.getColor( lastUsedColor ); usedTripplets.put( color.getHexString(), color ); return lastUsedColor++; } }
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(); }
private HSSFFont matchFont( Font font ) { 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 = (font.getStyle() & Font.BOLD) != 0; boolean italic = (font.getStyle() & Font.ITALIC) != 0; HSSFFont hssfFont = workbook.findFont(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0, hssfColor.getIndex(), (short)(font.getSize() * 20), font.getName(), italic, false, (short)0, (byte)0); if (hssfFont == null) { hssfFont = workbook.createFont(); hssfFont.setBoldweight(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0); hssfFont.setColor(hssfColor.getIndex()); hssfFont.setFontHeight((short)(font.getSize() * 20)); hssfFont.setFontName(font.getName()); hssfFont.setItalic(italic); hssfFont.setStrikeout(false); hssfFont.setTypeOffset((short) 0); hssfFont.setUnderline((byte) 0); } return hssfFont; }
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; }
private HSSFFont matchFont( Font font ) { 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 = (font.getStyle() & Font.BOLD) != 0; boolean italic = (font.getStyle() & Font.ITALIC) != 0; HSSFFont hssfFont = workbook.findFont(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0, hssfColor.getIndex(), (short)(font.getSize() * 20), font.getName(), italic, false, (short)0, (byte)0); if (hssfFont == null) { hssfFont = workbook.createFont(); hssfFont.setBoldweight(bold ? HSSFFont.BOLDWEIGHT_BOLD : 0); hssfFont.setColor(hssfColor.getIndex()); hssfFont.setFontHeight((short)(font.getSize() * 20)); hssfFont.setFontName(font.getName()); hssfFont.setItalic(italic); hssfFont.setStrikeout(false); hssfFont.setTypeOffset((short) 0); hssfFont.setUnderline((byte) 0); } return hssfFont; }
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(); }
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(); }