16进制转rgb

- (void)colorWithHexString: (NSString *) stringToConvert

{

//去掉前后空格换行符

NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]] uppercaseString];

if ([cString hasPrefix:@"0X"])

cString = [cString substringFromIndex:2];

else if ([cString hasPrefix:@"#"])

cString = [cString substringFromIndex:1];

// Separate into r, g, b substrings

NSRange range;

range.location = 0;

range.length = 2;

NSString *rString = [cString substringWithRange:range];

range.location = 2;

NSString *gString = [cString substringWithRange:range];

range.location = 4;

NSString *bString = [cString substringWithRange:range];

unsigned int r, g, b;

[[NSScanner scannerWithString:rString] scanHexInt:&r];

[[NSScanner scannerWithString:gString] scanHexInt:&g];

[[NSScanner scannerWithString:bString] scanHexInt:&b];

NSLog(@"r = %u, g = %u, b = %u",r, g, b);

}

上一篇:CSS样式中字体乱码


下一篇:【Android】自动测试工具 Monkey