Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Created September 15, 2011 11:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonwhitaker/1219029 to your computer and use it in GitHub Desktop.
Save simonwhitaker/1219029 to your computer and use it in GitHub Desktop.
A category on UIColor adding +(UIColor*)colorWithHexValue:(NSString*)hexValue;
//
//
// UIColor+GSAdditions.h
//
// Created by Simon Whitaker at Goo Software Ltd on 15/09/2011.
//
#import <UIKit/UIKit.h>
@interface UIColor (GSAdditions)
+ (UIColor*)colorWithHexValue:(NSString*)hexValue;
@end
// UIColor+GSAdditions.m
//
// Created by Simon Whitaker at Goo Software Ltd on 15/09/2011.
//
#import "UIColor+GSAdditions.h"
@implementation UIColor (GSAdditions)
+ (UIColor*)colorWithHexValue:(NSString*)hexValue
{
UIColor *defaultResult = [UIColor blackColor];
// Strip leading # if there is one
if ([hexValue hasPrefix:@"#"] && [hexValue length] > 1) {
hexValue = [hexValue substringFromIndex:1];
}
NSUInteger componentLength = 0;
if ([hexValue length] == 3)
componentLength = 1;
else if ([hexValue length] == 6)
componentLength = 2;
else
return defaultResult;
BOOL isValid = YES;
CGFloat components[3];
for (NSUInteger i = 0; i < 3; i++) {
NSString *component = [hexValue substringWithRange:NSMakeRange(componentLength * i, componentLength)];
if (componentLength == 1) {
component = [component stringByAppendingString:component];
}
NSScanner *scanner = [NSScanner scannerWithString:component];
unsigned int value;
isValid &= [scanner scanHexInt:&value];
components[i] = (CGFloat)value / 256.0;
}
if (!isValid) {
return defaultResult;
}
return [UIColor colorWithRed:components[0]
green:components[1]
blue:components[2]
alpha:1.0];
}
@end
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
@jnutting
Copy link

Nice, but I'm partial to this:

@implementation UIColor (Hex)
+ (UIColor *) colorWithHex:(uint) hex
{
  int red, green, blue, alpha;
  blue = hex & 0x000000FF;
  green = ((hex & 0x0000FF00) >> 8);
  red = ((hex & 0x00FF0000) >> 16);
  alpha = ((hex & 0xFF000000) >> 24);
  return [UIColor colorWithRed:red/255.0f
                         green:green/255.0f
                          blue:blue/255.0f
                         alpha:alpha/255.0f];
}
@end

Use like:

[UIColor colorWithHex:0xffff0000]; // red
[UIColor colorWithHex:0x770000FF]; // half-transparent blue

etc.

@simonwhitaker
Copy link
Author

Damn Jack, you've put me to shame. Nice! :)

@jnutting
Copy link

Well now, I certainly didn't mean to shame anyone ;) I think the string-based approach has its uses also, especially if you want to grab web colors from user input, or from HTML. But in code I really like the succinctness of the hex integer format.

@krejko
Copy link

krejko commented Jul 18, 2014

Hey, I came across this Hex String to UIColor method and it works wonderfully except for one thing: It appears as though you may have mistyped the maximum for the component to be 256 instead of 255 in the line components[i] = (CGFloat)value / 256.0;; When I switch it to 255 I get 1.0 for each of the RGB values in the component array when I pass in @"#FFFFFF". Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment