use of org.apache.poi.hssf.util.HSSFColor in project poi by apache.
the class TestHSSFPalette method testCustomPalette.
/**
* Verifies that a custom palette can be created, saved, and reloaded
*/
@Test
public void testCustomPalette() {
//reading sample xls
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("Simple.xls");
//creating custom palette
HSSFPalette palette = book.getCustomPalette();
palette.setColorAtIndex((short) 0x12, (byte) 101, (byte) 230, (byte) 100);
palette.setColorAtIndex((short) 0x3b, (byte) 0, (byte) 255, (byte) 52);
//writing to disk; reading in and verifying palette
book = HSSFTestDataSamples.writeOutAndReadBack(book);
palette = book.getCustomPalette();
//unmodified
HSSFColor color = palette.getColor(HSSFColorPredefined.CORAL.getIndex());
assertNotNull("Unexpected null in custom palette (unmodified index)", color);
short[] expectedRGB = HSSFColorPredefined.CORAL.getTriplet();
short[] actualRGB = color.getTriplet();
String msg = "Expected palette position to remain unmodified";
assertEquals(msg, expectedRGB[0], actualRGB[0]);
assertEquals(msg, expectedRGB[1], actualRGB[1]);
assertEquals(msg, expectedRGB[2], actualRGB[2]);
color = palette.getColor((short) 0x12);
assertNotNull("Unexpected null in custom palette (modified)", color);
actualRGB = color.getTriplet();
msg = "Expected palette modification to be preserved across save";
assertEquals(msg, (short) 101, actualRGB[0]);
assertEquals(msg, (short) 230, actualRGB[1]);
assertEquals(msg, (short) 100, actualRGB[2]);
}
use of org.apache.poi.hssf.util.HSSFColor in project poi by apache.
the class EscherGraphics method matchFont.
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 ? HSSFFont.BOLDWEIGHT_BOLD : 0, hssfColor.getIndex(), (short) (matchFont.getSize() * 20), matchFont.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) (matchFont.getSize() * 20));
hssfFont.setFontName(matchFont.getName());
hssfFont.setItalic(italic);
hssfFont.setStrikeout(false);
hssfFont.setTypeOffset((short) 0);
hssfFont.setUnderline((byte) 0);
}
return hssfFont;
}
use of org.apache.poi.hssf.util.HSSFColor in project poi by apache.
the class DefaultIndexedColorMap method getDefaultRGB.
/**
* @param index
* @return RGB bytes from HSSF default color by index
*/
public static byte[] getDefaultRGB(int index) {
HSSFColor hssfColor = HSSFColor.getIndexHash().get(index);
if (hssfColor == null)
return null;
short[] rgbShort = hssfColor.getTriplet();
return new byte[] { (byte) rgbShort[0], (byte) rgbShort[1], (byte) rgbShort[2] };
}
Aggregations