Skip to content

Instantly share code, notes, and snippets.

@ahmattox
Created September 28, 2012 14:42
Show Gist options
  • Save ahmattox/3800279 to your computer and use it in GitHub Desktop.
Save ahmattox/3800279 to your computer and use it in GitHub Desktop.
UIColor Additions
//
// UIColor+Additions.h
//
// Created by Anthony Mattox on 9/28/12.
// Copyright (c) 2012 Friends of The Web. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (Additions)
- (NSString *) hexString;
+ (UIColor *) colorWithHexString: (NSString *) hexString;
- (float) lightness;
- (BOOL) isTransparent;
@end
//
// UIColor+Additions.m
// Colors
//
// Created by Anthony Mattox on 9/28/12.
// Copyright (c) 2012 Friends of The Web. All rights reserved.
//
#import "UIColor+Additions.h"
@implementation UIColor (Additions)
#pragma mark - Hexidecimal Colors
- (NSString *) hexString {
const CGFloat *_color = CGColorGetComponents(self.CGColor);
NSString *hex = [NSString stringWithFormat:@"#%02X%02X%02X", (int)(_color[0]*255.0), (int)(_color[1]*255.0), (int)(_color[2]*255.0)];
return hex;
}
+ (UIColor *) colorWithHexString: (NSString *) hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 1];
green = [self colorComponentFrom: colorString start: 1 length: 1];
blue = [self colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self colorComponentFrom: colorString start: 0 length: 1];
red = [self colorComponentFrom: colorString start: 1 length: 1];
green = [self colorComponentFrom: colorString start: 2 length: 1];
blue = [self colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self colorComponentFrom: colorString start: 0 length: 2];
red = [self colorComponentFrom: colorString start: 2 length: 2];
green = [self colorComponentFrom: colorString start: 4 length: 2];
blue = [self colorComponentFrom: colorString start: 6 length: 2];
break;
default:
[NSException raise:@"Invalid color value" format: @"Color value %@ is invalid. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
break;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
#pragma mark - Inferred Color Properties
- (float) lightness {
const CGFloat *componentColors = CGColorGetComponents(self.CGColor);
return ((componentColors[0] * 299) + (componentColors[1] * 587) + (componentColors[2] * 114)) / 1000;
}
- (BOOL) isTransparent {
const CGFloat *componentColors = CGColorGetComponents(self.CGColor);
if (componentColors[3] == 0) {
return YES;
}
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment