Search in sources :

Example 6 with CTSRgbColor

use of org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor in project poi by apache.

the class TestXSLFColor method testGetters.

@Test
public void testGetters() {
    CTColor xml = CTColor.Factory.newInstance();
    CTSRgbColor c = xml.addNewSrgbClr();
    c.setVal(new byte[] { (byte) 0xFF, 0, 0 });
    XSLFColor color = new XSLFColor(xml, null, null);
    assertEquals(-1, color.getAlpha());
    c.addNewAlpha().setVal(50000);
    assertEquals(50, color.getAlpha());
    assertEquals(-1, color.getAlphaMod());
    c.addNewAlphaMod().setVal(50000);
    assertEquals(50, color.getAlphaMod());
    assertEquals(-1, color.getAlphaOff());
    c.addNewAlphaOff().setVal(50000);
    assertEquals(50, color.getAlphaOff());
    assertEquals(-1, color.getLumMod());
    c.addNewLumMod().setVal(50000);
    assertEquals(50, color.getLumMod());
    assertEquals(-1, color.getLumOff());
    c.addNewLumOff().setVal(50000);
    assertEquals(50, color.getLumOff());
    assertEquals(-1, color.getSat());
    c.addNewSat().setVal(50000);
    assertEquals(50, color.getSat());
    assertEquals(-1, color.getSatMod());
    c.addNewSatMod().setVal(50000);
    assertEquals(50, color.getSatMod());
    assertEquals(-1, color.getSatOff());
    c.addNewSatOff().setVal(50000);
    assertEquals(50, color.getSatOff());
    assertEquals(-1, color.getRed());
    c.addNewRed().setVal(50000);
    assertEquals(50, color.getRed());
    assertEquals(-1, color.getGreen());
    c.addNewGreen().setVal(50000);
    assertEquals(50, color.getGreen());
    assertEquals(-1, color.getBlue());
    c.addNewBlue().setVal(50000);
    assertEquals(50, color.getRed());
    assertEquals(-1, color.getShade());
    c.addNewShade().setVal(50000);
    assertEquals(50, color.getShade());
    assertEquals(-1, color.getTint());
    c.addNewTint().setVal(50000);
    assertEquals(50, color.getTint());
}
Also used : CTSRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor) CTColor(org.openxmlformats.schemas.drawingml.x2006.main.CTColor) Test(org.junit.Test)

Example 7 with CTSRgbColor

use of org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor in project poi by apache.

the class XSLFColor method setColor.

/**
     * Sets the solid color
     *
     * @param color solid color
     */
@Internal
protected void setColor(Color color) {
    if (!(_xmlObject instanceof CTSolidColorFillProperties)) {
        LOGGER.log(POILogger.ERROR, "XSLFColor.setColor currently only supports CTSolidColorFillProperties");
        return;
    }
    CTSolidColorFillProperties fill = (CTSolidColorFillProperties) _xmlObject;
    if (fill.isSetSrgbClr()) {
        fill.unsetSrgbClr();
    }
    if (fill.isSetScrgbClr()) {
        fill.unsetScrgbClr();
    }
    if (fill.isSetHslClr()) {
        fill.unsetHslClr();
    }
    if (fill.isSetPrstClr()) {
        fill.unsetPrstClr();
    }
    if (fill.isSetSchemeClr()) {
        fill.unsetSchemeClr();
    }
    if (fill.isSetSysClr()) {
        fill.unsetSysClr();
    }
    float[] rgbaf = color.getRGBComponents(null);
    boolean addAlpha = (rgbaf.length == 4 && rgbaf[3] < 1f);
    CTPositiveFixedPercentage alphaPct;
    // see office open xml part 4 - 5.1.2.2.30 and 5.1.2.2.32
    if (isInt(rgbaf[0]) && isInt(rgbaf[1]) && isInt(rgbaf[2])) {
        // sRGB has a gamma of 2.2
        CTSRgbColor rgb = fill.addNewSrgbClr();
        byte[] rgbBytes = { (byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue() };
        rgb.setVal(rgbBytes);
        alphaPct = (addAlpha) ? rgb.addNewAlpha() : null;
    } else {
        CTScRgbColor rgb = fill.addNewScrgbClr();
        rgb.setR(DrawPaint.srgb2lin(rgbaf[0]));
        rgb.setG(DrawPaint.srgb2lin(rgbaf[1]));
        rgb.setB(DrawPaint.srgb2lin(rgbaf[2]));
        alphaPct = (addAlpha) ? rgb.addNewAlpha() : null;
    }
    // alpha (%)
    if (alphaPct != null) {
        alphaPct.setVal((int) (100000 * rgbaf[3]));
    }
}
Also used : CTScRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor) CTSolidColorFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties) CTSRgbColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor) CTPositiveFixedPercentage(org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveFixedPercentage) Internal(org.apache.poi.util.Internal)

Example 8 with CTSRgbColor

use of org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor 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

CTSRgbColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSRgbColor)8 CTSolidColorFillProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTSolidColorFillProperties)5 Color (java.awt.Color)3 CTColor (org.openxmlformats.schemas.drawingml.x2006.main.CTColor)3 CTScRgbColor (org.openxmlformats.schemas.drawingml.x2006.main.CTScRgbColor)2 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)2 CTTextCharacterProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 PresetColor (org.apache.poi.sl.usermodel.PresetColor)1 Internal (org.apache.poi.util.Internal)1 XmlObject (org.apache.xmlbeans.XmlObject)1 Test (org.junit.Test)1 CTFontReference (org.openxmlformats.schemas.drawingml.x2006.main.CTFontReference)1 CTHslColor (org.openxmlformats.schemas.drawingml.x2006.main.CTHslColor)1 CTLineProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTLineProperties)1 CTPositiveFixedPercentage (org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveFixedPercentage)1 CTPresetColor (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetColor)1 CTSchemeColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor)1 CTSystemColor (org.openxmlformats.schemas.drawingml.x2006.main.CTSystemColor)1