Search in sources :

Example 1 with PresetColor

use of org.apache.poi.sl.usermodel.PresetColor in project poi by apache.

the class TestXSLFColor method testPresetColor.

@Test
public void testPresetColor() {
    CTColor xml = CTColor.Factory.newInstance();
    xml.addNewPrstClr().setVal(STPresetColorVal.AQUAMARINE);
    XSLFColor color = new XSLFColor(xml, null, null);
    assertEquals(new Color(127, 255, 212), color.getColor());
    for (PresetColor pc : PresetColor.values()) {
        if (pc.ooxmlId == null)
            continue;
        xml = CTColor.Factory.newInstance();
        STPresetColorVal.Enum preVal = STPresetColorVal.Enum.forString(pc.ooxmlId);
        STSystemColorVal.Enum sysVal = STSystemColorVal.Enum.forString(pc.ooxmlId);
        assertTrue(pc.ooxmlId, preVal != null || sysVal != null);
        if (preVal != null) {
            xml.addNewPrstClr().setVal(preVal);
        } else {
            xml.addNewSysClr().setVal(sysVal);
        }
        color = new XSLFColor(xml, null, null);
        assertEquals(pc.color, color.getColor());
    }
}
Also used : STPresetColorVal(org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal) STSystemColorVal(org.openxmlformats.schemas.drawingml.x2006.main.STSystemColorVal) Color(java.awt.Color) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor) CTHslColor(org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor) CTSRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor) CTSystemColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor) PresetColor(org.apache.poi.sl.usermodel.PresetColor) PresetColor(org.apache.poi.sl.usermodel.PresetColor) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor) Test(org.junit.Test)

Example 2 with PresetColor

use of org.apache.poi.sl.usermodel.PresetColor in project poi by apache.

the class HSLFShape method getSysIndexColor.

private Color getSysIndexColor(EscherColorRef ecr) {
    SysIndexSource sis = ecr.getSysIndexSource();
    if (sis == null) {
        int sysIdx = ecr.getSysIndex();
        PresetColor pc = PresetColor.valueOfNativeId(sysIdx);
        return (pc != null) ? pc.color : null;
    }
    // a different color type
    switch(sis) {
        case FILL_COLOR:
            {
                return getFill().getForegroundColor();
            }
        case LINE_OR_FILL_COLOR:
            {
                Color col = null;
                if (this instanceof HSLFSimpleShape) {
                    col = ((HSLFSimpleShape) this).getLineColor();
                }
                if (col == null) {
                    col = getFill().getForegroundColor();
                }
                return col;
            }
        case LINE_COLOR:
            {
                if (this instanceof HSLFSimpleShape) {
                    return ((HSLFSimpleShape) this).getLineColor();
                }
                break;
            }
        case SHADOW_COLOR:
            {
                if (this instanceof HSLFSimpleShape) {
                    return ((HSLFSimpleShape) this).getShadowColor();
                }
                break;
            }
        case CURRENT_OR_LAST_COLOR:
            {
                // TODO ... read from graphics context???
                break;
            }
        case FILL_BACKGROUND_COLOR:
            {
                return getFill().getBackgroundColor();
            }
        case LINE_BACKGROUND_COLOR:
            {
                if (this instanceof HSLFSimpleShape) {
                    return ((HSLFSimpleShape) this).getLineBackgroundColor();
                }
                break;
            }
        case FILL_OR_LINE_COLOR:
            {
                Color col = getFill().getForegroundColor();
                if (col == null && this instanceof HSLFSimpleShape) {
                    col = ((HSLFSimpleShape) this).getLineColor();
                }
                return col;
            }
        default:
            break;
    }
    return null;
}
Also used : Color(java.awt.Color) PresetColor(org.apache.poi.sl.usermodel.PresetColor) PresetColor(org.apache.poi.sl.usermodel.PresetColor) SysIndexSource(org.apache.poi.ddf.EscherColorRef.SysIndexSource)

Example 3 with PresetColor

use of org.apache.poi.sl.usermodel.PresetColor in project poi by apache.

the class XSLFColor method toColor.

Color toColor(XmlObject obj, XSLFTheme theme) {
    Color color = null;
    for (XmlObject ch : obj.selectPath("*")) {
        if (ch instanceof CTHslColor) {
            CTHslColor hsl = (CTHslColor) ch;
            int h = hsl.getHue2();
            int s = hsl.getSat2();
            int l = hsl.getLum2();
            color = DrawPaint.HSL2RGB(h / 60000d, s / 1000d, l / 1000d, 1d);
        } else if (ch instanceof CTPresetColor) {
            CTPresetColor prst = (CTPresetColor) ch;
            String colorName = prst.getVal().toString();
            PresetColor pc = PresetColor.valueOfOoxmlId(colorName);
            if (pc != null) {
                color = pc.color;
            }
        } else if (ch instanceof CTSchemeColor) {
            CTSchemeColor schemeColor = (CTSchemeColor) ch;
            String colorRef = schemeColor.getVal().toString();
            if (_phClr != null) {
                // context color overrides the theme
                colorRef = _phClr.getVal().toString();
            }
            // find referenced CTColor in the theme and convert it to java.awt.Color via a recursive call
            CTColor ctColor = theme.getCTColor(colorRef);
            if (ctColor != null) {
                color = toColor(ctColor, null);
            }
        } else if (ch instanceof CTScRgbColor) {
            // color in percentage is in linear RGB color space, i.e. needs to be gamma corrected for AWT color
            CTScRgbColor scrgb = (CTScRgbColor) ch;
            color = new Color(DrawPaint.lin2srgb(scrgb.getR()), DrawPaint.lin2srgb(scrgb.getG()), DrawPaint.lin2srgb(scrgb.getB()));
        } else if (ch instanceof CTSRgbColor) {
            // color in sRGB color space, i.e. same as AWT Color
            CTSRgbColor srgb = (CTSRgbColor) ch;
            byte[] val = srgb.getVal();
            color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
        } else if (ch instanceof CTSystemColor) {
            CTSystemColor sys = (CTSystemColor) ch;
            if (sys.isSetLastClr()) {
                byte[] val = sys.getLastClr();
                color = new Color(0xFF & val[0], 0xFF & val[1], 0xFF & val[2]);
            } else {
                String colorName = sys.getVal().toString();
                PresetColor pc = PresetColor.valueOfOoxmlId(colorName);
                if (pc != null) {
                    color = pc.color;
                }
                if (color == null) {
                    color = Color.black;
                }
            }
        } else if (ch instanceof CTFontReference) {
            // try next ...
            continue;
        } else {
            throw new IllegalArgumentException("Unexpected color choice: " + ch.getClass());
        }
    }
    return color;
}
Also used : CTPresetColor(org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor) Color(java.awt.Color) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor) CTPresetColor(org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor) CTScRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor) CTHslColor(org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor) CTSRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor) CTSystemColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor) PresetColor(org.apache.poi.sl.usermodel.PresetColor) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) CTScRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor) CTFontReference(org.openxmlformats.schemas.drawingml.x2006.main.CTFontReference) CTHslColor(org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor) DrawPaint(org.apache.poi.sl.draw.DrawPaint) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor) CTSystemColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor) XmlObject(org.apache.xmlbeans.XmlObject) CTPresetColor(org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor) PresetColor(org.apache.poi.sl.usermodel.PresetColor) CTSRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor)

Aggregations

Color (java.awt.Color)3 PresetColor (org.apache.poi.sl.usermodel.PresetColor)3 CTColor (org.openxmlformats.schemas.drawingml.x2006.main.CTColor)2 CTHslColor (org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor)2 CTSRgbColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor)2 CTSystemColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor)2 SysIndexSource (org.apache.poi.ddf.EscherColorRef.SysIndexSource)1 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 XmlObject (org.apache.xmlbeans.XmlObject)1 Test (org.junit.Test)1 CTFontReference (org.openxmlformats.schemas.drawingml.x2006.main.CTFontReference)1 CTPresetColor (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor)1 CTScRgbColor (org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor)1 CTSchemeColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor)1 STPresetColorVal (org.openxmlformats.schemas.drawingml.x2006.main.STPresetColorVal)1 STSystemColorVal (org.openxmlformats.schemas.drawingml.x2006.main.STSystemColorVal)1