Accessing a Color’s Components

There’s usually no need to retrieve the individual components of a color, but when needed, you can retrieve either a set of components (using such methods as getRed:green:blue:alpha:) or an individual component (using such methods as redComponent). However, it’s illegal to ask an NSColor for components that aren’t defined for its color space. You can identify the color space by sending a colorSpaceName message to the NSColor object. If you need to ask an NSColor for components that aren’t in its color space (for instance, when you’ve gotten the color from the color panel), first convert the color to the appropriate color space using the colorUsingColorSpaceName: method. If the color is already in the specified color space, you get the same color back; otherwise you get a conversion that’s usually lossy or that’s correct only for the current device. You get back nil if the specified conversion can’t be done.