Created
January 17, 2012 21:17
-
-
Save cocoahero/1628933 to your computer and use it in GitHub Desktop.
An extension to the UIColor class for working with hexadecimal representations.
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 (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 |
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 "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