Last active
December 21, 2015 19:49
-
-
Save zydeco/6357436 to your computer and use it in GitHub Desktop.
Init UIColor from string like #ff33cc rgb(255,51,204) or rgba(255,51,204,0.8)
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
#import <UIKit/UIKit.h> | |
@interface UIColor (String) | |
+ (UIColor*)colorWithString:(NSString*)str; | |
- (UIColor*)initWithString:(NSString*)str; | |
@end |
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
#import "UIColor+String.h" | |
@implementation UIColor (String) | |
+ (UIColor *)colorWithString:(NSString *)str { | |
return [[UIColor alloc] initWithString:str]; | |
} | |
- (UIColor *)initWithString:(NSString *)str { | |
CGFloat red = 0,green = 0,blue = 0,alpha = 1.0; | |
if ([str hasPrefix:@"#"]) { | |
// #rrggbb or #rrggbbaa | |
uint32_t colorValue = 0; | |
sscanf(str.UTF8String, "#%x", &colorValue); | |
if (str.length < 9) { | |
colorValue <<= 8; | |
colorValue |= 0xFF; | |
} | |
red = ((colorValue & 0xFF000000) >> 24) / 255.0; | |
green = ((colorValue & 0x00FF0000) >> 16) / 255.0; | |
blue = ((colorValue & 0x0000FF00) >> 8) / 255.0; | |
alpha = (colorValue & 0x000000FF) / 255.0; | |
} else if ([str hasPrefix:@"rgb("]) { | |
// rgb(1,2,3) | |
int r,g,b; | |
sscanf(str.UTF8String, "rgb(%d,%d,%d)", &r, &g, &b); | |
red = r / 255.0; | |
green = g / 255.0; | |
blue = b / 255.0; | |
} else if ([str hasPrefix:@"rgba("]) { | |
// rgba(1,2,3,0.75) | |
int r,g,b; | |
sscanf(str.UTF8String, "rgba(%d,%d,%d,%g)", &r, &g, &b, &alpha); | |
red = r / 255.0; | |
green = g / 255.0; | |
blue = b / 255.0; | |
} | |
return [self initWithRed:red green:green blue:blue alpha:alpha]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great!
The only issue is that it throws up four "Variable X is used uninitialized whenever 'if' condition is false'".
You can fix this by changing line 10 to:
CGFloat red=0,green=0,blue=0,alpha=0;