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

The following examples show how to use org.apache.poi.hssf.util.HSSFColor#getTriplet() . 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: JavaToExcel.java    From hy.common.report with Apache License 2.0 6 votes vote down vote up
/**
 * 创建颜色。
 * 
 * 写本方法的原因是:从2003版本的模板中复制单元格颜色时,会出现颜色失真的问题。
 * 
 * 但最终也没有解决。因为:当单元格的颜色设置为非标准颜色时,就会失真,但设置为标准颜色时,是正常的。
 * 
 * 也因为此,本方法与 i_ToCellStyle.setFillBackgroundColor(i_FromCellStyle.getFillBackgroundColor()); 的效果是相同的。
 * 
 * 本方法作为研究使用而保留下来,但不没有使用价值。
 * 
 * @author      ZhengWei(HY)
 * @createDate  2017-03-21
 * @version     v1.0
 *
 * @param i_FromColor
 * @param i_DataWorkbook
 * @return
 */
@Deprecated
public final static HSSFColor createColor(HSSFColor i_FromColor ,HSSFWorkbook i_DataWorkbook)
{
    short [] v_RGBHex    = i_FromColor.getTriplet();
    byte     v_ByteRed   = (byte)v_RGBHex[0];
    byte     v_ByteGreen = (byte)v_RGBHex[1];
    byte     v_ByteBlue  = (byte)v_RGBHex[2];
    
    HSSFPalette v_Palette   = i_DataWorkbook.getCustomPalette();
    HSSFColor   v_DataColor = v_Palette.findColor(v_ByteRed ,v_ByteGreen ,v_ByteBlue);
    
    if ( v_DataColor == null )
    {
        v_Palette.setColorAtIndex(i_FromColor.getIndex() ,v_ByteRed ,v_ByteGreen ,v_ByteBlue);
        
        return v_Palette.getColor(i_FromColor.getIndex());
    }
    
    return  v_DataColor;
}
 
Example 3
Source File: StylerHelper.java    From autopoi with Apache License 2.0 5 votes vote down vote up
private void styleColor(Formatter out, String attr, short index) {
	HSSFColor color = colors.getColor(index);
	if (index == HSSF_AUTO.getIndex() || color == null) {
		out.format("  /* %s: index = %d */%n", attr, index);
	} else {
		short[] rgb = color.getTriplet();
		out.format("  %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], index);
	}
}
 
Example 4
Source File: StylerHelper.java    From autopoi with Apache License 2.0 5 votes vote down vote up
public void styleColor(Formatter out, String attr, Color color) {
	if (color == null) {
		return;
	}
	HSSFColor hSSFColor = (HSSFColor) color;
	short[] rgb = hSSFColor.getTriplet();
	out.format("  %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]);
}
 
Example 5
Source File: StylerHelper.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
private void styleColor(Formatter out, String attr, short index) {
	HSSFColor color = colors.getColor(index);
	if (index == HSSF_AUTO.getIndex() || color == null) {
		out.format("  /* %s: index = %d */%n", attr, index);
	} else {
		short[] rgb = color.getTriplet();
		out.format("  %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2], index);
	}
}
 
Example 6
Source File: StylerHelper.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
public void styleColor(Formatter out, String attr, Color color) {
	if (color == null) {
		return;
	}
	HSSFColor hSSFColor = (HSSFColor) color;
	short[] rgb = hSSFColor.getTriplet();
	out.format("  %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]);
}
 
Example 7
Source File: HSSFExtendedColor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected byte[] getIndexedRGB() {
    if (isIndexed() && getIndex() > 0) {
        int indexNum = getIndex();
        HSSFColor indexed = HSSFColor.getIndexHash().get(indexNum);
        if (indexed != null) {
            byte[] rgb = new byte[3];
            rgb[0] = (byte) indexed.getTriplet()[0];
            rgb[1] = (byte) indexed.getTriplet()[1];
            rgb[2] = (byte) indexed.getTriplet()[2];
            return rgb;
        }
    } // else
    return null;
}
 
Example 8
Source File: StylerHelper.java    From easypoi with Apache License 2.0 5 votes vote down vote up
private void styleColor(Formatter out, String attr, short index) {
    HSSFColor color = colors.getColor(index);
    if (index == HSSF_AUTO.getIndex() || color == null) {
        out.format("  /* %s: index = %d */%n", attr, index);
    } else {
        short[] rgb = color.getTriplet();
        out.format("  %s: #%02x%02x%02x; /* index = %d */%n", attr, rgb[0], rgb[1], rgb[2],
            index);
    }
}
 
Example 9
Source File: StylerHelper.java    From easypoi with Apache License 2.0 5 votes vote down vote up
public void styleColor(Formatter out, String attr, Color color) {
    if (color == null) {
        return;
    }
    HSSFColor hSSFColor = (HSSFColor) color;
    short[] rgb = hSSFColor.getTriplet();
    out.format("  %s: #%02x%02x%02x; %n", attr, rgb[0], rgb[1], rgb[2]);
}
 
Example 10
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;
}