33
33
import java .awt .image .Raster ;
34
34
import java .awt .image .WritableRaster ;
35
35
36
+ import java .util .Arrays ;
37
+ import java .awt .Color ;
38
+
36
39
import processing .core .PApplet ;
37
40
import processing .core .PGraphics ;
38
41
import processing .core .PShapeSVG ;
@@ -96,16 +99,29 @@ public void setColor(String colorText, boolean isFill) {
96
99
*/
97
100
98
101
99
- static class LinearGradientPaint implements Paint {
102
+ public static class LinearGradientPaint implements Paint {
100
103
float x1 , y1 , x2 , y2 ;
101
104
float [] offset ;
102
105
int [] color ;
106
+ Color [] colors ;
103
107
int count ;
104
108
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
+ }
105
121
106
122
public LinearGradientPaint (float x1 , float y1 , float x2 , float y2 ,
107
123
float [] offset , int [] color , int count ,
108
- float opacity ) {
124
+ float opacity , AffineTransform xform ) {
109
125
this .x1 = x1 ;
110
126
this .y1 = y1 ;
111
127
this .x2 = x2 ;
@@ -114,6 +130,13 @@ public LinearGradientPaint(float x1, float y1, float x2, float y2,
114
130
this .color = color ;
115
131
this .count = count ;
116
132
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
+ }
117
140
}
118
141
119
142
public PaintContext createContext (ColorModel cm ,
@@ -125,6 +148,35 @@ public PaintContext createContext(ColorModel cm,
125
148
(float ) t2 .getX (), (float ) t2 .getY ());
126
149
}
127
150
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
+
128
180
public int getTransparency () {
129
181
return TRANSLUCENT ; // why not.. rather than checking each color
130
182
}
@@ -221,23 +273,43 @@ public Raster getRaster(int x, int y, int w, int h) {
221
273
}
222
274
223
275
224
- static class RadialGradientPaint implements Paint {
276
+ public static class RadialGradientPaint implements Paint {
225
277
float cx , cy , radius ;
226
278
float [] offset ;
227
279
int [] color ;
280
+ Color [] colors ;
228
281
int count ;
229
282
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
+ }
230
295
231
296
public RadialGradientPaint (float cx , float cy , float radius ,
232
297
float [] offset , int [] color , int count ,
233
- float opacity ) {
298
+ float opacity , AffineTransform xform ) {
234
299
this .cx = cx ;
235
300
this .cy = cy ;
236
301
this .radius = radius ;
237
302
this .offset = offset ;
238
303
this .color = color ;
239
304
this .count = count ;
240
305
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
+ }
241
313
}
242
314
243
315
public PaintContext createContext (ColorModel cm ,
@@ -246,6 +318,41 @@ public PaintContext createContext(ColorModel cm,
246
318
return new RadialGradientContext ();
247
319
}
248
320
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
+
249
356
public int getTransparency () {
250
357
return TRANSLUCENT ;
251
358
}
@@ -305,14 +412,14 @@ protected Paint calcGradientPaint(Gradient gradient) {
305
412
LinearGradient grad = (LinearGradient ) gradient ;
306
413
return new LinearGradientPaint (grad .x1 , grad .y1 , grad .x2 , grad .y2 ,
307
414
grad .offset , grad .color , grad .count ,
308
- opacity );
415
+ opacity , grad . transform );
309
416
310
417
} else if (gradient instanceof RadialGradient ) {
311
418
// System.out.println("creating radial gradient");
312
419
RadialGradient grad = (RadialGradient ) gradient ;
313
420
return new RadialGradientPaint (grad .cx , grad .cy , grad .r ,
314
421
grad .offset , grad .color , grad .count ,
315
- opacity );
422
+ opacity , grad . transform );
316
423
}
317
424
return null ;
318
425
}
0 commit comments