An object that stores color data and sometimes opacity (alpha value).
- macOS 10.0+
Many methods in AppKit require you to specify color data using an
NSColor object; when drawing you use them to set the current fill and stroke colors. Color objects are immutable and can be used safely from multiple threads in your app.Color objects can be created in a variety of ways:
AppKit provides standard system colors that you can use to match the colors of your own UI objects to those provided by AppKit. You can use these colors without needing to understand the underlying representation of color.
AppKit provides definitions for very basic colors, such as red, orange, yellow, and so on.
You can create a custom color in a variety of color spaces.
You can create a color object from another object, such as another
NSColorobject, a Core Graphics representation of a color, or a Core Image color.
You can create a color from an
NSImageobject, which is used to paint a repeating pattern instead of a solid color.
You can create a color by applying a transform to another
NSColorobject. For example, you might perform a blend operation between two colors, or you might create a color that represents the same color, but in a different color space.
Color and Color Spaces
A color object is typically represented internally as a Core Graphics color (
CGColor) in a Core Graphics color space (
CGColor). There are methods and properties that return the underlying color data. See Retrieving Component Values from aColor Object and Retrieving Individual Components.
Colors can be created in extended color spaces:
When working in an extended color space, color values are not clamped to fit inside the color gamut, meaning that component values may be less than
0 or greater than
1. When displayed on an sRGB display, such colors are outside the gamut and will not be rendered accurately. However, extended color spaces are useful working color spaces when you want a pixel format and representation that other color spaces can be easily converted into. For example, a color in the display P3 color space that is not within the sRGB color gamut can still be converted to an extended sRGB format. Some of its values will be outside of the
0-1 range. However, when displayed on a device with a P3 display gamut, it would still be rendered correctly.
In general, when you need to worry about color spaces, use these extended color spaces as working color spaces. When you need to worry about representing that color as closely as possible in a specific color space, convert the color from the extended color space into the target color space.
It is invalid to use an accessor method related to components of a particular color space on an
NSColor object that does not share the same color space model. For example, the
red property and
get method work on color objects that use an RGB color space model; if you access the property on a color object in the CMYK color space, an exception is raised. Further, the
get method and
number property work in color spaces that have individual components. Thus they return the components of color objects as individual floating-point values regardless of whether they’re based on
NSColor objects or named color spaces. However, older component-fetching methods such as
get are effective only on color objects based on named color spaces.
If you have a color 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.