有没有简单的方法可以转换UIColor
为十六进制值?
或者我们是否必须使用以下方法获取 RGB 分量CGColorGetComponents
然后从那里解决?
e.g CGColorGetComponents(color.CGColor)[0] * 256
?
我还必须将 UIColor 转换为其十六进制组件。
正如 lewiguez 已经指出的那样,有一个非常好的类别github https://github.com/erica/uicolor-utilities就可以完成所有这些事情。
但因为我想了解它是如何完成的,所以我自己制作了 RGB 颜色的简单实现。
+ (NSString*)colorToWeb:(UIColor*)color
{
NSString *webColor = nil;
// This method only works for RGB colors
if (color &&
CGColorGetNumberOfComponents(color.CGColor) == 4)
{
// Get the red, green and blue components
const CGFloat *components = CGColorGetComponents(color.CGColor);
// These components range from 0.0 till 1.0 and need to be converted to 0 till 255
CGFloat red, green, blue;
red = roundf(components[0] * 255.0);
green = roundf(components[1] * 255.0);
blue = roundf(components[2] * 255.0);
// Convert with %02x (use 02 to always get two chars)
webColor = [[NSString alloc]initWithFormat:@"%02x%02x%02x", (int)red, (int)green, (int)blue];
}
return webColor;
}
欢迎所有反馈!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)