alpha组件 color_如何正确获取CGColor的颜色组件

I have a UILabel with black color;

i am writing the following code to get black color's components.

UIColor *aColor = [aLabel.textColor retain];

const CGFloat* components = CGColorGetComponents(aColor.CGColor);

CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();

but always this is giving Green color components instead of black color components;

Is any one have idea about this?

Am i passing different color space?

:-( Please help me.

解决方案

Most likely wrong colorspace. I guess the alpha component of the grayscale colorspace gets where you think the green component should be.

I use this function to create a string from UIColor, I only encounter RGB and Grayscale colorspaces, so I just interpret every color with less than 4 (R+G+B+A) components as grayscale.

if (CGColorGetNumberOfComponents(color.CGColor) < 4) {

const CGFloat *components = CGColorGetComponents(color.CGColor);

color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]];

}

if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {

NSLog(@"no rgb colorspace");

// do seomthing

}

const CGFloat *components = CGColorGetComponents(color.CGColor);

NSString *colorAsString = [NSString stringWithFormat:@"%f,%f,%f,%f", components[0], components[1], components[2], components[3]];

of course this method is not save for all cases, you should adopt it to your requirements.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值