Created
September 15, 2011 11:19
-
-
Save simonwhitaker/1219029 to your computer and use it in GitHub Desktop.
A category on UIColor adding +(UIColor*)colorWithHexValue:(NSString*)hexValue;
This file contains hidden or 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
// | |
// | |
// 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 |
This file contains hidden or 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
// 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 file contains hidden or 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
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/> |
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
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.