Search in sources :

Example 1 with EscherColorRef

use of org.apache.poi.ddf.EscherColorRef in project poi by apache.

the class HSLFFill method getGradientPaint.

private GradientPaint getGradientPaint(final GradientType gradientType) {
    AbstractEscherOptRecord opt = shape.getEscherOptRecord();
    final EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS);
    final int colorCnt = (ep == null) ? 0 : ep.getNumberOfElementsInArray();
    // NOFILLHITTEST can be in the normal escher opt record but also in the tertiary record
    // the extended bit fields seem to be in the second
    opt = (AbstractEscherOptRecord) shape.getEscherChild(RecordTypes.EscherUserDefined);
    EscherSimpleProperty p = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__NOFILLHITTEST);
    int propVal = (p == null) ? 0 : p.getPropertyValue();
    final boolean rotateWithShape = FILL_USE_USE_SHAPE_ANCHOR.isSet(propVal) && FILL_USE_SHAPE_ANCHOR.isSet(propVal);
    return new GradientPaint() {

        @Override
        public double getGradientAngle() {
            // A value of type FixedPoint, as specified in [MS-OSHARED] section 2.2.1.6,
            // that specifies the angle of the gradient fill. Zero degrees represents a vertical vector from
            // bottom to top. The default value for this property is 0x00000000.
            int rot = shape.getEscherProperty(EscherProperties.FILL__ANGLE);
            return 90 - Units.fixedPointToDouble(rot);
        }

        @Override
        public ColorStyle[] getGradientColors() {
            ColorStyle[] cs;
            if (colorCnt == 0) {
                cs = new ColorStyle[2];
                cs[0] = wrapColor(getBackgroundColor());
                cs[1] = wrapColor(getForegroundColor());
            } else {
                cs = new ColorStyle[colorCnt];
                int idx = 0;
                // TODO: handle palette colors and alpha(?) value 
                for (byte[] data : ep) {
                    EscherColorRef ecr = new EscherColorRef(data, 0, 4);
                    cs[idx++] = wrapColor(shape.getColor(ecr));
                }
            }
            return cs;
        }

        private ColorStyle wrapColor(Color col) {
            return (col == null) ? null : DrawPaint.createSolidPaint(col).getSolidColor();
        }

        @Override
        public float[] getGradientFractions() {
            float[] frc;
            if (colorCnt == 0) {
                frc = new float[] { 0, 1 };
            } else {
                frc = new float[colorCnt];
                int idx = 0;
                for (byte[] data : ep) {
                    double pos = Units.fixedPointToDouble(LittleEndian.getInt(data, 4));
                    frc[idx++] = (float) pos;
                }
            }
            return frc;
        }

        @Override
        public boolean isRotatedWithShape() {
            return rotateWithShape;
        }

        @Override
        public GradientType getGradientType() {
            return gradientType;
        }
    };
}
Also used : EscherColorRef(org.apache.poi.ddf.EscherColorRef) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) Color(java.awt.Color) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord) DrawPaint(org.apache.poi.sl.draw.DrawPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty)

Example 2 with EscherColorRef

use of org.apache.poi.ddf.EscherColorRef in project poi by apache.

the class TestBugs method bug46441.

@Test
public void bug46441() throws IOException {
    HSLFSlideShow ppt = open("bug46441.ppt");
    HSLFAutoShape as = (HSLFAutoShape) ppt.getSlides().get(0).getShapes().get(0);
    AbstractEscherOptRecord opt = as.getEscherOptRecord();
    EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS);
    double[][] exp = { // r, g, b, position
    { 94, 158, 255, 0 }, { 133, 194, 255, 0.399994 }, { 196, 214, 235, 0.699997 }, { 255, 235, 250, 1 } };
    int i = 0;
    for (byte[] data : ep) {
        EscherColorRef ecr = new EscherColorRef(data, 0, 4);
        int[] rgb = ecr.getRGB();
        double pos = Units.fixedPointToDouble(LittleEndian.getInt(data, 4));
        assertEquals((int) exp[i][0], rgb[0]);
        assertEquals((int) exp[i][1], rgb[1]);
        assertEquals((int) exp[i][2], rgb[2]);
        assertEquals(exp[i][3], pos, 0.01);
        i++;
    }
    ppt.close();
}
Also used : EscherColorRef(org.apache.poi.ddf.EscherColorRef) EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) Test(org.junit.Test)

Example 3 with EscherColorRef

use of org.apache.poi.ddf.EscherColorRef in project poi by apache.

the class HSLFShape method getColor.

Color getColor(short colorProperty, short opacityProperty, int defaultColor) {
    AbstractEscherOptRecord opt = getEscherOptRecord();
    EscherSimpleProperty p = getEscherProperty(opt, colorProperty);
    if (p == null && defaultColor == -1)
        return null;
    int val = (p == null) ? defaultColor : p.getPropertyValue();
    EscherColorRef ecr = new EscherColorRef(val);
    Color col = getColor(ecr);
    if (col == null) {
        return null;
    }
    double alpha = getAlpha(opacityProperty);
    return new Color(col.getRed(), col.getGreen(), col.getBlue(), (int) (alpha * 255.0));
}
Also used : EscherColorRef(org.apache.poi.ddf.EscherColorRef) EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty) Color(java.awt.Color) PresetColor(org.apache.poi.sl.usermodel.PresetColor) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord)

Aggregations

AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)3 EscherColorRef (org.apache.poi.ddf.EscherColorRef)3 Color (java.awt.Color)2 EscherArrayProperty (org.apache.poi.ddf.EscherArrayProperty)2 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)2 ColorStyle (org.apache.poi.sl.usermodel.ColorStyle)1 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1 PresetColor (org.apache.poi.sl.usermodel.PresetColor)1 Test (org.junit.Test)1