CGColorGetComponents ( UIColor获取RGB值 用于颜色过渡) (一)

博客介绍了如何使用CGColorGetComponents方法从UIColor中获取RGB值,以便进行颜色过渡操作。内容包括个人实践验证的可行方法,并引用了StackOverflow上的相关讨论链接。
摘要由CSDN通过智能技术生成
方法一: CGFloat * colors = CGColorGetComponents ( hsbaColor . CGColor )
如:
UIColor *color = [UIColor colorWithRed:0.0 green:0.0 blue:1.0alpha:1.0];
const CGFloat *components =CGColorGetComponents(color.CGColor);
NSLog(@"Red: %f", components[0]);
NSLog(@"Green: %f", components[1]); 
NSLog(@"Blue: %f", components[2]);

CGColorGetComponents方法返回的是一个数组,存储的是R\G\B\ALPHA四个值,关于  CGColorGetComponents请看APPLE文档的描述.
CGColorGetComponents
Returns the values of the color components (including alpha)associated with a Quartz color.

const CGFloat * CGColorGetComponents (
    CGColorRef color
);
Parameters
color
A Quartz color.
Return Value
An array of intensity values for the color components(including alpha) associated with the specified color. The size ofthe array is one more than the number of components of the colorspace for the color.


但是,此方法只适用于RGB颜色空间,UIColor *color =[UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0];
如果是非RGB颜色空间   [UIColorgrayColor]就不能用这个方法了!

方法二:

Google了一下,找到一个方法,记录备忘。自己亲身试过可行!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
CGFloat R, G, B;
 
UIColor *uiColor = [lblDate textColor];
CGColorRef color = [uiColor CGColor];
int numComponents = CGColorGetNumberOfComponents(color);
 
if (numComponents == 4)
{
         
        const CGFloat *components = CGColorGetComponents(color);
         R = components[0];
         G = components[1];
         B 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值