Skip to content
This repository was archived by the owner on Nov 18, 2019. It is now read-only.
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
1 change: 1 addition & 0 deletions UIColor/UIColor+Expanded.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
// Color builders
+ (UIColor *)randomColor;
+ (UIColor *)colorWithString:(NSString *)stringToConvert;
+ (UIColor *)colorWithRGBString:(NSString *)stringToConvert;//added by ysc
+ (UIColor *)colorWithRGBHex:(UInt32)hex;
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert;
+ (UIColor *)colorWithName:(NSString *)cssColorName;
Expand Down
35 changes: 35 additions & 0 deletions UIColor/UIColor+Expanded.m
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,41 @@ + (UIColor *)colorWithString:(NSString *)stringToConvert {
return color;
}

///////////////////added by ysc//////////////////////////////////////////////////////////////
+ (UIColor *)colorWithRGBString:(NSString *)stringToConvert {
NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
if (![scanner scanString:@"{" intoString:NULL]) return nil;
const NSUInteger kMaxComponents = 4;
CGFloat c[kMaxComponents];
NSUInteger i = 0;
if (![scanner scanFloat:&c[i++]]) return nil;
while (1) {
if ([scanner scanString:@"}" intoString:NULL]) break;
if (i >= kMaxComponents) return nil;
if ([scanner scanString:@"," intoString:NULL]) {
if (![scanner scanFloat:&c[i++]]) return nil;

} else {
// either we're at the end of there's an unexpected character here
// both cases are error conditions
return nil;
}
}
if (![scanner isAtEnd]) return nil;
UIColor *color;
switch (i) {
case 2: // monochrome
color = [UIColor colorWithWhite:c[0] alpha:c[1]];
break;
case 3: // RGB
color = [UIColor colorWithRed:c[0] / 255.0f green:c[1] / 255.0f blue:c[2] / 255.0f alpha:1.0f];
break;
default:
color = nil;
}
return color;
}

#pragma mark Class methods

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down