Oolite
Loading...
Searching...
No Matches
OOColor.m
Go to the documentation of this file.
1/*
2
3OOColor.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOColor.h"
27#import "OOMaths.h"
28
29@implementation OOColor
30
31// Set methods are internal, because OOColor is immutable (as seen from outside).
32- (void)setRed:(float)r green:(float)g blue:(float)b alpha:(float)a
33{
34 rgba[0] = r;
35 rgba[1] = g;
36 rgba[2] = b;
37 rgba[3] = a;
38}
39
40- (void)setHue:(float)h saturation:(float)s brightness:(float)b alpha:(float)a
41{
42 rgba[3] = a;
43 if (s == 0.0f) {
44 rgba[0] = rgba[1] = rgba[2] = b;
45 return;
46 }
47 float f, p, q, t;
48 int i;
49 h = fmod(h, 360.0f);
50 if (h < 0.0)
51 h += 360.0f;
52 h /= 60.0f;
53
54 i = floor(h);
55 f = h - i;
56 p = b * (1.0f - s);
57 q = b * (1.0f - (s * f));
58 t = b * (1.0f - (s * (1.0f - f)));
59
60 switch (i) {
61 case 0:
62 rgba[0] = b;
63 rgba[1] = t;
64 rgba[2] = p;
65 break;
66 case 1:
67 rgba[0] = q;
68 rgba[1] = b;
69 rgba[2] = p;
70 break;
71 case 2:
72 rgba[0] = p;
73 rgba[1] = b;
74 rgba[2] = t;
75 break;
76 case 3:
77 rgba[0] = p;
78 rgba[1] = q;
79 rgba[2] = b;
80 break;
81 case 4:
82 rgba[0] = t;
83 rgba[1] = p;
84 rgba[2] = b;
85 break;
86 case 5:
87 rgba[0] = b;
88 rgba[1] = p;
89 rgba[2] = q;
90 break;
91 }
92}
93
94- (id)copyWithZone:(NSZone*)zone
95{
96 // Copy is implemented as retain since OOColor is immutable.
97 return [self retain];
98}
99
100+ (OOColor*)colorWithHue:(float)hue saturation:(float)saturation brightness:(float)brightness alpha:(float)alpha
101{
102 OOColor* result = [[OOColor alloc] init];
103 [result setHue:360.0f * hue saturation:saturation brightness:brightness alpha:alpha];
104 return [result autorelease];
105}
106
107+ (OOColor*)colorWithRed:(float)red green:(float)green blue:(float)blue alpha:(float)alpha
108{
109 OOColor* result = [[OOColor alloc] init];
110 [result setRed:red green:green blue:blue alpha:alpha];
111 return [result autorelease];
112}
113
114+ (OOColor*)colorWithWhite:(float)white alpha:(float)alpha
115{
116 return [OOColor colorWithRed:white green:white blue:white alpha:alpha];
117}
118
119+ (OOColor*)colorWithRGBAComponents:(OORGBAComponents)components
120{
121 return [self colorWithRed:components.r
122 green:components.g
123 blue:components.b
124 alpha:components.a];
125}
126
127+ (OOColor*)colorWithHSBAComponents:(OOHSBAComponents)components
128{
129 return [self colorWithHue:components.h / 360.0f
130 saturation:components.s
131 brightness:components.b
132 alpha:components.a];
133}
134
135+ (OOColor*)colorWithDescription:(id)description
136{
137 return [self colorWithDescription:description saturationFactor:1.0f];
138}
139
140+ (OOColor*)colorWithDescription:(id)description saturationFactor:(float)factor
141{
142 NSDictionary* dict = nil;
143 OOColor* result = nil;
144
145 if (description == nil)
146 return nil;
147
148 if ([description isKindOfClass:[OOColor class]]) {
149 result = [[description copy] autorelease];
150 } else if ([description isKindOfClass:[NSString class]]) {
151 if ([description hasSuffix:@"Color"]) {
152 // +fooColor selector
153 SEL selector = NSSelectorFromString(description);
154 if ([self respondsToSelector:selector])
155 result = [self performSelector:selector];
156 } else {
157 // Some other string
158 result = [self colorFromString:description];
159 }
160 } else if ([description isKindOfClass:[NSArray class]]) {
161 result = [self colorFromString:[description componentsJoinedByString:@" "]];
162 } else if ([description isKindOfClass:[NSDictionary class]]) {
163 dict = description; // Workaround for gnu-gcc's more agressive "multiple methods named..." warnings.
164
165 if ([dict objectForKey:@"hue"] != nil) {
166 // Treat as HSB(A) dictionary
167 float h = [dict oo_floatForKey:@"hue"];
168 float s = [dict oo_floatForKey:@"saturation" defaultValue:1.0f];
169 float b = [dict oo_floatForKey:@"brightness" defaultValue:-1.0f];
170 if (b < 0.0f)
171 b = [dict oo_floatForKey:@"value" defaultValue:1.0f];
172 float a = [dict oo_floatForKey:@"alpha" defaultValue:-1.0f];
173 if (a < 0.0f)
174 a = [dict oo_floatForKey:@"opacity" defaultValue:1.0f];
175
176 // Not "result =", because we handle the saturation scaling here to allow oversaturation.
177 return [OOColor colorWithHue:h / 360.0f saturation:s * factor brightness:b alpha:a];
178 } else {
179 // Treat as RGB(A) dictionary
180 float r = [dict oo_floatForKey:@"red"];
181 float g = [dict oo_floatForKey:@"green"];
182 float b = [dict oo_floatForKey:@"blue"];
183 float a = [dict oo_floatForKey:@"alpha" defaultValue:-1.0f];
184 if (a < 0.0f)
185 a = [dict oo_floatForKey:@"opacity" defaultValue:1.0f];
186
187 result = [OOColor colorWithRed:r green:g blue:b alpha:a];
188 }
189 }
190
191 if (factor != 1.0f && result != nil) {
192 float h, s, b, a;
194 h *= 1.0 / 360.0f; // See note in header.
195 s *= factor;
196 result = [self colorWithHue:h saturation:s brightness:b alpha:a];
197 }
198
199 return result;
200}
201
202+ (OOColor*)brightColorWithDescription:(id)description
203{
204 OOColor* color = [OOColor colorWithDescription:description];
205 if (color == nil || 0.5f <= [color brightnessComponent])
206 return color;
207
209}
210
211+ (OOColor*)colorFromString:(NSString*)colorFloatString
212{
213 float rgbaValue[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
214 NSScanner* scanner = [NSScanner scannerWithString:colorFloatString];
215 float factor = 1.0f;
216 int i;
217
218 for (i = 0; i != 4; ++i) {
219 if (![scanner scanFloat:&rgbaValue[i]]) {
220 // Less than three floats or non-float, can't parse -> quit
221 if (i < 3)
222 return nil;
223
224 // If we get here, we only got three components. Make sure alpha is at correct scale:
225 rgbaValue[3] /= factor;
226 }
227 if (1.0f < rgbaValue[i])
228 factor = 1.0f / 255.0f;
229 }
230
231 return [OOColor colorWithRed:rgbaValue[0] * factor green:rgbaValue[1] * factor blue:rgbaValue[2] * factor alpha:rgbaValue[3] * factor];
232}
233
234+ (OOColor*)blackColor // 0.0 white
235{
236 return [OOColor colorWithWhite:0.0f alpha:1.0f];
237}
238
239+ (OOColor*)darkGrayColor // 0.333 white
240{
241 return [OOColor colorWithWhite:1.0f / 3.0f alpha:1.0f];
242}
243
244+ (OOColor*)lightGrayColor // 0.667 white
245{
246 return [OOColor colorWithWhite:2.0f / 3.0f alpha:1.0f];
247}
248
249+ (OOColor*)whiteColor // 1.0 white
250{
251 return [OOColor colorWithWhite:1.0f alpha:1.0f];
252}
253
254+ (OOColor*)grayColor // 0.5 white
255{
256 return [OOColor colorWithWhite:0.5f alpha:1.0f];
257}
258
259+ (OOColor*)redColor // 1.0, 0.0, 0.0 RGB
260{
261 return [OOColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
262}
263
264+ (OOColor*)greenColor // 0.0, 1.0, 0.0 RGB
265{
266 return [OOColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:1.0f];
267}
268
269+ (OOColor*)blueColor // 0.0, 0.0, 1.0 RGB
270{
271 return [OOColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f];
272}
273
274+ (OOColor*)cyanColor // 0.0, 1.0, 1.0 RGB
275{
276 return [OOColor colorWithRed:0.0f green:1.0f blue:1.0f alpha:1.0f];
277}
278
279+ (OOColor*)yellowColor // 1.0, 1.0, 0.0 RGB
280{
281 return [OOColor colorWithRed:1.0f green:1.0f blue:0.0f alpha:1.0f];
282}
283
284+ (OOColor*)magentaColor // 1.0, 0.0, 1.0 RGB
285{
286 return [OOColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f];
287}
288
289+ (OOColor*)orangeColor // 1.0, 0.5, 0.0 RGB
290{
291 return [OOColor colorWithRed:1.0f green:0.5f blue:0.0f alpha:1.0f];
292}
293
294+ (OOColor*)purpleColor // 0.5, 0.0, 0.5 RGB
295{
296 return [OOColor colorWithRed:0.5f green:0.0f blue:0.5f alpha:1.0f];
297}
298
299+ (OOColor*)brownColor // 0.6, 0.4, 0.2 RGB
300{
301 return [OOColor colorWithRed:0.6f green:0.4f blue:0.2f alpha:1.0f];
302}
303
304+ (OOColor*)clearColor // 0.0 white, 0.0 alpha
305{
306 return [OOColor colorWithWhite:0.0f alpha:0.0f];
307}
308
309- (OOColor*)blendedColorWithFraction:(float)fraction ofColor:(OOColor*)color
310{
311 float rgba1[4];
312 [color getRed:&rgba1[0] green:&rgba1[1] blue:&rgba1[2] alpha:&rgba1[3]];
313
314 OOColor* result = [[OOColor alloc] init];
315 [result setRed:OOLerp(rgba[0], rgba1[0], fraction)
316 green:OOLerp(rgba[1], rgba1[1], fraction)
317 blue:OOLerp(rgba[2], rgba1[2], fraction)
318 alpha:OOLerp(rgba[3], rgba1[3], fraction)];
319
320 return [result autorelease];
321}
322
324{
325 return [NSString stringWithFormat:@"%g, %g, %g, %g", rgba[0], rgba[1], rgba[2], rgba[3]];
326}
327
328// Get the red, green, or blue components.
330{
331 return rgba[0];
332}
333
335{
336 return rgba[1];
337}
338
340{
341 return rgba[2];
342}
343
344- (void)getRed:(float*)red green:(float*)green blue:(float*)blue alpha:(float*)alpha
345{
346 NSParameterAssert(red != NULL && green != NULL && blue != NULL && alpha != NULL);
347
348 *red = rgba[0];
349 *green = rgba[1];
350 *blue = rgba[2];
351 *alpha = rgba[3];
352}
353
355{
356 OORGBAComponents c = { rgba[0], rgba[1], rgba[2], rgba[3] };
357 return c;
358}
359
360- (BOOL)isBlack
361{
362 return rgba[0] == 0.0f && rgba[1] == 0.0f && rgba[2] == 0.0f;
363}
364
365- (BOOL)isWhite
366{
367 return rgba[0] == 1.0f && rgba[1] == 1.0f && rgba[2] == 1.0f && rgba[3] == 1.0f;
368}
369
370// Get the components as hue, saturation, or brightness.
372{
373 float maxrgb = (rgba[0] > rgba[1]) ? ((rgba[0] > rgba[2]) ? rgba[0] : rgba[2]) : ((rgba[1] > rgba[2]) ? rgba[1] : rgba[2]);
374 float minrgb = (rgba[0] < rgba[1]) ? ((rgba[0] < rgba[2]) ? rgba[0] : rgba[2]) : ((rgba[1] < rgba[2]) ? rgba[1] : rgba[2]);
375 float delta = maxrgb - minrgb + 0.0001f;
376 float fRed = rgba[0], fGreen = rgba[1], fBlue = rgba[2];
377 float hue = 0.0f;
378 if (maxrgb == fRed && fGreen >= fBlue) {
379 hue = 60.0f * (fGreen - fBlue) / delta;
380 } else if (maxrgb == fRed && fGreen < fBlue) {
381 hue = 60.0f * (fGreen - fBlue) / delta + 360.0f;
382 } else if (maxrgb == fGreen) {
383 hue = 60.0f * (fBlue - fRed) / delta + 120.0f;
384 } else if (maxrgb == fBlue) {
385 hue = 60.0f * (fRed - fGreen) / delta + 240.0f;
386 }
387 return hue;
388}
389
391{
392 float maxrgb = (rgba[0] > rgba[1]) ? ((rgba[0] > rgba[2]) ? rgba[0] : rgba[2]) : ((rgba[1] > rgba[2]) ? rgba[1] : rgba[2]);
393 float minrgb = (rgba[0] < rgba[1]) ? ((rgba[0] < rgba[2]) ? rgba[0] : rgba[2]) : ((rgba[1] < rgba[2]) ? rgba[1] : rgba[2]);
394 return maxrgb == 0.0f ? 0.0f : (1.0f - (minrgb / maxrgb));
395}
396
398{
399 float maxrgb = (rgba[0] > rgba[1]) ? ((rgba[0] > rgba[2]) ? rgba[0] : rgba[2]) : ((rgba[1] > rgba[2]) ? rgba[1] : rgba[2]);
400 return maxrgb;
401}
402
403- (void)getHue:(float*)hue saturation:(float*)saturation brightness:(float*)brightness alpha:(float*)alpha
404{
405 NSParameterAssert(hue != NULL && saturation != NULL && brightness != NULL && alpha != NULL);
406
407 *alpha = rgba[3];
408
409 float fRed = rgba[0], fGreen = rgba[1], fBlue = rgba[2];
410 float maxrgb = fmax(fRed, fmax(fGreen, fBlue));
411 float minrgb = fmin(fRed, fmin(fGreen, fBlue));
412 float delta = maxrgb - minrgb + 0.0001f;
413 float h = 0.0f;
414 if (maxrgb == fRed && fGreen >= fBlue) {
415 h = 60.0f * (fGreen - fBlue) / delta;
416 } else if (maxrgb == fRed && fGreen < fBlue) {
417 h = 60.0f * (fGreen - fBlue) / delta + 360.0f;
418 } else if (maxrgb == fGreen) {
419 h = 60.0f * (fBlue - fRed) / delta + 120.0f;
420 } else if (maxrgb == fBlue) {
421 h = 60.0f * (fRed - fGreen) / delta + 240.0f;
422 }
423
424 float s = (maxrgb == 0.0f) ? 0.0f : (1.0f - (minrgb / maxrgb));
425
426 *hue = h;
427 *saturation = s;
428 *brightness = maxrgb;
429}
430
432{
434 [self getHue:&c.h
435 saturation:&c.s
436 brightness:&c.b
437 alpha:&c.a];
438 return c;
439}
440
441// Get the alpha component.
443{
444 return rgba[3];
445}
446
448{
449 if (rgba[3] == 1.0f)
450 return [[self retain] autorelease];
451 return [OOColor colorWithRed:rgba[0] * rgba[3]
452 green:rgba[1] * rgba[3]
453 blue:rgba[2] * rgba[3]
454 alpha:1.0f];
455}
456
457- (OOColor*)colorWithBrightnessFactor:(float)factor
458{
459 return [OOColor colorWithRed:OOClamp_0_1_f(rgba[0] * factor)
460 green:OOClamp_0_1_f(rgba[1] * factor)
461 blue:OOClamp_0_1_f(rgba[2] * factor)
462 alpha:rgba[3]];
463}
464
466{
467 float r, g, b, a;
468 [self getRed:&r green:&g blue:&b alpha:&a];
469 return [NSArray arrayWithObjects:
470 [NSNumber numberWithFloat:r],
471 [NSNumber numberWithFloat:g],
472 [NSNumber numberWithFloat:b],
473 [NSNumber numberWithFloat:a],
474 nil];
475}
476
477- (NSString*)rgbaDescription
478{
480}
481
482- (NSString*)hsbaDescription
483{
485}
486
487@end
488
490{
491 return [NSString stringWithFormat:@"{%.3g, %.3g, %.3g, %.3g}", components.r, components.g, components.b, components.a];
492}
493
495{
496 return [NSString stringWithFormat:@"{%i, %.3g, %.3g, %.3g}", (int)components.h, components.s, components.b, components.a];
497}
NSString * OORGBAComponentsDescription(OORGBAComponents components)
Definition OOColor.m:489
NSString * OOHSBAComponentsDescription(OOHSBAComponents components)
Definition OOColor.m:494
NSString * OORGBAComponentsDescription(OORGBAComponents components)
Definition OOColor.m:489
NSString * OOHSBAComponentsDescription(OOHSBAComponents components)
Definition OOColor.m:494
return nil
void getHue:saturation:brightness:alpha:(float *hue,[saturation] float *saturation,[brightness] float *brightness,[alpha] float *alpha)
Definition OOColor.m:403
BOOL isBlack()
Definition OOColor.m:360
OOColor * cyanColor()
Definition OOColor.m:274
NSString * descriptionComponents()
Definition OOColor.m:323
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
Definition OOColor.m:107
OOColor * colorWithWhite:alpha:(float white,[alpha] float alpha)
Definition OOColor.m:114
void setHue:saturation:brightness:alpha:(float h, [saturation] float s, [brightness] float b, [alpha] float a)
Definition OOColor.m:40
float alphaComponent()
Definition OOColor.m:442
OOColor * darkGrayColor()
Definition OOColor.m:239
NSString * rgbaDescription()
Definition OOColor.m:477
float saturationComponent()
Definition OOColor.m:390
OOColor * colorFromString:(NSString *colorFloatString)
Definition OOColor.m:211
float blueComponent()
Definition OOColor.m:339
OOColor * orangeColor()
Definition OOColor.m:289
BOOL isWhite()
Definition OOColor.m:365
OOColor * colorWithDescription:(id description)
Definition OOColor.m:135
OOColor * redColor()
Definition OOColor.m:259
OOColor * greenColor()
Definition OOColor.m:264
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
Definition OOColor.m:344
OOColor * blueColor()
Definition OOColor.m:269
OOColor * clearColor()
Definition OOColor.m:304
NSArray * normalizedArray()
Definition OOColor.m:465
float hueComponent()
Definition OOColor.m:371
OOColor * grayColor()
Definition OOColor.m:254
OOColor * whiteColor()
Definition OOColor.m:249
OOColor * colorWithHue:saturation:brightness:alpha:(float hue,[saturation] float saturation,[brightness] float brightness,[alpha] float alpha)
Definition OOColor.m:100
void setRed:green:blue:alpha:(float r, [green] float g, [blue] float b, [alpha] float a)
Definition OOColor.m:32
OOColor * brownColor()
Definition OOColor.m:299
float redComponent()
Definition OOColor.m:329
OOColor * premultipliedColor()
Definition OOColor.m:447
NSString * hsbaDescription()
Definition OOColor.m:482
OOHSBAComponents hsbaComponents()
Definition OOColor.m:431
OOColor * yellowColor()
Definition OOColor.m:279
OOColor * lightGrayColor()
Definition OOColor.m:244
OOColor * magentaColor()
Definition OOColor.m:284
OOColor * blackColor()
Definition OOColor.m:234
OORGBAComponents rgbaComponents()
Definition OOColor.m:354
float rgba[4]
Definition OOColor.h:43
float greenComponent()
Definition OOColor.m:334
OOColor * purpleColor()
Definition OOColor.m:294
float brightnessComponent()
Definition OOColor.m:397
OOColor * colorWithDescription:saturationFactor:(id description,[saturationFactor] float factor)
Definition OOColor.m:140