Skip to content

Commit b024526

Browse files
authored
Merge pull request #929 from tracerstar/svg-gradient
Adds gradient export to SVG library
2 parents 78ef9f5 + c8a85e3 commit b024526

File tree

4 files changed

+329
-8
lines changed

4 files changed

+329
-8
lines changed

core/src/processing/awt/PShapeJava2D.java

+113-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import java.awt.image.Raster;
3434
import java.awt.image.WritableRaster;
3535

36+
import java.util.Arrays;
37+
import java.awt.Color;
38+
3639
import processing.core.PApplet;
3740
import processing.core.PGraphics;
3841
import processing.core.PShapeSVG;
@@ -96,16 +99,29 @@ public void setColor(String colorText, boolean isFill) {
9699
*/
97100

98101

99-
static class LinearGradientPaint implements Paint {
102+
public static class LinearGradientPaint implements Paint {
100103
float x1, y1, x2, y2;
101104
float[] offset;
102105
int[] color;
106+
Color[] colors;
103107
int count;
104108
float opacity;
109+
AffineTransform xform;
110+
111+
public static enum CycleMethod {
112+
NO_CYCLE,
113+
REFLECT,
114+
REPEAT
115+
}
116+
117+
public static enum ColorSpaceType {
118+
SRGB,
119+
LINEAR_RGB
120+
}
105121

106122
public LinearGradientPaint(float x1, float y1, float x2, float y2,
107123
float[] offset, int[] color, int count,
108-
float opacity) {
124+
float opacity, AffineTransform xform) {
109125
this.x1 = x1;
110126
this.y1 = y1;
111127
this.x2 = x2;
@@ -114,6 +130,13 @@ public LinearGradientPaint(float x1, float y1, float x2, float y2,
114130
this.color = color;
115131
this.count = count;
116132
this.opacity = opacity;
133+
this.xform = xform;
134+
135+
//set an array of type Color
136+
this.colors = new Color[this.color.length];
137+
for (int i = 0; i < this.color.length; i++) {
138+
this.colors[i] = new Color(this.color[i], true);
139+
}
117140
}
118141

119142
public PaintContext createContext(ColorModel cm,
@@ -125,6 +148,35 @@ public PaintContext createContext(ColorModel cm,
125148
(float) t2.getX(), (float) t2.getY());
126149
}
127150

151+
public Point2D getStartPoint() {
152+
return new Point2D.Float(this.x1, this.y1);
153+
}
154+
155+
public Point2D getEndPoint() {
156+
return new Point2D.Float(this.x2, this.y2);
157+
}
158+
159+
/* MultipleGradientPaint methods... */
160+
public AffineTransform getTransform() {
161+
return this.xform;
162+
}
163+
164+
public ColorSpaceType getColorSpace() {
165+
return ColorSpaceType.SRGB;
166+
}
167+
168+
public CycleMethod getCycleMethod() {
169+
return CycleMethod.NO_CYCLE;
170+
}
171+
172+
public Color[] getColors() {
173+
return Arrays.copyOf(this.colors, this.colors.length);
174+
}
175+
176+
public float[] getFractions() {
177+
return Arrays.copyOf(this.offset, this.offset.length);
178+
}
179+
128180
public int getTransparency() {
129181
return TRANSLUCENT; // why not.. rather than checking each color
130182
}
@@ -221,23 +273,43 @@ public Raster getRaster(int x, int y, int w, int h) {
221273
}
222274

223275

224-
static class RadialGradientPaint implements Paint {
276+
public static class RadialGradientPaint implements Paint {
225277
float cx, cy, radius;
226278
float[] offset;
227279
int[] color;
280+
Color[] colors;
228281
int count;
229282
float opacity;
283+
AffineTransform xform;
284+
285+
public static enum CycleMethod {
286+
NO_CYCLE,
287+
REFLECT,
288+
REPEAT
289+
}
290+
291+
public static enum ColorSpaceType {
292+
SRGB,
293+
LINEAR_RGB
294+
}
230295

231296
public RadialGradientPaint(float cx, float cy, float radius,
232297
float[] offset, int[] color, int count,
233-
float opacity) {
298+
float opacity, AffineTransform xform) {
234299
this.cx = cx;
235300
this.cy = cy;
236301
this.radius = radius;
237302
this.offset = offset;
238303
this.color = color;
239304
this.count = count;
240305
this.opacity = opacity;
306+
this.xform = xform;
307+
308+
//set an array of type Color
309+
this.colors = new Color[this.color.length];
310+
for (int i = 0; i < this.color.length; i++) {
311+
this.colors[i] = new Color(this.color[i], true);
312+
}
241313
}
242314

243315
public PaintContext createContext(ColorModel cm,
@@ -246,6 +318,41 @@ public PaintContext createContext(ColorModel cm,
246318
return new RadialGradientContext();
247319
}
248320

321+
public Point2D getCenterPoint() {
322+
return new Point2D.Double(this.cx, this.cy);
323+
}
324+
325+
//TODO: investigate how to change a focus point for 0% x of the gradient
326+
//for now default to center x/y
327+
public Point2D getFocusPoint() {
328+
return new Point2D.Double(this.cx, this.cy);
329+
}
330+
331+
public float getRadius() {
332+
return this.radius;
333+
}
334+
335+
/* MultipleGradientPaint methods... */
336+
public AffineTransform getTransform() {
337+
return this.xform;
338+
}
339+
340+
public ColorSpaceType getColorSpace() {
341+
return ColorSpaceType.SRGB;
342+
}
343+
344+
public CycleMethod getCycleMethod() {
345+
return CycleMethod.NO_CYCLE;
346+
}
347+
348+
public Color[] getColors() {
349+
return Arrays.copyOf(this.colors, this.colors.length);
350+
}
351+
352+
public float[] getFractions() {
353+
return Arrays.copyOf(this.offset, this.offset.length);
354+
}
355+
249356
public int getTransparency() {
250357
return TRANSLUCENT;
251358
}
@@ -305,14 +412,14 @@ protected Paint calcGradientPaint(Gradient gradient) {
305412
LinearGradient grad = (LinearGradient) gradient;
306413
return new LinearGradientPaint(grad.x1, grad.y1, grad.x2, grad.y2,
307414
grad.offset, grad.color, grad.count,
308-
opacity);
415+
opacity, grad.transform);
309416

310417
} else if (gradient instanceof RadialGradient) {
311418
// System.out.println("creating radial gradient");
312419
RadialGradient grad = (RadialGradient) gradient;
313420
return new RadialGradientPaint(grad.cx, grad.cy, grad.r,
314421
grad.offset, grad.color, grad.count,
315-
opacity);
422+
opacity, grad.transform);
316423
}
317424
return null;
318425
}

core/src/processing/core/PShapeSVG.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,8 @@ void setFillOpacity(String opacityText) {
13971397
}
13981398

13991399

1400-
void setColor(String colorText, boolean isFill) {
1400+
//making this public allows us to set gradient fills on a PShape
1401+
public void setColor(String colorText, boolean isFill) {
14011402
colorText = colorText.trim();
14021403
int opacityMask = fillColor & 0xFF000000;
14031404
boolean visible = true;
@@ -1620,7 +1621,7 @@ static protected float parseFloatOrPercent(String text) {
16201621

16211622

16221623
static public class Gradient extends PShapeSVG {
1623-
AffineTransform transform;
1624+
public AffineTransform transform;
16241625

16251626
public float[] offset;
16261627
public int[] color;

0 commit comments

Comments
 (0)