根据HEX颜色创建UIColor和获取UIColor颜色分量

    如何根据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];
阅读更多
文章标签: hex components float c
个人分类: iphone
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

根据HEX颜色创建UIColor和获取UIColor颜色分量

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭