Java Examples for org.apache.poi.ss.usermodel.Color

The following java examples will help you to understand the usage of org.apache.poi.ss.usermodel.Color. These source code samples are taken from different open source projects.

Example 1
Project: axebomber-master  File: CellImpl.java View source code
public java.awt.Color getColor() {
    java.awt.Color awtColor = null;
    CellStyle style = cell.getCellStyle();
    Color color = style.getFillForegroundColorColor();
    if (color instanceof HSSFColor) {
        short[] rgb = ((HSSFColor) color).getTriplet();
        if (((HSSFColor) color).getIndex() == IndexedColors.AUTOMATIC.getIndex())
            rgb[0] = rgb[1] = rgb[2] = 255;
        awtColor = new java.awt.Color(rgb[0], rgb[1], rgb[2]);
    } else if (color instanceof XSSFColor) {
        byte[] rgb = ((XSSFColor) color).getRgb();
        if (((XSSFColor) color).isAuto())
            rgb[0] = rgb[1] = rgb[2] = (byte) 0xFF;
        awtColor = new java.awt.Color(rgb[0], rgb[1], rgb[2], rgb[3]);
    }
    return awtColor;
}
Example 2
Project: metamodel-master  File: ExcelUtils.java View source code
public static Style getCellStyle(Workbook workbook, Cell cell) {
    if (cell == null) {
        return Style.NO_STYLE;
    }
    final CellStyle cellStyle = cell.getCellStyle();
    final short fontIndex = cellStyle.getFontIndex();
    final Font font = workbook.getFontAt(fontIndex);
    final StyleBuilder styleBuilder = new StyleBuilder();
    // Font bold, italic, underline
    if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
        styleBuilder.bold();
    }
    if (font.getItalic()) {
        styleBuilder.italic();
    }
    if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
        styleBuilder.underline();
    }
    // Font size
    final Font stdFont = workbook.getFontAt((short) 0);
    final short fontSize = font.getFontHeightInPoints();
    if (stdFont.getFontHeightInPoints() != fontSize) {
        styleBuilder.fontSize(fontSize, SizeUnit.PT);
    }
    // Font color
    final short colorIndex = font.getColor();
    if (font instanceof HSSFFont) {
        if (colorIndex != HSSFFont.COLOR_NORMAL) {
            final HSSFWorkbook wb = (HSSFWorkbook) workbook;
            HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
            if (color != null) {
                short[] triplet = color.getTriplet();
                styleBuilder.foreground(triplet);
            }
        }
    } else if (font instanceof XSSFFont) {
        XSSFFont xssfFont = (XSSFFont) font;
        XSSFColor color = xssfFont.getXSSFColor();
        if (color != null) {
            String argbHex = color.getARGBHex();
            if (argbHex != null) {
                styleBuilder.foreground(argbHex.substring(2));
            }
        }
    } else {
        throw new IllegalStateException("Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
    }
    // Background color
    if (cellStyle.getFillPattern() == 1) {
        Color color = cellStyle.getFillForegroundColorColor();
        if (color instanceof HSSFColor) {
            short[] triplet = ((HSSFColor) color).getTriplet();
            if (triplet != null) {
                styleBuilder.background(triplet);
            }
        } else if (color instanceof XSSFColor) {
            String argb = ((XSSFColor) color).getARGBHex();
            if (argb != null) {
                styleBuilder.background(argb.substring(2));
            }
        } else {
            throw new IllegalStateException("Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
        }
    }
    // alignment
    switch(cellStyle.getAlignment()) {
        case CellStyle.ALIGN_LEFT:
            styleBuilder.leftAligned();
            break;
        case CellStyle.ALIGN_RIGHT:
            styleBuilder.rightAligned();
            break;
        case CellStyle.ALIGN_CENTER:
            styleBuilder.centerAligned();
            break;
        case CellStyle.ALIGN_JUSTIFY:
            styleBuilder.justifyAligned();
            break;
    }
    return styleBuilder.create();
}
Example 3
Project: aorra-master  File: SpreadsheetDataSource.java View source code
@Override
public java.awt.Color asColor() {
    for (Color c : Lists.newArrayList(cell.getCellStyle().getFillForegroundColorColor(), cell.getCellStyle().getFillBackgroundColorColor())) {
        if (c instanceof HSSFColor && (((HSSFColor) c).getTriplet() != null)) {
            final short[] rgb = ((HSSFColor) c).getTriplet();
            return new java.awt.Color(rgb[0], rgb[1], rgb[2]);
        }
        if (c instanceof XSSFColor && (((XSSFColor) c).getRgb() != null)) {
            final byte[] rgb = ((XSSFColor) c).getRgb();
            // Convert bytes to unsigned integers
            return new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF);
        }
    }
    return null;
}
Example 4
Project: poi-master  File: TestXSSFConditionalFormatting.java View source code
@Override
protected void assertColour(String hexExpected, Color actual) {
    assertNotNull("Colour must be given", actual);
    XSSFColor colour = (XSSFColor) actual;
    if (hexExpected.length() == 8) {
        assertEquals(hexExpected, colour.getARGBHex());
    } else {
        assertEquals(hexExpected, colour.getARGBHex().substring(2));
    }
}