我有一台黑色的UILabel;
我正在编写以下代码来获取黑色的组件。
UIColor *aColor = [aLabel.textColor retain];
const CGFloat* components = CGColorGetComponents(aColor.CGColor);
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();但这总是给出绿色分量而不是黑色分量;
有没有人知道这件事?
我传递的是不同的颜色空间吗?
:(请帮帮我。
发布于 2010-11-11 23:18:45
很可能是错误的色彩空间。我猜灰度颜色空间的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]];当然,这种方法并不适用于所有情况,您应该根据自己的需求采用它。
发布于 2011-04-03 07:45:55
您看到r=0、g=1、b=0、a=0的原因是因为您将返回数组中的值误解为RGB颜色模型中的值。UIColor将单色色彩空间用于灰度颜色,就像本例中的黑色一样。
您看到的是来自单色模型的2组件数组。第一个是灰度 (0表示黑色),第二个是alpha (1表示不透明)。您正在查看的最后两个值位于2元素数组的末尾,在本例中恰好是0。
你会注意到,如果颜色是黑色,你尝试CGColorGetNumberOfComponents(color.CGColor),它会返回2。如果您尝试使用CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)),它将返回对应于kCGColorSpaceModelMonochrome的0 (请参阅CGColorSpace.h中的枚举CGColorSpaceModel )
请参阅CGColorSpace Reference
发布于 2011-11-29 01:00:01
我认为这是一个非常好的方式来获得任何UIColor*的rgb表示,它已经有了一个方便的方法来保留它的组件。
-(CGColorRef)CGColorRefFromUIColor:(UIColor*)newColor {
CGFloat components[4] = {0.0,0.0,0.0,0.0};
[newColor getRed:&components[0] green:&components[1] blue:&components[2] alpha:&components[3]];
CGColorRef newRGB = CGColorCreate(CGColorSpaceCreateDeviceRGB(), components);
return newRGB;
}https://stackoverflow.com/questions/4155642
复制相似问题