Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.application |
Companion guide |
An NSColor object represents a color, which is defined in a color space, each point of which has a set of components (such as red, green, and blue) that uniquely define a color.
Color space
Color components
Various colorWith...
and colorUsing...
methods.
Preset colors: blackColor
, blueColor
, and so on.
colorUsingColorSpaceName
Creates an NSColor in the specified color space.
set
Sets the drawing color.
An NSColor object represents color and sometimes opacity (alpha). By sending a set
message to an NSColor instance, you set the color for the current drawing context. Setting the color causes subsequently drawn graphics to have the color represented by the NSColor instance.
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, methods such as redComponent
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 definitons of NSColor constants, as well as a discussion of their usage, see “About Color Spaces” in Color Programming Topics for Cocoa.
colorWithCalibratedHSB
colorWithCalibratedRGB
colorWithCalibratedWhite
colorWithCatalogName
colorWithDeviceCMYK
colorWithDeviceHSB
colorWithDeviceRGB
colorWithDeviceWhite
colorWithColorSpace
blackColor
blueColor
brownColor
clearColor
cyanColor
darkGrayColor
grayColor
greenColor
lightGrayColor
magentaColor
orangeColor
purpleColor
redColor
whiteColor
yellowColor
alternateSelectedControlColor
alternateSelectedControlTextColor
colorForControlTint
controlBackgroundColor
controlColor
controlAlternatingRowBackgroundColors
controlHighlightColor
controlLightHighlightColor
controlShadowColor
controlDarkShadowColor
controlTextColor
currentControlTint
disabledControlTextColor
gridColor
highlightColor
keyboardFocusIndicatorColor
knobColor
scrollBarColor
secondarySelectedControlColor
selectedControlColor
selectedControlTextColor
selectedMenuItemColor
selectedMenuItemTextColor
selectedTextBackgroundColor
selectedTextColor
selectedKnobColor
shadowColor
textBackgroundColor
textColor
windowBackgroundColor
windowFrameColor
windowFrameTextColor
alphaComponent
blackComponent
blueComponent
brightnessComponent
catalogNameComponent
colorNameComponent
cyanComponent
greenComponent
hueComponent
localizedCatalogNameComponent
localizedColorNameComponent
magentaComponent
redComponent
saturationComponent
whiteComponent
yellowComponent
colorSpaceName
colorUsingColorSpaceName
colorUsingColorSpaceNameAndDevice
colorSpace
colorUsingColorSpace
Returns the system color used for the face of a selected control—a control being clicked or dragged.
public static NSColor alternateSelectedControlColor
()
For use where iApp-like highlighting is desired. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for text in a selected control—a control being clicked or dragged.
public static NSColor alternateSelectedControlTextColor
()
or use where iApp-like highlighting is desired. For general information about system colors, see “Accessing System Colors”.
Returns an NSColor whose grayscale value is 0.0 and whose alpha value is 1.0.
public static NSColor blackColor
()
Returns an NSColor whose RGB value is 0.0, 0.0, 1.0 and whose alpha value is 1.0.
public static NSColor blueColor
()
Returns an NSColor whose RGB value is 0.6, 0.4, 0.2 and whose alpha value is 1.0.
public static NSColor brownColor
()
Returns an NSColor whose grayscale and alpha values are both 0.0.
public static NSColor clearColor
()
Returns the NSColor specified by controlTint, which is one of the tint settings.
public static NSColor colorForControlTint
(int controlTint)
Returns the NSColor currently on pasteBoard, or null
if pasteBoard doesn’t contain color data.
public static NSColor colorFromPasteboard
(NSPasteboard pasteBoard)
The returned color’s alpha component is set to 1.0 if ignoresAlpha
returns true
.
Creates and returns an NSColor whose opacity value is alpha and whose components in HSB space would be hue, saturation, and brightness.
public static NSColor colorWithCalibratedHSB
(float hue, float saturation, float brightness, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.)
Creates and returns an NSColor whose opacity value is alpha and whose RGB components are red, green, and blue.
public static NSColor colorWithCalibratedRGB
(float red, float green, float blue, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.)
Creates and returns an NSColor whose opacity value is alpha and whose grayscale value is white.
public static NSColor colorWithCalibratedWhite
(float white, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.)
Creates and returns an NSColor by finding the color named colorName in the catalog named listName, which may be a standard catalog.
public static NSColor colorWithCatalogName
(String listName, String colorName)
Note that the color must be defined in the named color space to retrieve it with this method.
Returns an NSColor object created from the specified components of color space space
public static NSColor colorWithColorSpace
(NSColorSpace space, float[] components)
. space must be a NSColorSpace object representing a color space. The number of components in the components array should match the number dictated by the specified color space plus one for alpha. Throws an exception if they do not match. The order of the components is determined by the color-space profile, with the alpha component always last. (If you want the created color to be opaque, specify 1.0 for the alpha component.) If space represents a color space that cannot cannot be used with NSColor objects—for example, a “pattern” color space—the method returns null
.
Creates and returns an NSColor whose opacity value is alpha and whose CMYK components are cyan, magenta, yellow, and black.
public static NSColor colorWithDeviceCMYK
(float cyan, float magenta, float yellow, float black, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.) In PostScript, this color space corresponds directly to the device-dependent operator setcmykcolor
.
Creates and returns an NSColor whose opacity value is alpha and whose components in HSB space would be hue, saturation, and brightness.
public static NSColor colorWithDeviceHSB
(float hue, float saturation, float brightness, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.) In PostScript, this color space corresponds directly to the device-dependent operator setrgbcolor
.
Creates and returns an NSColor whose opacity value is alpha and whose RGB components are red, green, and blue.
public static NSColor colorWithDeviceRGB
(float red, float green, float blue, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.) In PostScript, this color space corresponds directly to the device-dependent operator setrgbcolor
.
Creates and returns an NSColor whose opacity value is alpha and whose grayscale value is white.
public static NSColor colorWithDeviceWhite
(float white, float alpha)
(Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.) In PostScript, this color space corresponds directly to the device-dependent operator setgray
.
Creates and returns an NSColor that uses the pattern in image.
public static NSColor colorWithPatternImage
(NSImage image)
The image is tiled starting at the bottom of the window. The image is not scaled.
Returns an array containing the system specified background colors for alternating rows in tables and lists.
public static NSArray controlAlternatingRowBackgroundColors
()
You should not assume the array will contain only two colors. For general information on system colors, see “Accessing System Colors”.
Returns the system color used for the background of large controls such as browsers, table views, and clip views.
public static NSColor controlBackgroundColor
()
For general information on system colors, see “Accessing System Colors”.
Returns the system color used for the flat surfaces of a control.
public static NSColor controlColor
()
By default, the control color is a pattern color that will draw the ruled lines for the window background.
If you use controlColor
assuming that it is a solid, you may have an incorrect appearance. You should use lightGrayColor
in its place.
Returns the system color used for the dark edge of the shadow dropped from controls.
public static NSColor controlDarkShadowColor
()
Controls are displayed as though they were lit from the upper left. Two dark borders, representing shadows, run along the bottom and right. controlDarkShadowColor
returns the color of the outer, darker border. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the highlighted bezels of controls.
public static NSColor controlHighlightColor
()
Controls are displayed as though they were lit from the upper left. Two light borders, representing reflections from the light source, run along the top and left. controlHighlightColor
returns the color of the inner, duller border. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for light highlights in controls.
public static NSColor controlLightHighlightColor
()
Controls are displayed as though they were lit from the upper left. Two light borders, representing reflections from the light source, run along the top and left. controlLightHighlightColor
returns the color of the outer, brighter border. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the shadows dropped from controls.
public static NSColor controlShadowColor
()
Controls are displayed as though they were lit from the upper left. Two dark borders, representing shadows, run along the bottom and right. controlShadowColor
returns the color of the inner, lighter border. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for text on controls that aren’t disabled.
public static NSColor controlTextColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the current system control tint.
public static int currentControlTint
()
Returns an NSColor whose RGB value is 0.0, 1.0, 1.0 and whose alpha value is 1.0.
public static NSColor cyanColor
()
Returns an NSColor whose grayscale value is 1/3 and whose alpha value is 1.0.
public static NSColor darkGrayColor
()
Returns the system color used for text on disabled controls.
public static NSColor disabledControlTextColor
()
For general information about system colors, see “Accessing System Colors”.
Returns an NSColor whose grayscale value is 0.5 and whose alpha value is 1.0.
public static NSColor grayColor
()
Returns an NSColor whose RGB value is 0.0, 1.0, 0.0 and whose alpha value is 1.0.
public static NSColor greenColor
()
Returns the system color used for the optional gridlines in, for example, a table view.
public static NSColor gridColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color that represents the virtual light source on the screen.
public static NSColor highlightColor
()
This method is invoked by the highlightWithLevel
method. For general information about system colors, see “Accessing System Colors”.
Returns true
if the application doesn’t support alpha.
public static boolean ignoresAlpha
()
This value is consulted when an application imports alpha (through color dragging, for instance). The value determines whether the color panel has an opacity slider. This value is true
by default, indicating that the opacity components of imported colors will be set to 1.0. If an application wants alpha, it can invoke the setIgnoresAlpha
method with a parameter of false
.
Returns the system color that represents the keyboard focus ring around controls.
public static NSColor keyboardFocusIndicatorColor
()
Returns the system color used for the flat surface of a slider knob that hasn’t been selected.
public static NSColor knobColor
()
The knob’s beveled edges, which set it in relief, are drawn in highlighted and shadowed versions of the face color. When a knob is selected, its color changes to selectedKnobColor
. For general information about system colors, see “Accessing System Colors”.
Returns an NSColor whose grayscale value is 2/3 and whose alpha value is 1.0.
public static NSColor lightGrayColor
()
Returns an NSColor whose RGB value is 1.0, 0.0, 1.0 and whose alpha value is 1.0.
public static NSColor magentaColor
()
Returns an NSColor whose RGB value is 1.0, 0.5, 0.0 and whose alpha value is 1.0.
public static NSColor orangeColor
()
Returns an NSColor whose RGB value is 0.5, 0.0, 0.5 and whose alpha value is 1.0.
public static NSColor purpleColor
()
Returns an NSColor whose RGB value is 1.0, 0.0, 0.0 and whose alpha value is 1.0.
public static NSColor redColor
()
Returns the system color used for scroll “bars”—that is, for the groove in which a scroller’s knob moves
public static NSColor scrollBarColor
()
. For general information about system colors, see “Accessing System Colors”.
Returns the system color used in nonkey views.
public static NSColor secondarySelectedControlColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the face of a selected control—a control being clicked or dragged.
public static NSColor selectedControlColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for text in a selected control—a control being clicked or dragged.
public static NSColor selectedControlTextColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the slider knob when it is selected—that is, dragged.
public static NSColor selectedKnobColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the face of selected menu items.
public static NSColor selectedMenuItemColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the text in menu items.
public static NSColor selectedMenuItemTextColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the background of selected text.
public static NSColor selectedTextBackgroundColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for selected text.
public static NSColor selectedTextColor
()
For general information about system colors, see “Accessing System Colors”.
public static void setIgnoresAlpha
(boolean flag)
If flag is true
, the application won’t support alpha. In this case, no opacity slider is displayed in the color panel, and colors dragged in or pasted have their alpha values set to 1.0. By default, applications ignore alpha. Applications that need to import alpha can invoke this method with flag set to false
and explicitly make colors opaque in cases where it matters to them. Note that calling this with a value of true
overrides any value set with the NSColorPanel method setShowsAlpha
.
Returns the system color that represents the virtual shadows cast by raised objects on the screen.
public static NSColor shadowColor
()
This method is invoked by shadowWithLevel
. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the text background.
public static NSColor textBackgroundColor
()
When text is selected, its background color changes to the return value of selectedTextBackgroundColor
. For general information about system colors, see “Accessing System Colors”.
Returns the system color used for text.
public static NSColor textColor
()
When text is selected, its background color changes to the return value of selectedTextColor
. For general information about system colors, see “Accessing System Colors”.
Returns an NSColor whose grayscale and alpha values are both 1.0.
public static NSColor whiteColor
()
Returns a pattern color that will draw the ruled lines for the window background.
public static NSColor windowBackgroundColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for window frames, except for their text.
public static NSColor windowFrameColor
()
For general information about system colors, see “Accessing System Colors”.
Returns the system color used for the text in window frames.
public static NSColor windowFrameTextColor
()
For general information about system colors, see “Accessing System Colors”.
Returns an NSColor whose RGB value is 1.0, 1.0, 0.0 and whose alpha value is 1.0.
public static NSColor yellowColor
()
Returns the receiver’s alpha (opacity) component.
public float alphaComponent
()
Returns 1.0 (opaque) if the receiver has no alpha component.
Returns the receiver’s black component.
public float blackComponent
()
Throws an exception if the receiver isn’t a CMYK color.
Creates and returns an NSColor whose component values are a weighted sum of the receiver’s and color’s.
public NSColor blendedColorWithFractionOfColor
(float fraction, NSColor color)
The method converts color and a copy of the receiver to RGB, and then sets each component of the returned color to fraction of color’s value plus 1 – fraction of the receiver’s. Returns null
if the colors can’t be converted.
Returns the receiver’s blue component.
public float blueComponent
()
Throws an exception if the receiver isn’t an RGB color.
Returns the brightness component of the HSB color equivalent to the receiver.
public float brightnessComponent
()
Throws an exception if the receiver isn’t an RGB color.
Returns the name of the catalog containing the receiver’s name.
public String catalogNameComponent
()
Throws an exception if the receiver’s color space isn’t NSGraphics.NamedColorSpace
.
Returns the receiver’s name.
public String colorNameComponent
()
Throws an exception if the receiver’s color space isn’t NSGraphics.NamedColorSpace
.
Returns an object representing the color space of the receiver.
public NSColorSpace colorSpace
()
The returned NSColorSpace object may represent a custom color space.
Returns the name of the receiver’s color space.
public String colorSpaceName
()
This method should be implemented in subclasses of NSColor.
Returns a new color object representing the color of the receiver in the specified color space space.
public NSColor colorUsingColorSpace
(NSColorSpace space)
In creating the new NSColor object, this method converts the receiver’s color to an equivalent one in the new color space. Although the new color might have different component values, it looks the same as the original. The method returns the same NSColor object as the receiver if its color space is the same as the one specified. Returns nil
if conversion is not possible.
Creates and returns an NSColor whose color is the same as the receiver’s, except that the new NSColor is in the color space named colorSpace.
public NSColor colorUsingColorSpaceName
(String colorSpace)
If colorSpace is null
, the most appropriate color space is used.
Returns null
if the specified conversion cannot be done.
Creates and returns an NSColor whose color is the same as the receiver’s, except that the new NSColor is in the color space named colorSpace and is specific to the device described by deviceDescription.
public NSColor colorUsingColorSpaceNameAndDevice
(String colorSpace, NSDictionary deviceDescription)
Device descriptions can be obtained from windows, screens, and printers with the deviceDescription
method. If colorSpace is null
, the most appropriate color space is used.
If deviceDescription is null
, the current device (as obtained from the currently lockFocus’ed view’s window or, if printing, the current printer) is used.
Returns null
if the specified conversion cannot be done.
Creates and returns an NSColor that has the same color space and component values as the receiver, except its alpha component is alpha.
public NSColor colorWithAlphaComponent
(float alpha)
If the receiver’s color space doesn’t include an alpha component, the receiver is returned. A subclass with explicit opacity components should override this method to return a color with the specified alpha.
Returns the components of the receiver as an array of float
values.
public float[] components
()
You can invoke this method on NSColor objects created from custom color spaces to get the individual floating point components, including alpha. Throws an exception if the receiver doesn’t have floating-point components. To find out how many components are in the array, send the receiver a numberOfComponents
message.
Returns the receiver’s cyan component.
public float cyanComponent
()
Throws an exception if the receiver isn’t a CMYK color.
Draws the current color in the rectangle rect.
public void drawSwatchInRect
(NSRect rect)
Subclasses adorn the rectangle in some manner to indicate the type of color. This method is invoked by color wells, swatches, and other user interface objects that need to display colors.
Returns the receiver’s green component.
public float greenComponent
()
Throws an exception if the receiver isn’t an RGB color.
Returns an NSColor that represents a blend between the receiver and the highlight color returned by highlightColor
.
public NSColor highlightWithLevel
(float highlightLevel)
The highlight color’s contribution to the blend depends on highlightLevel, which should be a number from 0.0 through 1.0. (A highlightLevel below 0.0 is interpreted as 0.0 [the receiver]; a highlightLevel above 1.0 is interpreted as 1.0 [highlightLevel].)
Returns null
if the colors can’t be converted. Invoke this method when you want to brighten the receiving NSColor for use in highlights.
Returns the hue component of the HSB color equivalent to the receiver.
public float hueComponent
()
Throws an exception if the receiver isn’t an RGB color.
public String localizedCatalogNameComponent
()
Like catalogNameComponent
, but returns a localized string. This string may be displayed in user interface items like color pickers.
public String localizedColorNameComponent
()
Like colorNameComponent
, but returns a localized string. This string may be displayed in user interface items like color pickers.
Returns the receiver’s magenta component.
public float magentaComponent
()
Throws an exception if the receiver isn’t a CMYK color.
Returns the number of components in the receiver.
public int numberOfComponents
()
The floating-point components counted include alpha. Throws an exception if the receiver doesn’t have floating-point components.
Returns the image that the receiver is using as a pattern.
public NSImage patternImage
()
Throws an exception if the receiver doesn’t have an image.
Returns the receiver’s red component.
public float redComponent
()
Throws an exception if the receiver isn’t an RGB color.
Returns the saturation component of the HSB color equivalent to the receiver.
public float saturationComponent
()
Throws an exception if the receiver isn’t an RGB color.
Sets the color of subsequent drawing to the color that the receiver represents.
public void set
()
If the application is drawing to the screen rather than printing, this method also sets the current drawing context’s alpha value to the value returned by alphaComponent
; if the color doesn’t know about alpha, it’s set to 1.0. This method should be implemented in subclasses.
Sets the fill color of subsequent drawing to the receiver’s color.
public void setFill
()
If the application is drawing to the screen rather than printing, this method also sets the current drawing context’s alpha value to the value returned by alphaComponent
; if the color doesn’t know about alpha, it’s set to 1.0.
Subclasses of NSColor should implement this method.
Sets the stroke color of subsequent drawing to the receiver’s color.
public void setStroke
()
If the application is drawing to the screen rather than printing, this method also sets the current drawing context’s alpha value to the value returned by alphaComponent
; if the color doesn’t know about alpha, it’s set to 1.0.
Subclasses of NSColor should implement this method.
Returns an NSColor that represents a blend between the receiver and the shadow color returned by shadowColor
.
public NSColor shadowWithLevel
(float shadowLevel)
The shadow color’s contribution to the blend depends on shadowLevel, which should be a number from 0.0 through 1.0. (A shadowLevel below 0.0 is interpreted as 0.0 [the receiver]; a shadowLevel above 1.0 is interpreted as 1.0 [shadowLevel].)
Returns null
if the colors can’t be converted. Invoke this method when you want to darken the receiving NSColor for use in shadows.
Returns the receiver’s white component.
public float whiteComponent
()
Throws an exception if the receiver isn’t a grayscale color.
Writes the receiver’s data to pasteBoard, unless pasteBoard doesn’t support color data (in which case the method does nothing).
public void writeToPasteboard
(NSPasteboard pasteBoard)
Returns the receiver’s yellow component.
public float yellowComponent
()
Throws an exception if the receiver isn’t a CMYK color.
Sent when the system colors have been changed (such as through a system control panel interface).
This notification contains no notification object and no userInfo dictionary.
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)