Skip to content
Browse files

Add alpha support to UIColor hex categories

  • Loading branch information...
1 parent 625e9ac commit 56bf569ebd34ac4ea1cc568a100b684cd25ef18f @soffes soffes committed Nov 14, 2011
Showing with 77 additions and 25 deletions.
  1. +19 −3 SSToolkit/UIColor+SSToolkitAdditions.h
  2. +44 −19 SSToolkit/UIColor+SSToolkitAdditions.m
  3. +14 −3 Tests/ColorCategoryTest.m
View
22 SSToolkit/UIColor+SSToolkitAdditions.h
@@ -14,11 +14,14 @@
/**
Creates and returns an UIColor object containing a given value.
- @param hex The value for the new color. The `#` sign is optional.
+ @param hex The value for the new color. The `#` sign is not required.
@return An UIColor object containing a value.
- The alpha value will be `1.0`.
+ You can specify hex values in the following formats: `rgb`, `rrggbb`, or `rrggbbaa`.
+
+ The default alpha value is `1.0`.
+
*/
+ (UIColor *)colorWithHex:(NSString *)hex;
@@ -27,11 +30,24 @@
@return The receiver's value as a hex string.
- The value will be `nil` if the color is in a color space other than Grayscale or RGB. The `#` sign is omitted.
+ The value will be `nil` if the color is in a color space other than Grayscale or RGB. The `#` sign is omitted. Alpha
+ will be omitted.
*/
- (NSString *)hexValue;
/**
+ Returns the receiver's value as a hex string.
+
+ @param includeAlpha `YES` if alpha should be included. `NO` if it should not.
+
+ @return The receiver's value as a hex string.
+
+ The value will be `nil` if the color is in a color space other than Grayscale or RGB. The `#` sign is omitted. Alpha is
+ included if `includeAlpha` is `YES`.
+ */
+- (NSString *)hexValueWithAlpha:(BOOL)includeAlpha;
+
+/**
The receiver's red component value. (read-only)
The value of this property is a floating-point number in the range `0.0` to `1.0`. `-1.0` is returned if the color is
View
63 SSToolkit/UIColor+SSToolkitAdditions.m
@@ -8,60 +8,85 @@
#import "UIColor+SSToolkitAdditions.h"
-static NSUInteger integerFromHexString(NSString *string) {
+@interface NSString (SSToolkitPrivateAdditions)
+- (NSUInteger)_hexValue;
+@end
+
+@implementation NSString (SSToolkitPrivateAdditions)
+- (NSUInteger)_hexValue {
NSUInteger result = 0;
- sscanf([string UTF8String], "%x", &result);
+ sscanf([self UTF8String], "%x", &result);
return result;
}
+@end
@implementation UIColor (SSToolkitAdditions)
-// Adapted from https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
+ (UIColor *)colorWithHex:(NSString *)hex {
// Remove `#`
if ([[hex substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"#"]) {
hex = [hex substringFromIndex:1];
}
- // Invalid if not 3, or 6 characters
+ // Invalid if not 3, 6, or 8 characters
NSUInteger length = [hex length];
- if (length != 3 && length != 6) {
+ if (length != 3 && length != 6 && length != 8) {
return nil;
}
-
- NSUInteger digits = length / 3;
- CGFloat maxValue = (digits == 1) ? 15.0f : 255.0f;
- CGFloat red = integerFromHexString([hex substringWithRange:NSMakeRange(0, digits)]) / maxValue;
- CGFloat green = integerFromHexString([hex substringWithRange:NSMakeRange(digits, digits)]) / maxValue;
- CGFloat blue = integerFromHexString([hex substringWithRange:NSMakeRange(2 * digits, digits)]) / maxValue;
+ // Make the string 8 characters long for easier parsing
+ if (length == 3) {
+ NSString *r = [hex substringWithRange:NSMakeRange(0, 1)];
+ NSString *g = [hex substringWithRange:NSMakeRange(1, 1)];
+ NSString *b = [hex substringWithRange:NSMakeRange(2, 1)];
+ hex = [NSString stringWithFormat:@"%@%@%@%@%@%@ff",
+ r, r, g, g, b, b];
+ } else if (length == 6) {
+ hex = [hex stringByAppendingString:@"ff"];
+ }
+
+ CGFloat red = [[hex substringWithRange:NSMakeRange(0, 2)] _hexValue] / 255.0f;
+ CGFloat green = [[hex substringWithRange:NSMakeRange(2, 2)] _hexValue] / 255.0f;
+ CGFloat blue = [[hex substringWithRange:NSMakeRange(4, 2)] _hexValue] / 255.0f;
+ CGFloat alpha = [[hex substringWithRange:NSMakeRange(6, 2)] _hexValue] / 255.0f;
- return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
+ return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
-// Inspired by https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
-- (NSString *)hexValue {
+- (NSString *)hexValue {
+ return [self hexValueWithAlpha:NO];
+}
+
+
+- (NSString *)hexValueWithAlpha:(BOOL)includeAlpha {
CGColorRef color = self.CGColor;
size_t count = CGColorGetNumberOfComponents(color);
const CGFloat *components = CGColorGetComponents(color);
static NSString *stringFormat = @"%02x%02x%02x";
+ NSString *hex = nil;
+
// Grayscale
if (count == 2) {
- NSUInteger white = (NSUInteger)(components[0] * (CGFloat)255);
- return [NSString stringWithFormat:stringFormat, white, white, white];
+ NSUInteger white = (NSUInteger)(components[0] * 255.0f);
+ hex = [NSString stringWithFormat:stringFormat, white, white, white];
}
// RGB
else if (count == 4) {
- return [NSString stringWithFormat:stringFormat, (NSUInteger)(components[0] * (CGFloat)255),
- (NSUInteger)(components[1] * (CGFloat)255), (NSUInteger)(components[2] * (CGFloat)255)];
+ hex = [NSString stringWithFormat:stringFormat, (NSUInteger)(components[0] * 255.0f),
+ (NSUInteger)(components[1] * 255.0f), (NSUInteger)(components[2] * 255.0f)];
+ }
+
+ // Add alpha
+ if (hex && includeAlpha) {
+ hex = [hex stringByAppendingFormat:@"%02x", (NSUInteger)(self.alpha * 255.0f)];
}
// Unsupported color space
- return nil;
+ return hex;
}
View
17 Tests/ColorCategoryTest.m
@@ -51,31 +51,42 @@ - (void)testBlue {
- (void)testColorWithHex {
UIColor *red = [UIColor redColor];
- GHAssertEqualObjects(red, [UIColor colorWithHex:@"ff0000"], nil);
GHAssertEqualObjects(red, [UIColor colorWithHex:@"f00"], nil);
+ GHAssertEqualObjects(red, [UIColor colorWithHex:@"ff0000"], nil);
+ GHAssertEqualObjects(red, [UIColor colorWithHex:@"ff0000ff"], nil);
UIColor *green = [UIColor greenColor];
- GHAssertEqualObjects(green, [UIColor colorWithHex:@"00ff00"], nil);
GHAssertEqualObjects(green, [UIColor colorWithHex:@"0f0"], nil);
+ GHAssertEqualObjects(green, [UIColor colorWithHex:@"00ff00"], nil);
+ GHAssertEqualObjects(green, [UIColor colorWithHex:@"00ff00ff"], nil);
UIColor *blue = [UIColor blueColor];
- GHAssertEqualObjects(blue, [UIColor colorWithHex:@"0000ff"], nil);
GHAssertEqualObjects(blue, [UIColor colorWithHex:@"00f"], nil);
+ GHAssertEqualObjects(blue, [UIColor colorWithHex:@"0000ff"], nil);
+ GHAssertEqualObjects(blue, [UIColor colorWithHex:@"0000ffff"], nil);
+
+ UIColor *bleh = [UIColor colorWithRed:1.0f green:0.2f blue:0.0f alpha:0.2f];
+ GHAssertEqualObjects(bleh, [UIColor colorWithHex:@"ff330033"], nil);
}
- (void)testHexValue {
UIColor *red = [UIColor redColor];
GHAssertEqualObjects([red hexValue], @"ff0000", nil);
+ GHAssertEqualObjects([red hexValueWithAlpha:YES], @"ff0000ff", nil);
UIColor *green = [UIColor greenColor];
GHAssertEqualObjects([green hexValue], @"00ff00", nil);
+ GHAssertEqualObjects([green hexValueWithAlpha:YES], @"00ff00ff", nil);
UIColor *blue = [UIColor blueColor];
GHAssertEqualObjects([blue hexValue], @"0000ff", nil);
+ GHAssertEqualObjects([blue hexValueWithAlpha:YES], @"0000ffff", nil);
UIColor *white = [UIColor whiteColor];
GHAssertEqualObjects([white hexValue], @"ffffff", nil);
+ GHAssertEqualObjects([white hexValueWithAlpha:YES], @"ffffffff", nil);
+ GHAssertEqualObjects([[white colorWithAlphaComponent:0.2f] hexValueWithAlpha:YES], @"ffffff33", nil);
}
@end

0 comments on commit 56bf569

Please sign in to comment.
Something went wrong with that request. Please try again.