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

support rgba color definitions #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Demo/DB5Demo/DB5.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>backgroundColor</key>
<string>708090</string>
<key>labelTextColor</key>
<string>ececec</string>
<string>rgba(236, 236, 236, 1.0)</string>
<key>labelFont</key>
<string>Avenir-Medium</string>
<key>labelFontSize</key>
Expand Down
2 changes: 1 addition & 1 deletion Source/VSTheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef NS_ENUM(NSUInteger, VSTextCaseTransform) {
- (NSInteger)integerForKey:(NSString *)key;
- (CGFloat)floatForKey:(NSString *)key;
- (UIImage *)imageForKey:(NSString *)key; /*Via UIImage imageNamed:*/
- (UIColor *)colorForKey:(NSString *)key; /*123ABC or #123ABC: 6 digits, leading # allowed but not required*/
- (UIColor *)colorForKey:(NSString *)key; /*6 hexadecimal characters values: #123ABC, leading # not required OR rgba with interger values: rgba(18, 58, 188, 0.8), whitespace between values ignored*/
- (UIEdgeInsets)edgeInsetsForKey:(NSString *)key; /*xTop, xLeft, xRight, xBottom keys*/
- (UIFont *)fontForKey:(NSString *)key; /*x and xSize keys*/
- (CGPoint)pointForKey:(NSString *)key; /*xX and xY keys*/
Expand Down
25 changes: 24 additions & 1 deletion Source/VSTheme.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

static BOOL stringIsEmpty(NSString *s);
static UIColor *colorWithHexString(NSString *hexString);
static UIColor *colorWithRGBAString(NSString *rgbaString);


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

NSString *colorString = [self stringForKey:key];
UIColor *color = colorWithHexString(colorString);
UIColor *color = [colorString hasPrefix:@"rgba"] ? colorWithRGBAString(colorString) : colorWithHexString(colorString);
if (color == nil)
color = [UIColor blackColor];

Expand Down Expand Up @@ -284,3 +285,25 @@ static BOOL stringIsEmpty(NSString *s) {

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

static UIColor *colorWithRGBAString(NSString *rgbaString) {
static NSString* const kDB5RGBAStringPattern = @"^rgba\\(\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*,\\s*(\\d{1}\\.\\d+)\\s*\\)$";
static NSRegularExpression *rgbaRegularExpression;
if (!rgbaRegularExpression) {
rgbaRegularExpression = [NSRegularExpression regularExpressionWithPattern:kDB5RGBAStringPattern options:NSRegularExpressionCaseInsensitive error:nil];
}

NSArray* matches = [rgbaRegularExpression matchesInString:rgbaString options:0 range:NSMakeRange(0, [rgbaString length])];
if ([matches count] != 1) {
return nil;
}
NSTextCheckingResult *result = [matches firstObject];
if (result.numberOfRanges != 5) {
return nil;
}
int red = [[rgbaString substringWithRange:[result rangeAtIndex:1]] intValue];
int green = [[rgbaString substringWithRange:[result rangeAtIndex:2]] intValue];
int blue = [[rgbaString substringWithRange:[result rangeAtIndex:3]] intValue];
CGFloat alpha = [[rgbaString substringWithRange:[result rangeAtIndex:4]] floatValue];
return [UIColor colorWithRed:(CGFloat)red/255.0f green:(CGFloat)green/255.0f blue:(CGFloat)blue/255.0f alpha:alpha];
}