Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds gradient export to SVG library #770

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 114 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,30 @@ 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 +131,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 +149,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 +274,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 +319,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 +413,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
6 changes: 3 additions & 3 deletions core/src/processing/core/PShapeSVG.java
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,8 @@ void setFillOpacity(String opacityText) {
fillColor = ((int) (fillOpacity * 255)) << 24 | fillColor & 0xFFFFFF;
}


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 @@ -1497,7 +1497,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