- (UIColor *)UIColorFromRgbCode:(NSString *)colorCode {
int COLOR_RED = 0;
int COLOR_GREEN = 1;
int COLOR_BLUE = 2;
if ( [[colorCode substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"#"] ) {
colorCode = [colorCode substringWithRange:NSMakeRange(1, colorCode.length - 1)];
}
if ( 3 == [colorCode length] ) {
NSMutableString *_colorCode = [[[NSMutableString alloc] init] autorelease];
for (int i = 0; i < colorCode.length; i++) {
[_colorCode appendString:[colorCode substringWithRange:NSMakeRange(i, 1)]];
[_colorCode appendString:[colorCode substringWithRange:NSMakeRange(i, 1)]];
}
colorCode = [_colorCode copy];
}
char *hexCode;
char *endptr;
float red,green,blue;
for (int i = 0; i < 3; i++) {
hexCode = [[NSString stringWithFormat:@"+0x%@", [colorCode substringWithRange:NSMakeRange(i * 2, 2)]] cStringUsingEncoding:NSASCIIStringEncoding];
if (COLOR_RED == i) {
red = strtol(hexCode, &endptr, 16);
} else if ( COLOR_GREEN == i ) {
green = strtol(hexCode, &endptr, 16);
} else if ( COLOR_BLUE == i ) {
blue = strtol(hexCode, &endptr, 16);
}
}
return [UIColor colorWithRed:red / 255 green:green / 255 blue:blue / 255 alpha:1.0f];
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
33