如何将RGB转换为HTML颜色?

时间:2011-05-14 14:34:22

标签: ios objective-c hex uicolor

请告诉我如何将RGB UIColor转换为十六进制HTML颜色代码字符串?

1 个答案:

答案 0 :(得分:16)

- (NSString *)getHexStringForColor:(UIColor*)color {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    CGFloat r = components[0];
    CGFloat g = components[1];
    CGFloat b = components[2];

    return [NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
}