Last active
August 29, 2015 14:03
-
-
Save ffried/ce62441aaec219cb913c to your computer and use it in GitHub Desktop.
UIColor+FFColorWithHex
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
// | |
// UIColor+FFColorWithHex.h | |
// | |
// Created by Florian Friedrich on 11.07.14. | |
// Copyright (c) 2014 Florian Friedrich. All rights reserved. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// | |
// 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 OR COPYRIGHT HOLDERS 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. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIColor (FFColorWithHex) | |
/** | |
* Creates a color from a hex string. | |
* @param hex The hex string to create the color from. Can include # or 0x as prefix. | |
* @return The color represented by the hex string. | |
*/ | |
+ (instancetype)colorWithHexString:(NSString *)hex; | |
/** | |
* Creates a color from a hex string. | |
* @param hex The hex string to create the color from. Can include # or 0x as prefix. | |
* @param alpha The alpha value for the color. | |
* @return The color represented by the hex string including the alpha value. | |
*/ | |
+ (instancetype)colorWithHexString:(NSString *)hex 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
// | |
// UIColor+FFColorWithHex.h | |
// | |
// Created by Florian Friedrich on 11.07.14. | |
// Copyright (c) 2014 Florian Friedrich. All rights reserved. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
// | |
// 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 OR COPYRIGHT HOLDERS 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. | |
// | |
#import "UIColor+FFColorWithHex.h" | |
@implementation UIColor (FFColorWithHex) | |
+ (instancetype)colorWithHexString:(NSString *)hex { | |
return [self colorWithHexString:hex alpha:1.0f]; | |
} | |
+ (instancetype)colorWithHexString:(NSString *)hex alpha:(CGFloat)alpha { | |
NSString *rawHex = hex; | |
if ([hex hasPrefix:@"#"]) rawHex = [hex substringFromIndex:1]; | |
if ([hex hasPrefix:@"0x"]) rawHex = [hex substringFromIndex:2]; | |
NSAssert(rawHex.length == 6, @"Hex string has to have 6 chars (without # or 0x)"); | |
NSString *redHex = [rawHex substringWithRange:NSMakeRange(0, 2)]; | |
NSString *greenHex = [rawHex substringWithRange:NSMakeRange(2, 2)]; | |
NSString *blueHex = [rawHex substringWithRange:NSMakeRange(4, 2)]; | |
unsigned int r, g, b = 0; | |
[[NSScanner scannerWithString:redHex] scanHexInt:&r]; | |
[[NSScanner scannerWithString:greenHex] scanHexInt:&g]; | |
[[NSScanner scannerWithString:blueHex] scanHexInt:&b]; | |
return [self colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:alpha]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment