//十六进制颜色转换成RGB颜色
inline static UIColor* GetColorFromCSSHex(NSString *hexColor) { // #FF3300
if (hexColor == nil || [hexColor isEqualToString:@""]) {
return nil;
}
if ([hexColor length] != 7) {
return nil;
}
unsigned int red = 255, green = 255, blue = 255;
NSRange range;
range.length = 2;
range.location = 1;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&red];
range.location = 3;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&green];
range.location = 5;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]] scanHexInt:&blue];
return [UIColor colorWithRed:(float)(red/255.0f) green:(float)(green/255.0f) blue:(float)(blue/255.0f) alpha:1.0f];
}
//RGB颜色转化成十六进制颜色
inline static NSString* GetCSSHexFromColor(UIColor *color) {
if (color == nil) {
return nil;
}
NSString *strColor = nil;
CGColorRef cgColor = [color CGColor];
//int num = CGColorGetNumberOfComponents(cgColor);
const CGFloat *colors = CGColorGetComponents(cgColor);//RGB
int r = colors[0] * 255.0f;
int g = colors[1] * 255.0f;
int b = colors[2] * 255.0f;
strColor = [NSString stringWithFormat:@"#%02x%02x%02x", r, g, b];//RGB
return strColor;
}