use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint 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);
}
use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint 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;
}
};
}
use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint in project poi by apache.
the class TestXSLFTheme method slide4.
void slide4(XSLFSlide slide) {
PaintStyle fs = slide.getBackground().getFillStyle().getPaint();
assertTrue(fs instanceof GradientPaint);
XSLFTextShape sh1 = (XSLFTextShape) getShape(slide, "Rectangle 4");
XSLFTextRun run1 = sh1.getTextParagraphs().get(0).getTextRuns().get(0);
assertTrue(sameColor(Color.white, run1.getFontColor()));
assertEquals(new Color(148, 198, 0), sh1.getFillColor());
// solid fill
assertTrue(sh1.getFillStyle().getPaint() instanceof SolidPaint);
XSLFTextShape sh2 = (XSLFTextShape) getShape(slide, "Title 3");
XSLFTextRun run2 = sh2.getTextParagraphs().get(0).getTextRuns().get(0);
assertTrue(sameColor(new Color(148, 198, 0), run2.getFontColor()));
// no fill
assertNull(sh2.getFillColor());
assertTrue(slide.getSlideLayout().getFollowMasterGraphics());
}
use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint 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);
}
use of org.apache.poi.sl.usermodel.PaintStyle.GradientPaint 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);
}
Aggregations