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

Last change on this file since 1536 was 1536, checked in by bnemhaus, 4 years ago

Added toString function to expeditee.core.Colour for printing purposes

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