标签:
/**
* 获取UIColor对象的rgb值。
*
* @param originColor
*
* @return
*/
- (NSString *)getHexStringByColor:(UIColor *)originColor
{
NSDictionary *colorDic = [self getRGBDictionaryByColor:originColor];
int r = [colorDic[@"R"] floatValue] * 255;
int g = [colorDic[@"G"] floatValue] * 255;
int b = [colorDic[@"B"] floatValue] * 255;
NSString *red = [NSString stringWithFormat:@"%02x", r];
NSString *green = [NSString stringWithFormat:@"%02x", g];
NSString *blue = [NSString stringWithFormat:@"%02x", b];
return [NSString stringWithFormat:@"#%@%@%@", red, green, blue];
}
- (NSDictionary *)getRGBDictionaryByColor:(UIColor *)originColor
{
CGFloat r=0,g=0,b=0,a=0;
if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
[originColor getRed:&r green:&g blue:&b alpha:&a];
}
else {
const CGFloat *components = CGColorGetComponents(originColor.CGColor);
r = components[0];
g = components[1];
b = components[2];
a = components[3];
}
return @{@"R":@(r),
@"G":@(g),
@"B":@(b),
@"A":@(a)};
}
标签:
原文地址:http://blog.csdn.net/shouqiangwei/article/details/42454837