Search in sources :

Example 1 with ColorStyle

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

the class DrawPaint method createRadialGradientPaint.

protected Paint createRadialGradientPaint(GradientPaint fill, Graphics2D graphics) {
    Rectangle2D anchor = DrawShape.getAnchor(graphics, shape);
    Point2D pCenter = new Point2D.Double(anchor.getX() + anchor.getWidth() / 2, anchor.getY() + anchor.getHeight() / 2);
    float radius = (float) Math.max(anchor.getWidth(), anchor.getHeight());
    float[] fractions = fill.getGradientFractions();
    Color[] colors = new Color[fractions.length];
    int i = 0;
    for (ColorStyle fc : fill.getGradientColors()) {
        colors[i++] = applyColorTransform(fc);
    }
    return new RadialGradientPaint(pCenter, radius, fractions, colors);
}
Also used : Point2D(java.awt.geom.Point2D) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) RadialGradientPaint(java.awt.RadialGradientPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Example 2 with ColorStyle

use of org.apache.poi.sl.usermodel.ColorStyle 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 3 with ColorStyle

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

the class XSLFShape method selectPaint.

protected static PaintStyle selectPaint(final CTGradientFillProperties gradFill, CTSchemeColor phClr, final XSLFTheme theme) {
    final CTGradientStop[] gs = gradFill.getGsLst().getGsArray();
    Arrays.sort(gs, new Comparator<CTGradientStop>() {

        public int compare(CTGradientStop o1, CTGradientStop o2) {
            Integer pos1 = o1.getPos();
            Integer pos2 = o2.getPos();
            return pos1.compareTo(pos2);
        }
    });
    final ColorStyle[] cs = new ColorStyle[gs.length];
    final float[] fractions = new float[gs.length];
    int i = 0;
    for (CTGradientStop cgs : gs) {
        CTSchemeColor phClrCgs = phClr;
        if (phClrCgs == null && cgs.isSetSchemeClr()) {
            phClrCgs = cgs.getSchemeClr();
        }
        cs[i] = new XSLFColor(cgs, theme, phClrCgs).getColorStyle();
        fractions[i] = cgs.getPos() / 100000.f;
        i++;
    }
    return new GradientPaint() {

        public double getGradientAngle() {
            return (gradFill.isSetLin()) ? gradFill.getLin().getAng() / 60000.d : 0;
        }

        public ColorStyle[] getGradientColors() {
            return cs;
        }

        public float[] getGradientFractions() {
            return fractions;
        }

        public boolean isRotatedWithShape() {
            return gradFill.getRotWithShape();
        }

        public GradientType getGradientType() {
            if (gradFill.isSetLin()) {
                return GradientType.linear;
            }
            if (gradFill.isSetPath()) {
                /* TODO: handle rect path */
                STPathShadeType.Enum ps = gradFill.getPath().getPath();
                if (ps == STPathShadeType.CIRCLE) {
                    return GradientType.circular;
                } else if (ps == STPathShadeType.SHAPE) {
                    return GradientType.shape;
                }
            }
            return GradientType.linear;
        }
    };
}
Also used : STPathShadeType(org.openxmlformats.schemas.drawingml.x2006.main.STPathShadeType) CTGradientStop(org.openxmlformats.schemas.drawingml.x2006.main.CTGradientStop) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) CTSchemeColor(org.openxmlformats.schemas.drawingml.x2006.main.CTSchemeColor) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) DrawPaint(org.apache.poi.sl.draw.DrawPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)

Example 4 with ColorStyle

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

the class TestBugs method bug55983.

@Test
public void bug55983() throws IOException {
    HSLFSlideShow ppt1 = new HSLFSlideShow();
    HSLFSlide sl = ppt1.createSlide();
    sl.getBackground().getFill().setForegroundColor(Color.blue);
    HSLFFreeformShape fs = sl.createFreeform();
    Ellipse2D.Double el = new Ellipse2D.Double(0, 0, 300, 200);
    fs.setAnchor(new Rectangle2D.Double(100, 100, 300, 200));
    fs.setPath(new Path2D.Double(el));
    Color cExp = new Color(50, 100, 150, 200);
    fs.setFillColor(cExp);
    HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
    ppt1.close();
    sl = ppt2.getSlides().get(0);
    fs = (HSLFFreeformShape) sl.getShapes().get(0);
    Color cAct = fs.getFillColor();
    assertEquals(cExp.getRed(), cAct.getRed());
    assertEquals(cExp.getGreen(), cAct.getGreen());
    assertEquals(cExp.getBlue(), cAct.getBlue());
    assertEquals(cExp.getAlpha(), cAct.getAlpha(), 1);
    PaintStyle ps = fs.getFillStyle().getPaint();
    assertTrue(ps instanceof SolidPaint);
    ColorStyle cs = ((SolidPaint) ps).getSolidColor();
    cAct = cs.getColor();
    assertEquals(cExp.getRed(), cAct.getRed());
    assertEquals(cExp.getGreen(), cAct.getGreen());
    assertEquals(cExp.getBlue(), cAct.getBlue());
    assertEquals(255, cAct.getAlpha());
    assertEquals(cExp.getAlpha() * 100000. / 255., cs.getAlpha(), 1);
    ppt2.close();
}
Also used : ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Path2D(java.awt.geom.Path2D) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) Ellipse2D(java.awt.geom.Ellipse2D) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) PaintStyle(org.apache.poi.sl.usermodel.PaintStyle) Test(org.junit.Test)

Example 5 with ColorStyle

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

the class DrawPaint method createPathGradientPaint.

protected Paint createPathGradientPaint(GradientPaint fill, Graphics2D graphics) {
    // currently we ignore an eventually center setting
    float[] fractions = fill.getGradientFractions();
    Color[] colors = new Color[fractions.length];
    int i = 0;
    for (ColorStyle fc : fill.getGradientColors()) {
        colors[i++] = applyColorTransform(fc);
    }
    return new PathGradientPaint(colors, fractions);
}
Also used : ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Aggregations

ColorStyle (org.apache.poi.sl.usermodel.ColorStyle)7 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)6 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)6 Color (java.awt.Color)5 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)5 LinearGradientPaint (java.awt.LinearGradientPaint)4 Paint (java.awt.Paint)4 RadialGradientPaint (java.awt.RadialGradientPaint)4 Rectangle2D (java.awt.geom.Rectangle2D)3 Point2D (java.awt.geom.Point2D)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)2 AffineTransform (java.awt.geom.AffineTransform)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Path2D (java.awt.geom.Path2D)1 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)1 EscherArrayProperty (org.apache.poi.ddf.EscherArrayProperty)1 EscherColorRef (org.apache.poi.ddf.EscherColorRef)1 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)1 PaintStyle (org.apache.poi.sl.usermodel.PaintStyle)1 Test (org.junit.Test)1