adium 2230:fb9497a35bb0: rgb()/rgba() support for -colorWithHTML...

commits at adium.im commits at adium.im
Sun May 17 22:04:31 UTC 2009


details:	http://hg.adium.im/adium/rev/fb9497a35bb0
revision:	2230:fb9497a35bb0
author:		David Smith <catfish.man at gmail.com>
date:		Sun May 17 15:04:22 2009 -0700

rgb()/rgba() support for -colorWithHTMLString:, fixes a console log that's been bugging me

diffstat:

 Frameworks/AIUtilities Framework/Source/AIColorAdditions.m |  28 +++++++++++++-
 UnitTests/TestColorAdditions.m                             |  12 ++++++
 2 files changed, 39 insertions(+), 1 deletions(-)

diffs (61 lines):

diff -r 565518898c15 -r fb9497a35bb0 Frameworks/AIUtilities Framework/Source/AIColorAdditions.m
--- a/Frameworks/AIUtilities Framework/Source/AIColorAdditions.m	Sun May 17 13:56:31 2009 -0700
+++ b/Frameworks/AIUtilities Framework/Source/AIColorAdditions.m	Sun May 17 15:04:22 2009 -0700
@@ -497,8 +497,34 @@
 	if (!str) return defaultColor;
 
 	unsigned strLength = [str length];
-
+	
 	NSString *colorValue = str;
+	
+	if ([str hasPrefix:@"rgb"]) {
+		NSUInteger leftParIndex = [colorValue rangeOfString:@"("].location;
+		NSUInteger rightParIndex = [colorValue rangeOfString:@")"].location;
+		if (leftParIndex == NSNotFound || rightParIndex == NSNotFound)
+		{
+			NSLog(@"+[NSColor(AIColorAdditions) colorWithHTMLString:] called with unrecognised color function (str is %@); returning %@", str, defaultColor);
+			return defaultColor;
+		}
+		leftParIndex++;
+		NSRange substrRange = NSMakeRange(leftParIndex, rightParIndex - leftParIndex);
+		colorValue = [colorValue substringWithRange:substrRange];
+		NSArray *colorComponents = [colorValue componentsSeparatedByString:@","];
+		if ([colorComponents count] < 3 || [colorComponents count] > 4) {
+			NSLog(@"+[NSColor(AIColorAdditions) colorWithHTMLString:] called with a color function with the wrong number of arguments (str is %@); returning %@", str, defaultColor);
+			return defaultColor;
+		}
+		float red, green, blue, alpha = 1.0f;
+		red = [[colorComponents objectAtIndex:0] floatValue];
+		green = [[colorComponents objectAtIndex:1] floatValue];
+		blue = [[colorComponents objectAtIndex:2] floatValue];
+		if ([colorComponents count] == 4)
+			alpha = [[colorComponents objectAtIndex:3] floatValue];
+		return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
+	}
+	
 	if ((!strLength) || ([str characterAtIndex:0] != '#')) {
 		//look it up; it's a colour name
 		NSDictionary *colorValues = [self colorNamesDictionary];
diff -r 565518898c15 -r fb9497a35bb0 UnitTests/TestColorAdditions.m
--- a/UnitTests/TestColorAdditions.m	Sun May 17 13:56:31 2009 -0700
+++ b/UnitTests/TestColorAdditions.m	Sun May 17 15:04:22 2009 -0700
@@ -1775,6 +1775,18 @@
 
 #pragma mark -
 
+- (void) testColorWithRGBAString
+{
+	NSString *string = @"rgba(255, 255, 0, 0.75)";
+	NSColor *color = [NSColor colorWithHTMLString:string];
+	STAssertEquals([color redComponent], 1.0f, @"Red component of color should be 1.0");
+	STAssertEquals([color greenComponent], 1.0f, @"Green component of color should be 1.0");
+	STAssertEquals([color blueComponent], 0.0f, @"Blue component of color should be 0.0");
+	STAssertEquals([color alphaComponent], 0.75f, @"Alpha component of color should be 0.75");
+}
+
+#pragma mark -
+
 - (void)testColorWithHTMLStringWithNil
 {
 	NSColor *noColor = [NSColor colorWithHTMLString:nil];




More information about the commits mailing list