Search in sources :

Example 6 with ColorStyle

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

the class DrawPaint method createLinearGradientPaint.

protected Paint createLinearGradientPaint(GradientPaint fill, Graphics2D graphics) {
    // TODO: we need to find the two points for gradient - the problem is, which point at the outline
    // do you take? My solution would be to apply the gradient rotation to the shape in reverse
    // and then scan the shape for the largest possible horizontal distance
    double angle = fill.getGradientAngle();
    if (!fill.isRotatedWithShape()) {
        angle -= shape.getRotation();
    }
    Rectangle2D anchor = DrawShape.getAnchor(graphics, shape);
    final double h = anchor.getHeight(), w = anchor.getWidth(), x = anchor.getX(), y = anchor.getY();
    AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(angle), anchor.getCenterX(), anchor.getCenterY());
    double diagonal = Math.sqrt(h * h + w * w);
    Point2D p1 = new Point2D.Double(x + w / 2 - diagonal / 2, y + h / 2);
    p1 = at.transform(p1, null);
    Point2D p2 = new Point2D.Double(x + w, y + h / 2);
    p2 = at.transform(p2, null);
    if (p1.equals(p2)) {
        // gradient paint on the same point throws an exception ... and doesn't make sense
        return null;
    }
    float[] fractions = fill.getGradientFractions();
    Color[] colors = new Color[fractions.length];
    int i = 0;
    for (ColorStyle fc : fill.getGradientColors()) {
        // if fc is null, use transparent color to get color of background
        colors[i++] = (fc == null) ? TRANSPARENT : applyColorTransform(fc);
    }
    return new LinearGradientPaint(p1, p2, 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) AffineTransform(java.awt.geom.AffineTransform) LinearGradientPaint(java.awt.LinearGradientPaint) 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 7 with ColorStyle

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

the class DrawPaint method getSolidPaint.

protected Paint getSolidPaint(SolidPaint fill, Graphics2D graphics, final PaintModifier modifier) {
    final ColorStyle orig = fill.getSolidColor();
    ColorStyle cs = new ColorStyle() {

        @Override
        public Color getColor() {
            return orig.getColor();
        }

        @Override
        public int getAlpha() {
            return orig.getAlpha();
        }

        @Override
        public int getHueOff() {
            return orig.getHueOff();
        }

        @Override
        public int getHueMod() {
            return orig.getHueMod();
        }

        @Override
        public int getSatOff() {
            return orig.getSatOff();
        }

        @Override
        public int getSatMod() {
            return orig.getSatMod();
        }

        @Override
        public int getLumOff() {
            return orig.getLumOff();
        }

        @Override
        public int getLumMod() {
            return orig.getLumMod();
        }

        @Override
        public int getShade() {
            int shade = orig.getShade();
            switch(modifier) {
                case DARKEN:
                    return Math.min(100000, Math.max(0, shade) + 40000);
                case DARKEN_LESS:
                    return Math.min(100000, Math.max(0, shade) + 20000);
                default:
                    return shade;
            }
        }

        @Override
        public int getTint() {
            int tint = orig.getTint();
            switch(modifier) {
                case LIGHTEN:
                    return Math.min(100000, Math.max(0, tint) + 40000);
                case LIGHTEN_LESS:
                    return Math.min(100000, Math.max(0, tint) + 20000);
                default:
                    return tint;
            }
        }
    };
    return applyColorTransform(cs);
}
Also used : ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) 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