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; }
/** * 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 = 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"); }
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; }
palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green ); palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
HSSFPalette palette = wb.getCustomPalette(); palette.setColorAtIndex(HSSFColor.TAN.index, (byte)226, (byte)0, (byte)116); cabecalho.setFillForegroundColor(HSSFColor.TAN.index);
private static short colorIdx = 0x10; private static HSSFColor setColor(byte r, byte g, byte b) { HSSFPalette palette = wb.getCustomPalette(); palette.setColorAtIndex(colorIdx, r, g, b); HSSFColor hssfColor = palette.getColor(colorIdx); colorIdx++; return hssfColor; }
private void replaceColorsPallete(Map<HSSFColor, HSSFColor> colorsReplaceMap, Workbook wb) { if (! (wb instanceof HSSFWorkbook)) { return; } HSSFWorkbook hssfWb = (HSSFWorkbook) wb; final HSSFPalette customPalette = hssfWb.getCustomPalette(); for (Entry<HSSFColor, HSSFColor> e : colorsReplaceMap.entrySet()) { short[] rgb = e.getValue().getTriplet(); customPalette.setColorAtIndex(e.getKey().getIndex(), (byte)rgb[0], (byte)rgb[1], (byte)rgb[2] ); } }
//creating a custom palette for the workbook HSSFPalette palette = wb.getCustomPalette(); //replacing the standard red with freebsd.org red palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green (byte) 0 //RGB blue ); // or creating a new Color HSSFColor myColor = palette.addColor((byte) 153, (byte) 0, (byte) 0); HSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(myColor);
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++; } }
/** * 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 = 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"); }
/** * 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 = 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"); }
/** * 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 = 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"); }
private static void setCustomColor(Workbook workbook, CellStyle style, int r, int g, int b, AtomicInteger colorIndex) { if (workbook instanceof HSSFWorkbook) { HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook; HSSFPalette palette = hssfWorkbook.getCustomPalette(); short index = (short) colorIndex.getAndIncrement(); palette.setColorAtIndex(index, (byte) r, (byte) g, (byte) b); style.setFillForegroundColor(index); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } else { XSSFCellStyle xssfCellStyle = (XSSFCellStyle) style; xssfCellStyle.setFillForegroundColor(new XSSFColor(new Color(r, g, b), new DefaultIndexedColorMap())); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); } }
hssfColor= palette.findSimilarColor(r, g, b); if (hssfColor == null ){ palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b); hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green ); palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
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(); }
palette.setColorAtIndex(HSSFColor.LAVENDER.index, (byte) 204, (byte) 204,