如何将 NSColor 转换为 ccColor3B?NSColor 可以在任何颜色空间中,因此 redComponent 等有时不起作用。
问问题
596 次
2 回答
1
我不确定它是否会起作用,但您可以尝试:
NSColor *color = [NSColor redColor];
CGFloat red, green, blue;
[color getRed:&red green:&green blue:&blue alpha:NULL];
ccColor3B cccolor = {red / 255.0f, green / 255.0f, blue / 255.0f};
于 2013-09-12T08:09:44.010 回答
1
你猜怎么着,我在docs中找到了这个。
Can I Access the Components of Any NSColor Object?
It is invalid to use an accessor method related to components of a particular color space on an NSColor object that is not in that color space. For example, NSColor methods such as redComponent and getRed:green:blue:alpha: work on color objects in the calibrated and device RGB color spaces. If you send such a message to an NSColor object in the CMYK color space, an exception is raised.
If you have an NSColor object in an unknown color space and you want to extract its components, you should first convert the color object to a known color space before using the component accessor methods of that color space. For example:
NSColor *aColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if (aColor) {
float rcomp = [aColor redComponent];
}
If the color is already in the requested color space, you get back the original object.
不管怎么说,还是要谢谢你 ;-)
所以就我而言,我现在使用这个:
NSColor *colorConverted = [self._color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if (colorConverted) {
self.labelFormac.color = ccc3(colorConverted.redComponent * 255.0f, colorConverted.greenComponent * 255.0f, colorConverted.blueComponent * 255.0f);
}
于 2013-09-13T01:10:07.510 回答