Merge pull request #929 from tracerstar/svg-gradient

Adds gradient export to SVG library
This commit is contained in:
Stef Tervelde
2025-01-21 18:00:40 +01:00
committed by GitHub
4 changed files with 329 additions and 8 deletions
+113 -6
View File
@@ -33,6 +33,9 @@ import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.util.Arrays;
import java.awt.Color;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PShapeSVG;
@@ -96,16 +99,29 @@ public class PShapeJava2D extends PShapeSVG {
*/
static class LinearGradientPaint implements Paint {
public static class LinearGradientPaint implements Paint {
float x1, y1, x2, y2;
float[] offset;
int[] color;
Color[] colors;
int count;
float opacity;
AffineTransform xform;
public static enum CycleMethod {
NO_CYCLE,
REFLECT,
REPEAT
}
public static enum ColorSpaceType {
SRGB,
LINEAR_RGB
}
public LinearGradientPaint(float x1, float y1, float x2, float y2,
float[] offset, int[] color, int count,
float opacity) {
float opacity, AffineTransform xform) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
@@ -114,6 +130,13 @@ public class PShapeJava2D extends PShapeSVG {
this.color = color;
this.count = count;
this.opacity = opacity;
this.xform = xform;
//set an array of type Color
this.colors = new Color[this.color.length];
for (int i = 0; i < this.color.length; i++) {
this.colors[i] = new Color(this.color[i], true);
}
}
public PaintContext createContext(ColorModel cm,
@@ -125,6 +148,35 @@ public class PShapeJava2D extends PShapeSVG {
(float) t2.getX(), (float) t2.getY());
}
public Point2D getStartPoint() {
return new Point2D.Float(this.x1, this.y1);
}
public Point2D getEndPoint() {
return new Point2D.Float(this.x2, this.y2);
}
/* MultipleGradientPaint methods... */
public AffineTransform getTransform() {
return this.xform;
}
public ColorSpaceType getColorSpace() {
return ColorSpaceType.SRGB;
}
public CycleMethod getCycleMethod() {
return CycleMethod.NO_CYCLE;
}
public Color[] getColors() {
return Arrays.copyOf(this.colors, this.colors.length);
}
public float[] getFractions() {
return Arrays.copyOf(this.offset, this.offset.length);
}
public int getTransparency() {
return TRANSLUCENT; // why not.. rather than checking each color
}
@@ -221,16 +273,29 @@ public class PShapeJava2D extends PShapeSVG {
}
static class RadialGradientPaint implements Paint {
public static class RadialGradientPaint implements Paint {
float cx, cy, radius;
float[] offset;
int[] color;
Color[] colors;
int count;
float opacity;
AffineTransform xform;
public static enum CycleMethod {
NO_CYCLE,
REFLECT,
REPEAT
}
public static enum ColorSpaceType {
SRGB,
LINEAR_RGB
}
public RadialGradientPaint(float cx, float cy, float radius,
float[] offset, int[] color, int count,
float opacity) {
float opacity, AffineTransform xform) {
this.cx = cx;
this.cy = cy;
this.radius = radius;
@@ -238,6 +303,13 @@ public class PShapeJava2D extends PShapeSVG {
this.color = color;
this.count = count;
this.opacity = opacity;
this.xform = xform;
//set an array of type Color
this.colors = new Color[this.color.length];
for (int i = 0; i < this.color.length; i++) {
this.colors[i] = new Color(this.color[i], true);
}
}
public PaintContext createContext(ColorModel cm,
@@ -246,6 +318,41 @@ public class PShapeJava2D extends PShapeSVG {
return new RadialGradientContext();
}
public Point2D getCenterPoint() {
return new Point2D.Double(this.cx, this.cy);
}
//TODO: investigate how to change a focus point for 0% x of the gradient
//for now default to center x/y
public Point2D getFocusPoint() {
return new Point2D.Double(this.cx, this.cy);
}
public float getRadius() {
return this.radius;
}
/* MultipleGradientPaint methods... */
public AffineTransform getTransform() {
return this.xform;
}
public ColorSpaceType getColorSpace() {
return ColorSpaceType.SRGB;
}
public CycleMethod getCycleMethod() {
return CycleMethod.NO_CYCLE;
}
public Color[] getColors() {
return Arrays.copyOf(this.colors, this.colors.length);
}
public float[] getFractions() {
return Arrays.copyOf(this.offset, this.offset.length);
}
public int getTransparency() {
return TRANSLUCENT;
}
@@ -305,14 +412,14 @@ public class PShapeJava2D extends PShapeSVG {
LinearGradient grad = (LinearGradient) gradient;
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
grad.offset, grad.color, grad.count,
opacity);
opacity, grad.transform);
} else if (gradient instanceof RadialGradient) {
// System.out.println("creating radial gradient");
RadialGradient grad = (RadialGradient) gradient;
return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
grad.offset, grad.color, grad.count,
opacity);
opacity, grad.transform);
}
return null;
}
+3 -2
View File
@@ -1397,7 +1397,8 @@ public class PShapeSVG extends PShape {
}
void setColor(String colorText, boolean isFill) {
//making this public allows us to set gradient fills on a PShape
public void setColor(String colorText, boolean isFill) {
colorText = colorText.trim();
int opacityMask = fillColor & 0xFF000000;
boolean visible = true;
@@ -1620,7 +1621,7 @@ public class PShapeSVG extends PShape {
static public class Gradient extends PShapeSVG {
AffineTransform transform;
public AffineTransform transform;
public float[] offset;
public int[] color;