Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Adding alpha support #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions Source/VSTheme.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


static BOOL stringIsEmpty(NSString *s);
static UIColor *colorWithHexString(NSString *hexString);
static UIColor *colorWithHexString(NSString *hexString, NSNumber *alpha);


@interface VSTheme ()
Expand Down Expand Up @@ -117,7 +117,13 @@ - (UIColor *)colorForKey:(NSString *)key {
return cachedColor;

NSString *colorString = [self stringForKey:key];
UIColor *color = colorWithHexString(colorString);
NSNumber *number;
if ( [self objectForKey:[NSString stringWithFormat:@"%@Alpha", key]]) {
number = [NSNumber numberWithFloat:[self floatForKey:[NSString stringWithFormat:@"%@Alpha", key]]];
}


UIColor *color = colorWithHexString(colorString, number);
if (color == nil)
color = [UIColor blackColor];

Expand Down Expand Up @@ -262,8 +268,16 @@ static BOOL stringIsEmpty(NSString *s) {
}


static UIColor *colorWithHexString(NSString *hexString) {
static UIColor *colorWithHexString(NSString *hexString, NSNumber *alpha) {

float a = 1.0f;
if ( alpha ) {
a = [alpha floatValue];
}
if ( a > 1.0f || a < 0.0f ) {
a = 1.0f;
}

/*Picky. Crashes by design.*/

if (stringIsEmpty(hexString))
Expand All @@ -282,5 +296,5 @@ static BOOL stringIsEmpty(NSString *s) {
[[NSScanner scannerWithString:greenString] scanHexInt:&green];
[[NSScanner scannerWithString:blueString] scanHexInt:&blue];

return [UIColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:1.0f];
return [UIColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:a];
}