如何根据HEX值创建UIColor:
#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0
green:((c>>16)&0xFF)/255.0
blue:((c>>8)&0xFF)/255.0
alpha:((c)&0xFF)/255.0];
// usage:
UIColor* c = HEXCOLOR(0xff00ffff);
我发现 CGContext<?> 函数使用float 数组而不是UIColors (惊讶!). 下面这函数就又用啦:
static float* HexToFloats(int c)
{
static float components[4];
components[0] = ((c>>24)&0xFF)/255.0;
components[1] = ((c>>16)&0xFF)/255.0;
components[2] = ((c>> 8)&0xFF)/255.0;
components[3] = ((c )&0xFF)/255.0;
return components;
}
// usage:
CGContextSetStrokeColor(c, HexToFloats(0x808080ff));
如何获取UIColor的RGBA值?
UIColor *uicolor = [UIColor redColor];
CGColorRef color = [uicolor CGColor];int numComponents = CGColorGetNumberOfComponents(color);
if (numComponents >= 3)
{
const CGFloat *tmComponents = CGColorGetComponents(color);
red = tmComponents[0];
green = tmComponents[1];
blue = tmComponents[2];
alpha = tmComponents[3];
}