我有一个黑色的 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]];
当然这个方法并不适用于所有情况,您应该根据您的需求采用它。
关于iphone - 如何正确获取 CGColor 的颜色分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155642/