Created
December 4, 2013 11:32
-
-
Save liamnichols/7786159 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UIColor *)colorFromString:(NSString *)string | |
{ | |
if (![string isKindOfClass:[NSString class]] && (string.length != 3 || string.length != 6 || string.length != 8)) | |
return [UIColor USPGrayColour]; | |
if (string.length == 3) | |
{ | |
NSMutableString *str = [NSMutableString new]; | |
for(int i =0 ;i<[string length]; i++) | |
{ | |
char character = [string characterAtIndex:i]; | |
[str appendFormat:@"%c%c",character,character]; | |
} | |
string = [NSString stringWithString:str]; | |
} | |
if (string.length == 6) | |
{ | |
string = [string stringByAppendingString:@"FF"]; | |
} | |
string = [string uppercaseString]; | |
NSScanner *scanner = [NSScanner scannerWithString:string]; | |
uint32_t rgba; | |
[scanner scanHexInt:&rgba]; | |
CGFloat red = ((rgba & 0xFF000000) >> 24) / 255.0f; | |
CGFloat green = ((rgba & 0x00FF0000) >> 16) / 255.0f; | |
CGFloat blue = ((rgba & 0x0000FF00) >> 8) / 255.0f; | |
CGFloat alpha = (rgba & 0x000000FF) / 255.0f; | |
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment