Skip to content

Instantly share code, notes, and snippets.

@nateflink
Created August 14, 2013 21:14
Show Gist options
  • Save nateflink/6235696 to your computer and use it in GitHub Desktop.
Save nateflink/6235696 to your computer and use it in GitHub Desktop.
static inline NSString *hexFromUIColor(UIColor * _color) {
if (CGColorGetNumberOfComponents(_color.CGColor) < 4) {
const CGFloat *components = CGColorGetComponents(_color.CGColor);
_color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];
}
if (CGColorSpaceGetModel(CGColorGetColorSpace(_color.CGColor)) != kCGColorSpaceModelRGB) {
return [NSString stringWithFormat:@"#FFFFFF"];
}
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(_color.CGColor))[0]*255.0), (int)((CGColorGetComponents(_color.CGColor))[1]*255.0), (int)((CGColorGetComponents(_color.CGColor))[2]*255.0)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment