如何将RGB转换为HTML颜色?

问题描述:

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

- (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)]; 
} 
+2

不幸的是,这不适用于非RGB颜色,如'[UIColor blackColor]'。问题只是关于RGB颜色,但如果你想支持更多'UIColor',你可以在这里使用ngb的方法:http://*.com/a/13134326/211292 – ThomasW 2014-05-22 09:22:46