Created
October 9, 2012 20:47
-
-
Save jsumners/3861305 to your computer and use it in GitHub Desktop.
Screw all of the bullshit llvm warnings in UIColor-Utilities
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 (JBSUtilities) | |
/** | |
Give it a hex string, e.g. "#FFEEDD" or "FFEEDD", get back the UIColor | |
representation. | |
@param stringToConvert A NSASCIIStringEncoding hex string. | |
@return The UIColor represented by the hex string. | |
*/ | |
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert; | |
@end | |
@implementation UIColor (JBSUtilities) | |
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert | |
{ | |
NSString *str = [[stringToConvert | |
stringByReplacingOccurrencesOfString:@"#" | |
withString:@""] uppercaseString]; | |
const char *hex = [str cStringUsingEncoding:NSASCIIStringEncoding]; | |
uint32_t bytes = strtoq(hex, NULL, 16); | |
int red = (bytes >> 16) & 0xFF; | |
int green = (bytes >> 8) & 0xFF; | |
int blue = bytes & 0xFF; | |
return [UIColor | |
colorWithRed:(red / 255.0f) | |
green:(green / 255.0f) | |
blue:(blue / 255.0f) | |
alpha:1.0f]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment