Skip to content

Commit

Permalink
Enhance UIColorTypeConverter to support alpha. Fixes #181
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksundin committed Feb 23, 2014
1 parent fa4aed4 commit 5ee4021
Show file tree
Hide file tree
Showing 12 changed files with 4,184 additions and 17,291 deletions.
2 changes: 1 addition & 1 deletion .scripts/pod-update-checksum.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3f6713def3d63433f31cafc29523e14a85f22206
a858f8fbb771e8db241f5a3f882166951d7b5c2d
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

#import "TyphoonTypeConverter.h"


/**
* A type converter for UIColor.
*
* The formats supported are:
* Hexadecimal, #RRGGBB or #AARRGGBB
* Css-style, rgb(r,g,b) or rgba(r,g,b,a)
*
*/
@interface TyphoonUIColorTypeConverter : NSObject <TyphoonTypeConverter>

@end
67 changes: 57 additions & 10 deletions Source/ios/TypeConversion/Converters/TyphoonUIColorTypeConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#import "TyphoonUIColorTypeConverter.h"

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>


@implementation TyphoonUIColorTypeConverter
Expand All @@ -25,18 +22,68 @@ - (id)supportedType
return [UIColor class];
}

- (id)convert:(NSString *)stringValue
- (UIColor *)colorFromRed:(NSUInteger)red green:(NSUInteger)green blue:(NSUInteger)blue alpha:(CGFloat)alpha
{
return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:alpha];
}

- (UIColor *)colorFromHexString:(NSString *)hexString
{
NSString *hexString =
[[stringValue stringByReplacingOccurrencesOfString:@"#" withString:@""] stringByReplacingOccurrencesOfString:@"0x" withString:@""];
if (![hexString length] == 6) {
[NSException raise:NSInvalidArgumentException format:@"%@ requires a six digit hex string.", NSStringFromClass([self class])];
hexString =
[[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] stringByReplacingOccurrencesOfString:@"0x" withString:@""];

unsigned int red, green, blue, alpha;
if ([hexString length] == 6) {
sscanf([hexString UTF8String], "%02X%02X%02X", &red, &green, &blue);
alpha = 255;
}
else if ([hexString length] == 8) {
sscanf([hexString UTF8String], "%02X%02X%02X%02X", &alpha, &red, &green, &blue);
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ requires a six or eight digit hex string.",
NSStringFromClass([self class])];
}
return [self colorFromRed:red green:green blue:blue alpha:alpha / 255.0];
}

- (UIColor *)colorFromCssStyleString:(NSString *)cssString
{
cssString = [[[cssString stringByReplacingOccurrencesOfString:@"rgb(" withString:@""]
stringByReplacingOccurrencesOfString:@"rgba(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];

NSArray *colorComponents = [cssString componentsSeparatedByString:@","];

unsigned int red, green, blue;
sscanf([hexString UTF8String], "%02X%02X%02X", &red, &green, &blue);
__autoreleasing UIColor *color = [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1];
float alpha;
if ([colorComponents count] == 3) {
sscanf([cssString UTF8String], "%d,%d,%d", &red, &green, &blue);
alpha = 1.0;
}
else if ([colorComponents count] == 4) {
sscanf([cssString UTF8String], "%d,%d,%d,%f", &red, &green, &blue, &alpha);
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ requires css style format rgb(r,g,b) or rgba(r,g,b,a).",
NSStringFromClass([self class])];
}
return [self colorFromRed:red green:green blue:blue alpha:alpha];
}

- (id)convert:(NSString *)stringValue
{
UIColor *color = nil;

if ([stringValue hasPrefix:@"#"] || [stringValue hasPrefix:@"0x"]) {
color = [self colorFromHexString:stringValue];
}
else if ([stringValue hasPrefix:@"rgb"]) {
color = [self colorFromCssStyleString:stringValue];
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ does not support the color format %@", NSStringFromClass([self class]),
stringValue];
}
return color;
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ PODS:
- OCHamcrest (1.9)
- OCMockito (0.23):
- OCHamcrest
- Typhoon (1.7.8):
- Typhoon (1.8.0):
- Typhoon/no-arc
- Typhoon/no-arc (1.7.8)
- Typhoon/no-arc (1.8.0)

DEPENDENCIES:
- OCHamcrest (~> 1.9)
Expand All @@ -18,6 +18,6 @@ EXTERNAL SOURCES:
SPEC CHECKSUMS:
OCHamcrest: f8393efd5a49d91879be573635d6183effacc0ab
OCMockito: 01560fd24319b85c223e10f52e2e41fbe2c6d434
Typhoon: 36e15ef0a4d0f072c23b3a0a98e5bc76991a1697
Typhoon: 15463de5bb1d5b65d7d59e4a85414002cd2da937

COCOAPODS: 0.29.0
2 changes: 1 addition & 1 deletion Tests/Pods/Local Podspecs/Typhoon.podspec

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Tests/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Tests/Pods/Pods-OS X Tests (Cocoapods)-dummy.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Tests/Pods/Pods-OS X Tests (Cocoapods)-environment.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Tests/Pods/Pods-iOS Tests (Cocoapods)-dummy.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Tests/Pods/Pods-iOS Tests (Cocoapods)-environment.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ee4021

Please sign in to comment.