Skip to content

Instantly share code, notes, and snippets.

@cocoahero
Created January 17, 2012 21:17
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 cocoahero/1628933 to your computer and use it in GitHub Desktop.
Save cocoahero/1628933 to your computer and use it in GitHub Desktop.
An extension to the UIColor class for working with hexadecimal representations.
#import <UIKit/UIKit.h>
@interface UIColor (Hexadecimal)
/**
* Convenience method for creating a UIColor object from a hexadecimal
* representation.
*
* @param hexcode A color in hexadecimal represenation
* @param alpha The alpha (opacity) percentage
* @return An autoreleased UIColor instance
*/
+ (UIColor *)colorWithHexCode:(NSUInteger)hexcode alpha:(CGFloat)alpha;
@end
#import "UIColor+Hexadecimal.h"
@implementation UIColor (Hexadecimal)
+ (UIColor *)colorWithHexCode:(NSUInteger)hexcode alpha:(CGFloat)alpha {
NSUInteger mask = 0xFF0000;
CGFloat r = (((hexcode & mask) >> 16) / 255.0); mask >>= 8;
CGFloat g = (((hexcode & mask) >> 8) / 255.0); mask >>= 8;
CGFloat b = ((hexcode & mask) / 255.0);
return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment