Search in sources :

Example 16 with HSSFColor

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]);
}
Also used : HSSFColor(org.apache.poi.hssf.util.HSSFColor) Test(org.junit.Test)

Example 17 with HSSFColor

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;
}
Also used : HSSFColor(org.apache.poi.hssf.util.HSSFColor)

Example 18 with HSSFColor

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] };
}
Also used : HSSFColor(org.apache.poi.hssf.util.HSSFColor)

Aggregations

HSSFColor (org.apache.poi.hssf.util.HSSFColor)18 Test (org.junit.Test)4 Color (java.awt.Color)2 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)2 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)2 Cell (org.apache.poi.ss.usermodel.Cell)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 ExcelColDVO (com.kyj.fx.voeditor.visual.excels.base.ExcelColDVO)1 ExcelDataDVO (com.kyj.fx.voeditor.visual.excels.base.ExcelDataDVO)1 ExcelSVO (com.kyj.fx.voeditor.visual.excels.base.ExcelSVO)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Triplet (org.apache.poi.hwpf.converter.FontReplacer.Triplet)1 CellDateFormatter (org.apache.poi.ss.format.CellDateFormatter)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Color (org.apache.poi.ss.usermodel.Color)1 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1