Return to the Alphabetic Index
Return to the Class Browser
Return to the Picture Browser
Copyright (c) 1994 by NeXT Computer, Inc. All Rights Reserved.

NSColor

Inherits From: NSObject

Conforms To: NSCoding, NSCopying NSObject (NSObject)

Declared In: AppKit/NSColor.h

An NSColor represents a color. The color can be a grayscale value and can include alpha (opacity) information. By sending a set message to an NSColor instance, you set the color for the current PostScript drawing context. This causes subsequently drawn graphics to have the color represented by the NSColor instance.

A color is defined in some particular color space. A color space consists of a set of dimensionssuch as red, green, and blue in the case of RGB space. Each point in the space represents a unique color, and the point's location along each dimension is called a component. An individual color is usually specified by the numeric values of its components, which range from 0.0 to 1.0. For instance, a pure red is specified in RGB space by the component values 1.0, 0.0, and 0.0.

Some color spaces include an alpha component, which defines the color's opacity. An alpha value of 1.0 means completely opaque, and 0.0 means completely transparent. The alpha component is ignored when the color is used on a device that doesn't support alpha, such as a printer.

There are three kinds of color space in OpenStep:

. Device-dependent. This means that a given color might not look the same on different displays and printers.

. Device-independent, also known as calibrated. With this sort of color space, a given color should look the same on all devices.

. Named. The named color space has components that aren't numeric values, but simply names in various catalogs of colors. Named colors come with lookup tables that provide the ability to generate the correct color on a given device.

OpenStep includes six different color spaces, referred to by these enumeration constants:

NSDeviceCMYKColorSpace Cyan, magenta, yellow, black, and alpha components

NSDeviceWhiteColorSpace White and alpha components

NSDeviceRGBColorSpace Red, green, blue, and alpha components

Hue, saturation, brightness, and alpha components

NSCalibratedWhiteColorSpace White and alpha components

NSCalibratedRGBColorSpace Red, green, blue, and alpha components

Hue, saturation, brightness, and alpha components

NSNamedColorSpace Catalog name and color name components

(Color spaces whose names start with NSDevice are device-dependent; those with NSCalibrated are device-independent.)

There's usually no need to retrieve the individual components of a color, but when needed, you can either retrieve 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 method to the NSColor. 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 might also get back nil if the specified conversion can't be done.

Subclasses of NSColor need to implement the colorSpaceName and set methods, as well as the methods that return the components for that color space and the methods in the NSCoding protocol. Some other methodssuch as colorWithAlphaComponent:, isEqual:, and colorUsingColorSpaceName:device:may also be implemented if they make sense for the color space. Mutable subclasses (if any) should additionally implement copyWithZone: to provide a true copy.

Creating an NSColor from Component Values

Creating an NSColor With Preset Components

Ignoring Alpha Components

Retrieving a Set of Components

Retrieving Individual Components

Converting to Another Color Space

Copying and Pasting

Drawing