33- (void) setRed:(
float)r green:(
float)g blue:(
float)b alpha:(
float)a
42- (void) setHue:(
float)h saturation:(
float)s brightness:(
float)b alpha:(
float)a
53 if (h < 0.0) h += 360.0f;
59 q = b * (1.0f - (s * f));
60 t = b * (1.0f - (s * (1.0f - f)));
80- (id) copyWithZone:(NSZone *)zone
87+ (
OOColor *) colorWithHue:(
float)hue saturation:(
float)saturation brightness:(
float)brightness alpha:(
float)alpha
91 return [result autorelease];
95+ (
OOColor *) colorWithRed:(
float)red green:(
float)green blue:(
float)blue alpha:(
float)alpha
99 return [result autorelease];
103+ (
OOColor *) colorWithWhite:(
float)white alpha:(
float)alpha
111 return [
self colorWithRed:components.r
120 return [
self colorWithHue:components.h / 360.0f
121 saturation:components.s
122 brightness:components.b
127+ (
OOColor *) colorWithDescription:(
id)description
129 return [
self colorWithDescription:description saturationFactor:1.0f];
133+ (
OOColor *) colorWithDescription:(
id)description saturationFactor:(
float)factor
135 NSDictionary *dict =
nil;
138 if (description ==
nil)
return nil;
140 if ([description isKindOfClass:[
OOColor class]])
142 result = [[description copy] autorelease];
144 else if ([description isKindOfClass:[NSString
class]])
146 if ([description hasSuffix:
@"Color"])
149 SEL selector = NSSelectorFromString(description);
150 if ([
self respondsToSelector:selector]) result = [
self performSelector:selector];
155 result = [
self colorFromString:description];
158 else if ([description isKindOfClass:[NSArray
class]])
160 result = [
self colorFromString:[description componentsJoinedByString:@" "]];
162 else if ([description isKindOfClass:[NSDictionary
class]])
166 if ([dict objectForKey:
@"hue"] !=
nil)
169 float h = [dict oo_floatForKey:@"hue"];
170 float s = [dict oo_floatForKey:@"saturation" defaultValue:1.0f];
171 float b = [dict oo_floatForKey:@"brightness" defaultValue:-1.0f];
172 if (b < 0.0f) b = [dict oo_floatForKey:@"value" defaultValue:1.0f];
173 float a = [dict oo_floatForKey:@"alpha" defaultValue:-1.0f];
174 if (a < 0.0f) a = [dict oo_floatForKey:@"opacity" defaultValue:1.0f];
177 return [
OOColor colorWithHue:h / 360.0f saturation:s * factor brightness:b alpha:a];
182 float r = [dict oo_floatForKey:@"red"];
183 float g = [dict oo_floatForKey:@"green"];
184 float b = [dict oo_floatForKey:@"blue"];
185 float a = [dict oo_floatForKey:@"alpha" defaultValue:-1.0f];
186 if (a < 0.0f) a = [dict oo_floatForKey:@"opacity" defaultValue:1.0f];
188 result = [
OOColor colorWithRed:r green:g blue:b alpha:a];
192 if (factor != 1.0f && result !=
nil)
198 result = [
self colorWithHue:h saturation:s brightness:b alpha:a];
205+ (
OOColor *) brightColorWithDescription:(
id)description
214+ (
OOColor *) colorFromString:(NSString*) colorFloatString
216 float rgbaValue[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
217 NSScanner *scanner = [NSScanner scannerWithString:colorFloatString];
221 for (i = 0; i != 4; ++i)
223 if (![scanner scanFloat:&rgbaValue[i]])
226 if (i < 3)
return nil;
229 rgbaValue[3] /= factor;
231 if (1.0f < rgbaValue[i]) factor = 1.0f / 255.0f;
234 return [
OOColor colorWithRed:rgbaValue[0] * factor green:rgbaValue[1] * factor blue:rgbaValue[2] * factor alpha:rgbaValue[3] * factor];
240 return [
OOColor colorWithWhite:0.0f alpha:1.0f];
246 return [
OOColor colorWithWhite:1.0f/3.0f alpha:1.0f];
252 return [
OOColor colorWithWhite:2.0f/3.0f alpha:1.0f];
258 return [
OOColor colorWithWhite:1.0f alpha:1.0f];
264 return [
OOColor colorWithWhite:0.5f alpha:1.0f];
270 return [
OOColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:1.0f];
276 return [
OOColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:1.0f];
282 return [
OOColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:1.0f];
288 return [
OOColor colorWithRed:0.0f green:1.0f blue:1.0f alpha:1.0f];
294 return [
OOColor colorWithRed:1.0f green:1.0f blue:0.0f alpha:1.0f];
300 return [
OOColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:1.0f];
306 return [
OOColor colorWithRed:1.0f green:0.5f blue:0.0f alpha:1.0f];
328- (
OOColor *) blendedColorWithFraction:(
float)fraction ofColor:(
OOColor *)color
334 [result
setRed:OOLerp(rgba[0], rgba1[0], fraction)
335 green:OOLerp(rgba[1], rgba1[1], fraction)
336 blue:OOLerp(rgba[2], rgba1[2], fraction)
337 alpha:OOLerp(rgba[3], rgba1[3], fraction)];
339 return [result autorelease];
345 return [NSString stringWithFormat:@"%g, %g, %g, %g", rgba[0], rgba[1], rgba[2], rgba[3]];
368- (void) getRed:(
float *)red green:(
float *)green blue:(
float *)blue alpha:(
float *)alpha
370 NSParameterAssert(red != NULL && green != NULL && blue != NULL && alpha != NULL);
388 return rgba[0] == 0.0f &&
rgba[1] == 0.0f &&
rgba[2] == 0.0f;
394 return rgba[0] == 1.0f &&
rgba[1] == 1.0f &&
rgba[2] == 1.0f &&
rgba[3] == 1.0f;
403 float delta = maxrgb - minrgb + 0.0001f;
404 float fRed =
rgba[0], fGreen =
rgba[1], fBlue =
rgba[2];
406 if (maxrgb == fRed && fGreen >= fBlue)
408 hue = 60.0f * (fGreen - fBlue) / delta;
410 else if (maxrgb == fRed && fGreen < fBlue)
412 hue = 60.0f * (fGreen - fBlue) / delta + 360.0f;
414 else if (maxrgb == fGreen)
416 hue = 60.0f * (fBlue - fRed) / delta + 120.0f;
418 else if (maxrgb == fBlue)
420 hue = 60.0f * (fRed - fGreen) / delta + 240.0f;
429 return maxrgb == 0.0f ? 0.0f : (1.0f - (minrgb / maxrgb));
438- (void) getHue:(
float *)hue saturation:(
float *)saturation brightness:(
float *)brightness alpha:(
float *)alpha
440 NSParameterAssert(hue != NULL && saturation != NULL && brightness != NULL && alpha != NULL);
444 float fRed =
rgba[0], fGreen =
rgba[1], fBlue =
rgba[2];
445 float maxrgb = fmax(fRed, fmax(fGreen, fBlue));
446 float minrgb = fmin(fRed, fmin(fGreen, fBlue));
447 float delta = maxrgb - minrgb + 0.0001f;
449 if (maxrgb == fRed && fGreen >= fBlue)
451 h = 60.0f * (fGreen - fBlue) / delta;
453 else if (maxrgb == fRed && fGreen < fBlue)
455 h = 60.0f * (fGreen - fBlue) / delta + 360.0f;
457 else if (maxrgb == fGreen)
459 h = 60.0f * (fBlue - fRed) / delta + 120.0f;
461 else if (maxrgb == fBlue)
463 h = 60.0f * (fRed - fGreen) / delta + 240.0f;
466 float s = (maxrgb == 0.0f) ? 0.0f : (1.0f - (minrgb / maxrgb));
470 *brightness = maxrgb;
494 if (
rgba[3] == 1.0f)
return [[
self retain] autorelease];
496 green:rgba[1] * rgba[3]
497 blue:rgba[2] * rgba[3]
502- (
OOColor *) colorWithBrightnessFactor:(
float)factor
505 green:OOClamp_0_1_f(rgba[1] * factor)
506 blue:OOClamp_0_1_f(rgba[2] * factor)
514 [
self getRed:&r green:&g blue:&b alpha:&a];
515 return [NSArray arrayWithObjects:
516 [NSNumber numberWithFloat:r],
517 [NSNumber numberWithFloat:g],
518 [NSNumber numberWithFloat:b],
519 [NSNumber numberWithFloat:a],
540 return [NSString stringWithFormat:@"{%.3g, %.3g, %.3g, %.3g}", components.r, components.g, components.b, components.a];
546 return [NSString stringWithFormat:@"{%i, %.3g, %.3g, %.3g}", (int)components.h, components.s, components.b, components.a];
NSString * OORGBAComponentsDescription(OORGBAComponents components)
NSString * OOHSBAComponentsDescription(OOHSBAComponents components)
NSString * OORGBAComponentsDescription(OORGBAComponents components)
NSString * OOHSBAComponentsDescription(OOHSBAComponents components)
void getHue:saturation:brightness:alpha:(float *hue,[saturation] float *saturation,[brightness] float *brightness,[alpha] float *alpha)
NSString * descriptionComponents()
OOColor * colorWithRed:green:blue:alpha:(float red,[green] float green,[blue] float blue,[alpha] float alpha)
OOColor * colorWithWhite:alpha:(float white,[alpha] float alpha)
void setHue:saturation:brightness:alpha:(float h, [saturation] float s, [brightness] float b, [alpha] float a)
OOColor * darkGrayColor()
NSString * rgbaDescription()
float saturationComponent()
void getRed:green:blue:alpha:(float *red,[green] float *green,[blue] float *blue,[alpha] float *alpha)
NSArray * normalizedArray()
void setRed:green:blue:alpha:(float r, [green] float g, [blue] float b, [alpha] float a)
OOColor * premultipliedColor()
NSString * hsbaDescription()
OOHSBAComponents hsbaComponents()
OOColor * lightGrayColor()
OORGBAComponents rgbaComponents()
float brightnessComponent()