Skip to content

Instantly share code, notes, and snippets.

@jsumners
Created October 9, 2012 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsumners/3861305 to your computer and use it in GitHub Desktop.
Save jsumners/3861305 to your computer and use it in GitHub Desktop.
Screw all of the bullshit llvm warnings in UIColor-Utilities
#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