Skip to content

Commit

Permalink
Merge pull request #929 from tracerstar/svg-gradient
Browse files Browse the repository at this point in the history
Adds gradient export to SVG library
  • Loading branch information
Stefterv authored Jan 21, 2025
2 parents 78ef9f5 + c8a85e3 commit b024526
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 8 deletions.
119 changes: 113 additions & 6 deletions core/src/processing/awt/PShapeJava2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
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;
Expand Down Expand Up @@ -96,16 +99,29 @@ public void setColor(String colorText, boolean isFill) {
*/


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;
Expand All @@ -114,6 +130,13 @@ public LinearGradientPaint(float x1, float y1, float x2, float y2,
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,
Expand All @@ -125,6 +148,35 @@ public PaintContext createContext(ColorModel cm,
(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
}
Expand Down Expand Up @@ -221,23 +273,43 @@ public Raster getRaster(int x, int y, int w, int h) {
}


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;
this.offset = offset;
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,
Expand All @@ -246,6 +318,41 @@ public PaintContext createContext(ColorModel cm,
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;
}
Expand Down Expand Up @@ -305,14 +412,14 @@ protected Paint calcGradientPaint(Gradient gradient) {
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;
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/processing/core/PShapeSVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,8 @@ void setFillOpacity(String opacityText) {
}


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;
Expand Down Expand Up @@ -1620,7 +1621,7 @@ static protected float parseFloatOrPercent(String text) {


static public class Gradient extends PShapeSVG {
AffineTransform transform;
public AffineTransform transform;

public float[] offset;
public int[] color;
Expand Down
Loading

0 comments on commit b024526

Please sign in to comment.