Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/fills/GradientFill.java
1459 views
1
package org.newdawn.slick.fills;
2
3
import org.newdawn.slick.Color;
4
import org.newdawn.slick.ShapeFill;
5
import org.newdawn.slick.geom.Shape;
6
import org.newdawn.slick.geom.Vector2f;
7
8
/**
9
* A fill effect used to define gradients when filling and drawing shapes. A gradient is defined
10
* by two control points. Each point that is rendered is coloured based on it's proximity to the
11
* points. Note that the points are defined relative to the center of the shape being drawn. This
12
* is with the intention that the gradient fills can be used and do not need to be updated when
13
* the geometry is moved
14
*
15
* @author kevin
16
*/
17
public class GradientFill implements ShapeFill {
18
/** The contant offset */
19
private Vector2f none = new Vector2f(0,0);
20
/** The start position of the gradient */
21
private Vector2f start;
22
/** The end poisition of the gradient */
23
private Vector2f end;
24
/** The starting colour of the gradient */
25
private Color startCol;
26
/** The ending colour of the gradient */
27
private Color endCol;
28
/** True if the graident is defined in shape coordinates */
29
private boolean local = false;
30
31
/**
32
* Create a gradient fill
33
*
34
* @param sx The x coordinate of the starting control point
35
* @param sy The y coordinate of the starting control point
36
* @param startCol The colour to apply at the starting control point
37
* @param ex The x coordinate of the ending control point
38
* @param ey The y coordinate of the ending control point
39
* @param endCol The colour to apply at the ending control point
40
*/
41
public GradientFill(float sx, float sy, Color startCol, float ex, float ey, Color endCol)
42
{
43
this(sx,sy,startCol,ex,ey,endCol,false);
44
}
45
46
/**
47
* Create a gradient fill
48
*
49
* @param sx The x coordinate of the starting control point
50
* @param sy The y coordinate of the starting control point
51
* @param startCol The colour to apply at the starting control point
52
* @param ex The x coordinate of the ending control point
53
* @param ey The y coordinate of the ending control point
54
* @param endCol The colour to apply at the ending control point
55
* @param local True if the gradient is defined in local shape coordinates
56
*/
57
public GradientFill(float sx, float sy, Color startCol, float ex, float ey, Color endCol, boolean local)
58
{
59
this(new Vector2f(sx,sy), startCol, new Vector2f(ex,ey), endCol, local);
60
}
61
62
/**
63
* Create a gradient fill
64
*
65
* @param start The position of the starting control point
66
* @param startCol The colour to apply at the starting control point
67
* @param end The position of the ending control point
68
* @param endCol The colour to apply at the ending control point
69
* @param local True if the gradient is defined in local shape coordinates
70
*/
71
public GradientFill(Vector2f start, Color startCol, Vector2f end, Color endCol, boolean local) {
72
this.start = new Vector2f(start);
73
this.end = new Vector2f(end);
74
this.startCol = new Color(startCol);
75
this.endCol = new Color(endCol);
76
this.local = local;
77
}
78
79
/**
80
* Get an inverted copy of the gradient
81
*
82
* @return The copy with the colours inverted
83
*/
84
public GradientFill getInvertedCopy() {
85
return new GradientFill(start, endCol, end, startCol, local);
86
}
87
88
/**
89
* Indicate if the gradient is defined in shape local coordinates
90
*
91
* @param local True if the gradient is defined in shape local coordinates
92
*/
93
public void setLocal(boolean local) {
94
this.local = local;
95
}
96
97
/**
98
* Get the position of the start control point
99
*
100
* @return The position of the start control point
101
*/
102
public Vector2f getStart() {
103
return start;
104
}
105
106
/**
107
* Get the position of the end control point
108
*
109
* @return The position of the end control point
110
*/
111
public Vector2f getEnd() {
112
return end;
113
}
114
115
/**
116
* Get the colour at the start control point
117
*
118
* @return The color at the start control point
119
*/
120
public Color getStartColor() {
121
return startCol;
122
}
123
124
/**
125
* Get the colour at the end control point
126
*
127
* @return The color at the end control point
128
*/
129
public Color getEndColor() {
130
return endCol;
131
}
132
133
/**
134
* Set the start point's position
135
*
136
* @param x The x coordinate of the start control point
137
* @param y The y coordinate of the start control point
138
*/
139
public void setStart(float x, float y) {
140
setStart(new Vector2f(x,y));
141
}
142
143
/**
144
* Set the start control point's position
145
*
146
* @param start The new poisition for the start point
147
*/
148
public void setStart(Vector2f start) {
149
this.start = new Vector2f(start);
150
}
151
152
/**
153
* Set the end control point's position
154
*
155
* @param x The x coordinate of the end control point
156
* @param y The y coordinate of the end control point
157
*/
158
public void setEnd(float x, float y) {
159
setEnd(new Vector2f(x,y));
160
}
161
162
/**
163
* Set the end control point's position
164
*
165
* @param end The new position for the end point
166
*/
167
public void setEnd(Vector2f end) {
168
this.end = new Vector2f(end);
169
}
170
171
/**
172
* Set the colour to apply at the start control's position
173
*
174
* @param color The colour to apply at the start control point
175
*/
176
public void setStartColor(Color color) {
177
this.startCol = new Color(color);
178
}
179
180
/**
181
* Set the colour to apply at the end control's position
182
*
183
* @param color The colour to apply at the end control point
184
*/
185
public void setEndColor(Color color) {
186
this.endCol = new Color(color);
187
}
188
189
/**
190
* Get the colour that should be applied at the specified location
191
*
192
* @param shape The shape being filled
193
* @param x The x coordinate of the point being coloured
194
* @param y The y coordinate of the point being coloured
195
* @return The colour that should be applied based on the control points of this gradient
196
*/
197
public Color colorAt(Shape shape, float x, float y) {
198
if (local) {
199
return colorAt(x-shape.getCenterX(),y-shape.getCenterY());
200
} else {
201
return colorAt(x,y);
202
}
203
}
204
205
/**
206
* Get the colour that should be applied at the specified location
207
*
208
* @param x The x coordinate of the point being coloured
209
* @param y The y coordinate of the point being coloured
210
* @return The colour that should be applied based on the control points of this gradient
211
*/
212
public Color colorAt(float x, float y) {
213
float dx1 = end.getX() - start.getX();
214
float dy1 = end.getY() - start.getY();
215
216
float dx2 = -dy1;
217
float dy2 = dx1;
218
float denom = (dy2 * dx1) - (dx2 * dy1);
219
220
if (denom == 0) {
221
return Color.black;
222
}
223
224
float ua = (dx2 * (start.getY() - y)) - (dy2 * (start.getX() - x));
225
ua /= denom;
226
float ub = (dx1 * (start.getY() - y)) - (dy1 * (start.getX() - x));
227
ub /= denom;
228
float u = ua;
229
if (u < 0) {
230
u = 0;
231
}
232
if (u > 1) {
233
u = 1;
234
}
235
float v = 1 - u;
236
237
// u is the proportion down the line we are
238
Color col = new Color(1,1,1,1);
239
col.r = (u * endCol.r) + (v * startCol.r);
240
col.b = (u * endCol.b) + (v * startCol.b);
241
col.g = (u * endCol.g) + (v * startCol.g);
242
col.a = (u * endCol.a) + (v * startCol.a);
243
244
return col;
245
}
246
247
/**
248
* @see org.newdawn.slick.ShapeFill#getOffsetAt(org.newdawn.slick.geom.Shape, float, float)
249
*/
250
public Vector2f getOffsetAt(Shape shape, float x, float y) {
251
return none;
252
}
253
}
254
255