Search in sources :

Example 1 with XSSFColor

use of org.apache.poi.xssf.usermodel.XSSFColor in project poi by apache.

the class ThemesTable method inheritFromThemeAsRequired.

/**
     * If the colour is based on a theme, then inherit
     *  information (currently just colours) from it as
     *  required.
     */
public void inheritFromThemeAsRequired(XSSFColor color) {
    if (color == null) {
        // Nothing for us to do
        return;
    }
    if (!color.getCTColor().isSetTheme()) {
        // No theme set, nothing to do
        return;
    }
    // Get the theme colour
    XSSFColor themeColor = getThemeColor(color.getTheme());
    // Set the raw colour, not the adjusted one
    // Do a raw set, no adjusting at the XSSFColor layer either
    color.getCTColor().setRgb(themeColor.getCTColor().getRgb());
// All done
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor)

Example 2 with XSSFColor

use of org.apache.poi.xssf.usermodel.XSSFColor in project poi by apache.

the class ThemesTable method getThemeColor.

/**
     * Convert a theme "index" (as used by fonts etc) into a color.
     * @param idx A theme "index"
     * @return The mapped XSSFColor, or null if not mapped.
     */
public XSSFColor getThemeColor(int idx) {
    // Theme color references are NOT positional indices into the color scheme,
    // i.e. these keys are NOT the same as the order in which theme colors appear
    // in theme1.xml. They are keys to a mapped color.
    CTColorScheme colorScheme = theme.getTheme().getThemeElements().getClrScheme();
    CTColor ctColor;
    switch(ThemeElement.byId(idx)) {
        case LT1:
            ctColor = colorScheme.getLt1();
            break;
        case DK1:
            ctColor = colorScheme.getDk1();
            break;
        case LT2:
            ctColor = colorScheme.getLt2();
            break;
        case DK2:
            ctColor = colorScheme.getDk2();
            break;
        case ACCENT1:
            ctColor = colorScheme.getAccent1();
            break;
        case ACCENT2:
            ctColor = colorScheme.getAccent2();
            break;
        case ACCENT3:
            ctColor = colorScheme.getAccent3();
            break;
        case ACCENT4:
            ctColor = colorScheme.getAccent4();
            break;
        case ACCENT5:
            ctColor = colorScheme.getAccent5();
            break;
        case ACCENT6:
            ctColor = colorScheme.getAccent6();
            break;
        case HLINK:
            ctColor = colorScheme.getHlink();
            break;
        case FOLHLINK:
            ctColor = colorScheme.getFolHlink();
            break;
        default:
            return null;
    }
    byte[] rgb = null;
    if (ctColor.isSetSrgbClr()) {
        // Color is a regular one
        rgb = ctColor.getSrgbClr().getVal();
    } else if (ctColor.isSetSysClr()) {
        // Color is a tint of white or black
        rgb = ctColor.getSysClr().getLastClr();
    } else {
        return null;
    }
    return new XSSFColor(rgb, colorMap);
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) CTColorScheme(org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor)

Example 3 with XSSFColor

use of org.apache.poi.xssf.usermodel.XSSFColor in project poi by apache.

the class TestThemesTable method testThemesTableColors.

@Test
public void testThemesTableColors() throws Exception {
    // Load our two test workbooks
    XSSFWorkbook simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    XSSFWorkbook complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Save and re-load them, to check for stability across that
    XSSFWorkbook simpleRS = XSSFTestDataSamples.writeOutAndReadBack(simple);
    XSSFWorkbook complexRS = XSSFTestDataSamples.writeOutAndReadBack(complex);
    // Fetch fresh copies to test with
    simple = XSSFTestDataSamples.openSampleWorkbook(testFileSimple);
    complex = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    // Files and descriptions
    Map<String, XSSFWorkbook> workbooks = new LinkedHashMap<String, XSSFWorkbook>();
    workbooks.put(testFileSimple, simple);
    workbooks.put("Re-Saved_" + testFileSimple, simpleRS);
    workbooks.put(testFileComplex, complex);
    workbooks.put("Re-Saved_" + testFileComplex, complexRS);
    // Sanity check
    assertEquals(rgbExpected.length, rgbExpected.length);
    // For offline testing
    boolean createFiles = false;
    //  for the theme-applied cells in Column A are correct
    for (String whatWorkbook : workbooks.keySet()) {
        XSSFWorkbook workbook = workbooks.get(whatWorkbook);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int startRN = 0;
        if (whatWorkbook.endsWith(testFileComplex))
            startRN++;
        for (int rn = startRN; rn < rgbExpected.length + startRN; rn++) {
            XSSFRow row = sheet.getRow(rn);
            assertNotNull("Missing row " + rn + " in " + whatWorkbook, row);
            String ref = (new CellReference(rn, 0)).formatAsString();
            XSSFCell cell = row.getCell(0);
            assertNotNull("Missing cell " + ref + " in " + whatWorkbook, cell);
            int expectedThemeIdx = rn - startRN;
            ThemeElement themeElem = ThemeElement.byId(expectedThemeIdx);
            assertEquals("Wrong theme at " + ref + " in " + whatWorkbook, themeElem.name.toLowerCase(Locale.ROOT), cell.getStringCellValue());
            // Fonts are theme-based in their colours
            XSSFFont font = cell.getCellStyle().getFont();
            CTColor ctColor = font.getCTFont().getColorArray(0);
            assertNotNull(ctColor);
            assertEquals(true, ctColor.isSetTheme());
            assertEquals(themeElem.idx, ctColor.getTheme());
            // Get the colour, via the theme
            XSSFColor color = font.getXSSFColor();
            // Theme colours aren't tinted
            assertEquals(false, color.hasTint());
            // Check the RGB part (no tint)
            assertEquals("Wrong theme colour " + themeElem.name + " on " + whatWorkbook, rgbExpected[expectedThemeIdx], Hex.encodeHexString(color.getRGB()));
            long themeIdx = font.getCTFont().getColorArray(0).getTheme();
            assertEquals("Wrong theme index " + expectedThemeIdx + " on " + whatWorkbook, expectedThemeIdx, themeIdx);
            if (createFiles) {
                XSSFCellStyle cs = row.getSheet().getWorkbook().createCellStyle();
                cs.setFillForegroundColor(color);
                cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
                row.createCell(1).setCellStyle(cs);
            }
        }
        if (createFiles) {
            FileOutputStream fos = new FileOutputStream("Generated_" + whatWorkbook);
            workbook.write(fos);
            fos.close();
        }
    }
}
Also used : CellReference(org.apache.poi.ss.util.CellReference) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor) LinkedHashMap(java.util.LinkedHashMap) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 4 with XSSFColor

use of org.apache.poi.xssf.usermodel.XSSFColor in project poi by apache.

the class TestThemesTable method themedAndNonThemedColours.

/**
     * Ensure that, for a file with themes, we can correctly
     *  read both the themed and non-themed colours back.
     * Column A = Theme Foreground
     * Column B = Theme Foreground
     * Column C = Explicit Colour Foreground
     * Column E = Explicit Colour Background, Black Foreground
     * Column G = Conditional Formatting Backgrounds
     * 
     * Note - Grey Row has an odd way of doing the styling... 
     */
@Test
public void themedAndNonThemedColours() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook(testFileComplex);
    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFCellStyle style;
    XSSFColor color;
    XSSFCell cell;
    String[] names = { "White", "Black", "Grey", "Dark Blue", "Blue", "Red", "Green" };
    String[] explicitFHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060", "FF0070C0", "FFFF0000", "FF00B050" };
    String[] explicitBHexes = { "FFFFFFFF", "FF000000", "FFC0C0C0", "FF002060", "FF0000FF", "FFFF0000", "FF00FF00" };
    assertEquals(7, names.length);
    // Check the non-CF colours in Columns A, B, C and E
    for (int rn = 1; rn < 8; rn++) {
        int idx = rn - 1;
        XSSFRow row = sheet.getRow(rn);
        assertNotNull("Missing row " + rn, row);
        // Theme cells come first
        XSSFCell themeCell = row.getCell(0);
        ThemeElement themeElem = ThemeElement.byId(idx);
        assertCellContents(themeElem.name, themeCell);
        // Sanity check names
        assertCellContents(names[idx], row.getCell(1));
        assertCellContents(names[idx], row.getCell(2));
        assertCellContents(names[idx], row.getCell(4));
        // Check the colours
        //  A: Theme Based, Foreground
        style = themeCell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        assertEquals(idx, color.getTheme());
        assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
        //  B: Theme Based, Foreground
        cell = row.getCell(1);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        if (idx != 2) {
            assertEquals(idx, color.getTheme());
            assertEquals(rgbExpected[idx], Hex.encodeHexString(color.getRGB()));
        } else {
            assertEquals(1, color.getTheme());
            assertEquals(0.50, color.getTint(), 0.001);
        }
        //  C: Explicit, Foreground
        cell = row.getCell(2);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(false, color.isThemed());
        assertEquals(explicitFHexes[idx], color.getARGBHex());
        // E: Explicit Background, Foreground all Black
        cell = row.getCell(4);
        style = cell.getCellStyle();
        color = style.getFont().getXSSFColor();
        assertEquals(true, color.isThemed());
        assertEquals("FF000000", color.getARGBHex());
        color = style.getFillForegroundXSSFColor();
        assertEquals(false, color.isThemed());
        assertEquals(explicitBHexes[idx], color.getARGBHex());
        color = style.getFillBackgroundColorColor();
        assertEquals(false, color.isThemed());
        assertEquals(null, color.getARGBHex());
    }
// Check the CF colours
// TODO
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) ThemeElement(org.apache.poi.xssf.model.ThemesTable.ThemeElement) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Example 5 with XSSFColor

use of org.apache.poi.xssf.usermodel.XSSFColor in project poi by apache.

the class TestXSSFCellFill method testColorFromTheme.

@Test
public void testColorFromTheme() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
    XSSFCell cellWithThemeColor = wb.getSheetAt(0).getRow(10).getCell(0);
    //color RGB will be extracted from theme
    XSSFColor foregroundColor = cellWithThemeColor.getCellStyle().getFillForegroundXSSFColor();
    byte[] rgb = foregroundColor.getRGB();
    byte[] rgbWithTint = foregroundColor.getRGBWithTint();
    // Dk2
    assertEquals(rgb[0], 31);
    assertEquals(rgb[1], 73);
    assertEquals(rgb[2], 125);
    // Dk2, lighter 40% (tint is about 0.39998)
    // 31 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 120.59552 => 120 (byte)
    // 73 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 145.79636 => -111 (byte)
    // 125 * (1.0 - 0.39998) + (255 - 255 * (1.0 - 0.39998)) = 176.99740 => -80 (byte)
    assertEquals(rgbWithTint[0], 120);
    assertEquals(rgbWithTint[1], -111);
    assertEquals(rgbWithTint[2], -80);
    wb.close();
}
Also used : XSSFColor(org.apache.poi.xssf.usermodel.XSSFColor) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) Test(org.junit.Test)

Aggregations

XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)26 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)20 Cell (org.apache.poi.ss.usermodel.Cell)17 Row (org.apache.poi.ss.usermodel.Row)17 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)17 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)12 List (java.util.List)6 KpiVO (com.netsteadfast.greenstep.vo.KpiVO)5 DateRangeScoreVO (com.netsteadfast.greenstep.vo.DateRangeScoreVO)4 ObjectiveVO (com.netsteadfast.greenstep.vo.ObjectiveVO)4 PerspectiveVO (com.netsteadfast.greenstep.vo.PerspectiveVO)4 Map (java.util.Map)4 BufferedImage (java.awt.image.BufferedImage)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 Test (org.junit.Test)3 CTColor (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)3 Color (java.awt.Color)2 ThemeElement (org.apache.poi.xssf.model.ThemesTable.ThemeElement)2