RSS

Convert Haxstring to RGB color

28 Aug

-(UIColor*)colorWithHexString:(NSString*)hexaColor
{
NSString *cString = [[hexaColor stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

if([cString length] < 6) return [UIColor grayColor];
if([cString hasPrefix:@”0X”]) cString = [cString substringFromIndex:2];
if([cString length] != 6) return [UIColor grayColor];

NSRange range;
range.location = 0;
range.length = 2;

NSString *rString = [cString substringWithRange:range];

range.location = 2;
NSString *gString = [cString substringWithRange:range];

range.location = 4;
NSString *bString = [cString substringWithRange:range];

unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
NSLog(@”%i %i %i”, r,g,b);
return [UIColor colorWithRed:((float) r/255.0f) green:((float) g/255.0f) blue:((float) b/255.0f) alpha:1.0f];

}

Advertisements
 
Leave a comment

Posted by on August 28, 2014 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: