Java Code Examples for org.apache.poi.hssf.util.HSSFColor#getIndex()

The following examples show how to use org.apache.poi.hssf.util.HSSFColor#getIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OfficeConverter.java    From BBSSDK-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 单元格背景色转换
 */
private String convertToStardColor(HSSFColor hc) {
	StringBuffer sb = new StringBuffer("");
	if (hc != null) {
		int a = HSSFColor.AUTOMATIC.index;
		int b = hc.getIndex();
		if (a == b) {
			return null;
		}
		sb.append("#");
		for (int i = 0; i < hc.getTriplet().length; i++) {
			String str;
			String strTmp = Integer.toHexString(hc.getTriplet()[i]);
			if (strTmp != null && strTmp.length() < 2) {
				str = "0" + strTmp;
			} else {
				str = strTmp;
			}
			sb.append(str);
		}
	}
	return sb.toString();
}
 
Example 2
Source File: StyleManagerHUtils.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get an HSSFPalette index for a workbook that closely approximates the passed in colour.
 * @param workbook
 * The workbook for which the colour is being sought.
 * @param colour
 * The colour, in the form "rgb(<i>r</i>, <i>g</i>, <i>b</i>)".
 * @return
 * The index into the HSSFPallete for the workbook for a colour that approximates the passed in colour.
 */
private short getHColour( HSSFWorkbook workbook, String colour ) {
	int[] rgbInt = ColorUtil.getRGBs(colour);
	if( rgbInt == null ) {
		return 0;
	}
	
	byte[] rgbByte = new byte[] { (byte)rgbInt[0], (byte)rgbInt[1], (byte)rgbInt[2] };
	HSSFPalette palette = workbook.getCustomPalette();
	
	HSSFColor result = palette.findColor(rgbByte[0], rgbByte[1], rgbByte[2]);
	if( result == null) {
		if( paletteIndex > minPaletteIndex ) {
			--paletteIndex;
			palette.setColorAtIndex(paletteIndex, rgbByte[0], rgbByte[1], rgbByte[2]);
			return paletteIndex;
		} else {
			result = palette.findSimilarColor(rgbByte[0], rgbByte[1], rgbByte[2]);
		}
	}
	return result.getIndex();
}
 
Example 3
Source File: DynamicExcelColorProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
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++;
  }
}
 
Example 4
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected Font getFont(boolean bold, boolean italic, boolean underline, Color c) {
	Short color = null;
	if (c == null) c = Color.BLACK;
	if (c != null) {
		String colorId = Integer.toHexString(c.getRGB());
		color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(c.getRed(), c.getGreen(), c.getBlue());
			color = (clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
	}
	String fontId = (bold ? "b" : "") + (italic ? "i" : "") + (underline ? "u" : "") + (color == null ? "" : color);
	Font font = iFonts.get(fontId);
	if (font == null) {
		font = iWorkbook.createFont();
		font.setBold(bold);
		font.setItalic(italic);
		font.setUnderline(underline ? Font.U_SINGLE : Font.U_NONE);
		font.setColor(color);
		font.setFontHeightInPoints((short)iFontSize);
		font.setFontName(iFontName);
		iFonts.put(fontId, font);
	}
	return font;
}
 
Example 5
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingHeaderStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-header-" + Integer.toHexString(bgColor.getRGB()) + (p.iItalics ? "-italics" : "");
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderTop(BorderStyle.THICK);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setFont(getFont(parent));
        String colorId = Integer.toHexString(bgColor.getRGB());
		Short color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
			color = (clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example 6
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingFooterStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-footer-" + Integer.toHexString(bgColor.getRGB());
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THICK);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setFont(getFont(parent));
        String colorId = Integer.toHexString(bgColor.getRGB());
		Short color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
			color = (clr == null ? IndexedColors.WHITE.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example 7
Source File: XLSPrinter.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected Font getFont(boolean bold, boolean italic, boolean underline, Color c) {
	Short color = null;
	if (c == null) c = Color.BLACK;
	if (c != null) {
		String colorId = Integer.toHexString(c.getRGB());
		color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(c.getRed(), c.getGreen(), c.getBlue());
			color = (clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
	}
	String fontId = (bold ? "b" : "") + (italic ? "i" : "") + (underline ? "u" : "") + (color == null ? "" : color);
	Font font = iFonts.get(fontId);
	if (font == null) {
		font = iWorkbook.createFont();
		font.setBold(bold);
		font.setItalic(italic);
		font.setUnderline(underline ? Font.U_SINGLE : Font.U_NONE);
		font.setColor(color);
		font.setFontHeightInPoints((short)10);
		font.setFontName("Arial");
		iFonts.put(fontId, font);
	}
	return font;
}
 
Example 8
Source File: StaticExcelColorSupport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static short getNearestColor( final Color awtColor, final Map triplets ) {
  if ( awtColor == null ) {
    throw new NullPointerException();
  }

  if ( triplets == null || triplets.isEmpty() ) {
    logger.warn( "Unable to get triplet hashtable" );
    return HSSFColor.BLACK.index;
  }

  short color = HSSFColor.BLACK.index;
  double minDiff = Double.MAX_VALUE;

  // get the color without the alpha chanel
  final float[] hsb = Color.RGBtoHSB( awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue(), null );

  float[] excelHsb = null;
  final Iterator elements = triplets.values().iterator();
  while ( elements.hasNext() ) {
    final HSSFColor crtColor = (HSSFColor) elements.next();
    final short[] rgb = crtColor.getTriplet();
    excelHsb = Color.RGBtoHSB( rgb[0], rgb[1], rgb[2], excelHsb );

    final double weight =
        3.0 * ( Math.min( Math.abs( excelHsb[0] - hsb[0] ), Math.abs( excelHsb[0] - hsb[0] + 1 ) ) )
            + Math.abs( excelHsb[1] - hsb[1] ) + Math.abs( excelHsb[2] - hsb[2] );

    if ( weight < minDiff ) {
      minDiff = weight;
      if ( minDiff == 0 ) {
        // we found the color ...
        return crtColor.getIndex();
      }
      color = crtColor.getIndex();
    }
  }
  return color;
}