我有一个黑色的 UILabel;
我正在编写以下代码来获取黑色的组件。
UIColor *aColor = [aLabel.textColor retain];
const CGFloat* components = CGColorGetComponents(aColor.CGColor);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
但总是给出绿色分量而不是黑色分量;
有人对此有想法吗?
我是否传递了不同的色彩空间?
:-( 请帮我。
最有可能的是错误的色彩空间。我猜想灰度色彩空间的 alpha 分量达到了您认为绿色分量应该在的位置。
我使用这个函数从 UIColor 创建一个字符串,我只遇到 RGB 和灰度颜色空间,所以我只是将少于 4 个 (R+G+B+A) 分量的每种颜色解释为灰度。
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]];
当然这个方法并不适用于所有情况,您应该根据您的需求采用它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)