source: trunk/src/org/expeditee/core/Colour.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 8.7 KB
Line 
1package org.expeditee.core;
2
3/**
4 * Class representing all colour information in Expeditee.
5 * Internally uses 15-bit channels, but has helper methods for converting
6 * to and from standard 8-bit channels.
7 *
8 * @author cts16
9 *
10 */
11public class Colour {
12
13 private short r, g, b, a;
14
15 public static final short COMPONENT_MAX_VALUE = Short.MAX_VALUE;
16 public static final short COMPONENT_MIN_VALUE = (short) 0;
17
18 // Constant colour definitions
19 public static final Colour WHITE = new Colour(COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE);
20 public static final Colour RED = new Colour(COMPONENT_MAX_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE);
21 public static final Colour GREEN = new Colour(COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE);
22 public static final Colour BLUE = new Colour(COMPONENT_MIN_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE);
23 public static final Colour CYAN = new Colour(COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE);
24 public static final Colour MAGENTA = new Colour(COMPONENT_MAX_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE);
25 public static final Colour YELLOW = new Colour(COMPONENT_MAX_VALUE, COMPONENT_MAX_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE);
26 public static final Colour BLACK = new Colour(COMPONENT_MIN_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MIN_VALUE, COMPONENT_MAX_VALUE);
27 public static final Colour GREY = FromRGBA255(128, 128, 128, 255);
28 public static final Colour DARK_GREY = FromRGBA255(64, 64, 64, 255);
29 public static final Colour LIGHT_GREY = FromRGBA255(192, 192, 192, 255);
30 public static final Colour ORANGE = FromRGBA255(255, 200, 0, 255);
31 public static final Colour PINK = FromRGBA255(255, 175, 175, 255);
32 public static final Colour TRANSPARENT = new Colour();
33
34 // Spelling translations of 'grey' to 'gray' to conform to AWT Color names
35 // (because these can be reflected and so might break something if not available)
36 public static final Colour GRAY = GREY;
37 public static final Colour DARK_GRAY = DARK_GREY;
38 public static final Colour LIGHT_GRAY = LIGHT_GREY;
39
40 /**
41 * Default construct gives transparent black.
42 */
43 public Colour()
44 {
45 this(0, 0, 0, 0);
46 }
47
48 /**
49 * Standard constructor taking each component value as a parameter.
50 *
51 * @param r Red channel
52 * @param g Green channel
53 * @param b Blue channel
54 * @param a Alpha channel
55 */
56 public Colour(short r, short g, short b, short a)
57 {
58 assert(COMPONENT_MIN_VALUE <= r && r <= COMPONENT_MAX_VALUE);
59 assert(COMPONENT_MIN_VALUE <= g && g <= COMPONENT_MAX_VALUE);
60 assert(COMPONENT_MIN_VALUE <= b && b <= COMPONENT_MAX_VALUE);
61 assert(COMPONENT_MIN_VALUE <= a && a <= COMPONENT_MAX_VALUE);
62
63 this.r = r;
64 this.g = g;
65 this.b = b;
66 this.a = a;
67 }
68
69 /** Clone constructor. */
70 public Colour(Colour other)
71 {
72 this(other.r, other.g, other.b, other.a);
73 }
74
75 /** Convenience constructor taking r,g,b,a as ints. */
76 public Colour(int r, int g, int b, int a)
77 {
78 this((short) r,(short) g,(short) b,(short) a);
79 }
80
81 /** Convenience constructor taking r,g,b,a as floats in the range 0.0f to 1.0f. */
82 public Colour(float r, float g, float b, float a)
83 {
84 this(FromComponentFloat(r), FromComponentFloat(g), FromComponentFloat(b), FromComponentFloat(a));
85 }
86
87 /** Constructor defaults alpha to completely opaque. */
88 public Colour(short r, short g, short b)
89 {
90 this(r, g, b, COMPONENT_MAX_VALUE);
91 }
92
93 /** Constructor defaults alpha to completely opaque. */
94 public Colour(int r, int g, int b)
95 {
96 this((short) r,(short) g,(short) b);
97 }
98
99 /** Constructor defaults alpha to completely opaque. */
100 public Colour(float r, float g, float b)
101 {
102 this(r, g, b, 1.0f);
103 }
104
105 /** Creates a colour from r,g,b,a values in the range 0 - 255. */
106 public static Colour FromRGBA255(int r, int g, int b, int a)
107 {
108 return new Colour(FromComponent255(r), FromComponent255(g), FromComponent255(b), FromComponent255(a));
109 }
110
111 /** Creates a colour from r,g,b values in the range 0 - 255. */
112 public static Colour FromRGB255(int r, int g, int b)
113 {
114 return new Colour(FromComponent255(r), FromComponent255(g), FromComponent255(b));
115 }
116
117 /**
118 * Converts a single component value from the range 0 - 255 into
119 * the internal colour range.
120 */
121 public static short FromComponent255(int component)
122 {
123 assert(0 <= component && component <= 255);
124
125 return (short) (component << 7);
126 }
127
128 /**
129 * Converts a single component value from the internal colour range into
130 * the range 0 - 255.
131 */
132 public static int ToComponent255(short component)
133 {
134 assert(COMPONENT_MIN_VALUE <= component && component <= COMPONENT_MAX_VALUE);
135
136 return ((int) component) >> 7;
137 }
138
139 /**
140 * Converts a single component value from the range 0.0f - 1.0f into
141 * the internal colour range.
142 */
143 public static short FromComponentFloat(float component)
144 {
145 assert(0.0f <= component && component <= 1.0f);
146
147 return (short) (component * COMPONENT_MAX_VALUE);
148 }
149
150 /**
151 * Converts a single component value from the internal colour range into
152 * the range 0.0f - 1.0f.
153 */
154 public static float ToComponentFloat(short component)
155 {
156 assert(COMPONENT_MIN_VALUE <= component && component <= COMPONENT_MAX_VALUE);
157
158 return ((float) component) / COMPONENT_MAX_VALUE;
159 }
160
161 @Override
162 public boolean equals(Object other)
163 {
164 if (other instanceof Colour)
165 {
166 Colour c = (Colour) other;
167 return (r == c.r && g == c.g && b == c.b && a == c.a);
168 }
169
170 return false;
171 }
172
173 /** Returns a new colour which is this colour made brighter. */
174 public Colour brighter() {
175 int r = getRed();
176 int g = getGreen();
177 int b = getBlue();
178 int alpha = getAlpha();
179
180 int i = FromComponent255((int)(1.0/0.7));
181 if ( r == 0 && g == 0 && b == 0) {
182 return new Colour(i, i, i, alpha);
183 }
184 if ( r > 0 && r < i ) r = i;
185 if ( g > 0 && g < i ) g = i;
186 if ( b > 0 && b < i ) b = i;
187
188 return new Colour(r, g, b, alpha).scale(1.0/0.7);
189 }
190
191 /** Returns a new colour which is this colour made darker. */
192 public Colour darker()
193 {
194 return scale(0.7);
195 }
196
197 /**
198 * Returns a new colour which is this colour with the r,g,b
199 * components by the given scale factor.
200 */
201 public Colour scale(double factor)
202 {
203 if (factor < 0.0) return null;
204
205 double newR = r * factor;
206 double newG = g * factor;
207 double newB = b * factor;
208
209 if (newR > COMPONENT_MAX_VALUE) newR = COMPONENT_MAX_VALUE;
210 if (newG > COMPONENT_MAX_VALUE) newG = COMPONENT_MAX_VALUE;
211 if (newB > COMPONENT_MAX_VALUE) newB = COMPONENT_MAX_VALUE;
212
213 return new Colour((short) newR, (short) newG, (short) newB, a);
214 }
215
216 public short getRed() { return r; }
217 public short getGreen() { return g; }
218 public short getBlue() { return b; }
219 public short getAlpha() { return a; }
220
221 public void setRed(short r) { this.r = r; }
222 public void setGreen(short g) { this.g = g; }
223 public void setBlue(short b) { this.b = b; }
224 public void setAlpha(short a) { this.a = a; }
225
226 public int getRed255() { return ToComponent255(r); }
227 public int getGreen255() { return ToComponent255(g); }
228 public int getBlue255() { return ToComponent255(b); }
229 public int getAlpha255() { return ToComponent255(a); }
230
231 public float getRedFloat() { return ToComponentFloat(r); }
232 public float getGreenFloat() { return ToComponentFloat(g); }
233 public float getBlueFloat() { return ToComponentFloat(b); }
234 public float getAlphaFloat() { return ToComponentFloat(a); }
235
236 /** Creates a new colour which is the inverse of this colour. */
237 public Colour inverse()
238 {
239 return new Colour(COMPONENT_MAX_VALUE - r, COMPONENT_MAX_VALUE - g, COMPONENT_MAX_VALUE - b, a);
240 }
241
242 /**
243 * Decodes a string into a colour. The format of the string should be
244 * a packed r,g,b integer value e.g. "0xrrggbb".
245 */
246 public static Colour decode(String nm) throws NumberFormatException {
247 Integer intval = Integer.decode(nm);
248 int i = intval.intValue();
249 return fromRGB24BitPacked(i);
250 }
251
252 /**
253 * Returns an integer representing this colour as a 32-bit packed
254 * integer i.e. 0xaarrggbb
255 */
256 public int getARGB32BitPacked()
257 {
258 return getBlue255() | (getGreen255() << 8) | (getRed255() << 16) | (getAlpha255() << 24);
259 }
260
261 /** Creates a colour from a 32-bit packed integer in to form 0xaarrggbb. */
262 public static Colour fromARGB32BitPacked(int rgba)
263 {
264 int a = (rgba >> 24) & 0xFF;
265 int r = (rgba >> 16) & 0xFF;
266 int g = (rgba >> 8) & 0xFF;
267 int b = rgba & 0xFF;
268
269 return FromRGBA255(r, g, b, a);
270 }
271
272 /** Creates a colour from a 24-bit packed integer in to form 0xrrggbb. */
273 public static Colour fromRGB24BitPacked(int rgb)
274 {
275 return fromARGB32BitPacked(rgb | 0xFF000000);
276 }
277
278 public Colour clone()
279 {
280 return new Colour(this);
281 }
282}
Note: See TracBrowser for help on using the repository browser.