Created
October 3, 2013 12:28
-
-
Save PoslinskiNet/6809059 to your computer and use it in GitHub Desktop.
Colors helper
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 <Foundation/Foundation.h> | |
#import <QuartzCore/QuartzCore.h> | |
@interface FTUIHelper : NSObject | |
+ (UIImage *)createImageFromColor:(UIColor *)color; | |
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString | |
@end | |
@implementation UIHelperColors | |
+ (UIImage *)createImageFromColor:(UIColor *)color { | |
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; | |
colorView.backgroundColor = color; | |
UIGraphicsBeginImageContext(colorView.bounds.size); | |
[colorView.layer renderInContext:UIGraphicsGetCurrentContext()]; | |
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return colorImage; | |
} | |
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString { | |
UIColor *result = nil; | |
unsigned int colorCode = 0; | |
unsigned char redByte, greenByte, blueByte; | |
if (nil != inColorString) | |
{ | |
NSScanner *scanner = [NSScanner scannerWithString:inColorString]; | |
(void) [scanner scanHexInt:&colorCode]; // ignore error | |
} | |
redByte = (unsigned char) (colorCode >> 16); | |
greenByte = (unsigned char) (colorCode >> 8); | |
blueByte = (unsigned char) (colorCode); // masks off high bits | |
result = [UIColor | |
colorWithRed: (float)redByte / 0xff | |
green: (float)greenByte/ 0xff | |
blue: (float)blueByte / 0xff | |
alpha: 1.0]; | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment