Skip to content

Instantly share code, notes, and snippets.

@sadatrahman
Created June 15, 2011 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sadatrahman/1026653 to your computer and use it in GitHub Desktop.
Save sadatrahman/1026653 to your computer and use it in GitHub Desktop.
UIColor factory category method - returns an appropriate UIColor instance given a hex colour code.
//
// UIColor+Hex.h
// SRKit
//
// Created by Sadat Rahman on 29/11/08.
// Copyright Sadat Rahman 2008. All rights reserved.
//
@interface UIColor (Hex)
+ (UIColor *)colorWithHexValue:(NSString *)hexValue;
@end
//
// UIColor+Hex.m
// SRKit
//
// Created by Sadat Rahman on 29/11/08.
// Copyright Sadat Rahman 2008. All rights reserved.
//
#import "UIColor+Hex.h"
@implementation UIColor (Hex)
+ (UIColor *)colorWithHexValue:(NSString *)hexValue
{
NSString *cleanedHexValue = [hexValue stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
unsigned long rgbColorValue = strtoul([[cleanedHexValue substringToIndex:6] cStringUsingEncoding:NSASCIIStringEncoding], NULL, 16);
NSUInteger redValue = (rgbColorValue >> 16) & 0xFF;
NSUInteger greenValue = (rgbColorValue >> 8) & 0xFF;
NSUInteger blueValue = (rgbColorValue >> 0) & 0xFF;
return [UIColor colorWithRed:(redValue / 255.) green:(greenValue / 255.) blue:(blueValue / 255.) alpha:1.];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment