1 package org.newdawn.slick;
2
3 import java.io.Serializable;
4 import java.nio.FloatBuffer;
5
6 import org.newdawn.slick.opengl.renderer.Renderer;
7 import org.newdawn.slick.opengl.renderer.SGL;
8
9 import javax.annotation.Nonnull;
10
11
12
13
14
15
16 public class Color implements Serializable {
17
18 private static final long serialVersionUID = 1393939L;
19
20
21 private final transient SGL GL = Renderer.get();
22
23
24 public static final Color transparent = new Color(0.0f,0.0f,0.0f,0.0f);
25
26 public static final Color white = new Color(1.0f,1.0f,1.0f,1.0f);
27
28 public static final Color yellow = new Color(1.0f,1.0f,0,1.0f);
29
30 public static final Color red = new Color(1.0f,0,0,1.0f);
31
32 public static final Color blue = new Color(0,0,1.0f,1.0f);
33
34 public static final Color green = new Color(0,1.0f,0,1.0f);
35
36 public static final Color black = new Color(0,0,0,1.0f);
37
38 public static final Color gray = new Color(0.5f,0.5f,0.5f,1.0f);
39
40 public static final Color cyan = new Color(0,1.0f,1.0f,1.0f);
41
42 public static final Color darkGray = new Color(0.3f,0.3f,0.3f,1.0f);
43
44 public static final Color lightGray = new Color(0.7f,0.7f,0.7f,1.0f);
45
46 public final static Color pink = new Color(255, 175, 175, 255);
47
48 public final static Color orange = new Color(255, 200, 0, 255);
49
50 public final static Color magenta = new Color(255, 0, 255, 255);
51
52
53 public float r;
54
55 public float g;
56
57 public float b;
58
59 public float a = 1.0f;
60
61
62
63
64
65
66 public Color(@Nonnull Color color) {
67 r = color.r;
68 g = color.g;
69 b = color.b;
70 a = color.a;
71 }
72
73
74
75
76
77
78 public Color(@Nonnull FloatBuffer buffer) {
79 this.r = buffer.get();
80 this.g = buffer.get();
81 this.b = buffer.get();
82 this.a = buffer.get();
83 }
84
85
86
87
88
89
90
91
92 public Color(float r,float g,float b) {
93 this.r = r;
94 this.g = g;
95 this.b = b;
96 this.a = 1;
97 }
98
99
100
101
102
103
104
105
106
107 public Color(float r,float g,float b,float a) {
108 this.r = Math.min(r, 1);
109 this.g = Math.min(g, 1);
110 this.b = Math.min(b, 1);
111 this.a = Math.min(a, 1);
112 }
113
114
115
116
117
118
119
120
121 public Color(int r,int g,int b) {
122 this.r = r / 255.0f;
123 this.g = g / 255.0f;
124 this.b = b / 255.0f;
125 this.a = 1;
126 }
127
128
129
130
131
132
133
134
135
136 public Color(int r,int g,int b,int a) {
137 this.r = r / 255.0f;
138 this.g = g / 255.0f;
139 this.b = b / 255.0f;
140 this.a = a / 255.0f;
141 }
142
143
144
145
146
147
148
149
150 private Color(int value) {
151 int r = (value & 0x00FF0000) >> 16;
152 int g = (value & 0x0000FF00) >> 8;
153 int b = (value & 0x000000FF);
154 int a = (value & 0xFF000000) >> 24;
155
156 if (a < 0) {
157 a += 256;
158 }
159 if (a == 0) {
160 a = 255;
161 }
162
163 this.r = r / 255.0f;
164 this.g = g / 255.0f;
165 this.b = b / 255.0f;
166 this.a = a / 255.0f;
167 }
168
169
170
171
172
173
174
175
176 @Nonnull
177 public static Color decode(String nm) {
178 return new Color(Integer.decode(nm));
179 }
180
181
182
183
184 public void bind() {
185 GL.glColor4f(r,g,b,a);
186 }
187
188
189
190
191 public int hashCode() {
192 return ((int) (r+g+b+a)*255);
193 }
194
195
196
197
198 public boolean equals(Object other) {
199 if (other instanceof Color) {
200 Color o = (Color) other;
201 return ((o.r == r) && (o.g == g) && (o.b == b) && (o.a == a));
202 }
203
204 return false;
205 }
206
207
208
209
210 @Nonnull
211 public String toString() {
212 return "Color ("+r+","+g+","+b+","+a+")";
213 }
214
215
216
217
218
219
220 @Nonnull
221 public Color darker() {
222 return darker(0.5f);
223 }
224
225
226
227
228
229
230
231 @Nonnull Color darker(float scale) {
232 scale = 1 - scale;
233 Color temp = new Color(r * scale,g * scale,b * scale,a);
234
235 return temp;
236 }
237
238
239
240
241
242
243 @Nonnull
244 public Color brighter() {
245 return brighter(0.2f);
246 }
247
248
249
250
251
252
253 public int getRed() {
254 return (int) (r * 255);
255 }
256
257
258
259
260
261
262 public int getGreen() {
263 return (int) (g * 255);
264 }
265
266
267
268
269
270
271 public int getBlue() {
272 return (int) (b * 255);
273 }
274
275
276
277
278
279
280 public int getAlpha() {
281 return (int) (a * 255);
282 }
283
284
285
286
287
288
289 public int getRedByte() {
290 return (int) (r * 255);
291 }
292
293
294
295
296
297
298 public int getGreenByte() {
299 return (int) (g * 255);
300 }
301
302
303
304
305
306
307 public int getBlueByte() {
308 return (int) (b * 255);
309 }
310
311
312
313
314
315
316 public int getAlphaByte() {
317 return (int) (a * 255);
318 }
319
320
321
322
323
324
325
326 @Nonnull
327 Color brighter(float scale) {
328 scale += 1;
329 Color temp = new Color(r * scale,g * scale,b * scale,a);
330
331 return temp;
332 }
333
334
335
336
337
338
339
340 @Nonnull
341 public Color multiply(@Nonnull Color c) {
342 return new Color(r * c.r, g * c.g, b * c.b, a * c.a);
343 }
344
345
346
347
348
349
350 public void add(@Nonnull Color c) {
351 r += c.r;
352 g += c.g;
353 b += c.b;
354 a += c.a;
355 }
356
357
358
359
360
361
362 public void scale(float value) {
363 r *= value;
364 g *= value;
365 b *= value;
366 a *= value;
367 }
368
369
370
371
372
373
374
375 @Nonnull
376 public Color addToCopy(@Nonnull Color c) {
377 Color copy = new Color(r,g,b,a);
378 copy.r += c.r;
379 copy.g += c.g;
380 copy.b += c.b;
381 copy.a += c.a;
382
383 return copy;
384 }
385
386
387
388
389
390
391
392 @Nonnull
393 public Color scaleCopy(float value) {
394 Color copy = new Color(r,g,b,a);
395 copy.r *= value;
396 copy.g *= value;
397 copy.b *= value;
398 copy.a *= value;
399
400 return copy;
401 }
402 }