- Inherits from:
- NSObject
- Package:
- com.apple.yellow.application
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.
Various colorWith... and colorUsing... methods. | |
Preset colors: blackColor, blueColor, etc. |
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. 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 dimensions-such 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 spaces in the Application Kit:
NSColors provided by the Application Kit use eight different color spaces, referred to by these global String variables:
Global Variable | Color Space Description |
NSGraphics.DeviceCMYKColorSpace |
Cyan, magenta, yellow, black, and alpha components |
NSGraphics.DeviceWhiteColorSpace |
White and alpha components |
NSGraphics.DeviceRGBColorSpace |
Red, green, blue, and alpha componentsHue, saturation, brightness, and alpha components |
NSGraphics.CalibratedWhiteColorSpace |
White and alpha components |
NSGraphics.CalibratedRGBColorSpace |
Red, green, blue, and alpha componentsHue, saturation, brightness, and alpha components |
NSGraphics.NamedColorSpace |
Catalog name and color name components |
Color spaces whose names start with "Device" are device-dependent; those whose names start with "Calibrated" are device-independent.
There's usually no need to retrieve the individual components
of a color, but when needed, you can (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 null
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. Some other methods-such as colorWithAlphaComponent and colorUsingColorSpaceNameAndDevice-may also be implemented if they make sense for the color space.
NSColor has a number of methods which return "system"
colors; colors that are controlled by user preferences. These colors-including controlColor, textColor, and selectedTextColor-should
be used by developers who want to create custom controls or subclass
existing controls while honoring the user's color preferences. System
colors are implemented as named colors in a special color list named
"System
." To extract the components
of a system color, you must use NSColor's colorUsingColorSpaceName method to
convert the color to a color space known to respond to the component
accessor methods you need.
A SystemColorsDidChangeNotification is sent when the system colors have been changed (such as through a system control panel interface). If you have any non-system colors that depend on the system colors, you can change them when you receive this notification.
- Constructors
- NSColor
- Creating an NSColor object from Component Values
- colorWithCalibratedHSB
- colorWithCalibratedRGB
- colorWithCalibratedWhite
- colorWithCatalogName
- colorWithDeviceCMYK
- colorWithDeviceHSB
- colorWithDeviceRGB
- colorWithDeviceWhite
- Creating an NSColor With Preset Components
- blackColor
- blueColor
- brownColor
- clearColor
- cyanColor
- darkGrayColor
- grayColor
- greenColor
- lightGrayColor
- magentaColor
- orangeColor
- purpleColor
- redColor
- whiteColor
- yellowColor
- Creating a system color-an NSColor whose value is specified by user preferences
- controlBackgroundColor
- controlColor
- controlHighlightColor
- controlLightHighlightColor
- controlShadowColor
- controlDarkShadowColor
- controlTextColor
- disabledControlTextColor
- gridColor
- highlightColor
- knobColor
- scrollBarColor
- selectedControlColor
- selectedControlTextColor
- selectedMenuItemColor
- selectedMenuItemTextColor
- selectedTextBackgroundColor
- selectedTextColor
- selectedKnobColor
- shadowColor
- textBackgroundColor
- textColor
- windowFrameColor
- windowFrameTextColor
- Ignoring Alpha Components
- ignoresAlpha
- setIgnoresAlpha
- Copying and Pasting
- colorFromPasteboard
- writeToPasteboard
- Retrieving Individual Components
- alphaComponent
- blackComponent
- blueComponent
- brightnessComponent
- catalogNameComponent
- colorNameComponent
- cyanComponent
- greenComponent
- hueComponent
- localizedCatalogNameComponent
- localizedColorNameComponent
- magentaComponent
- redComponent
- saturationComponent
- whiteComponent
- yellowComponent
- Converting to Another Color Space
- colorSpaceName
- colorUsingColorSpaceName
- colorUsingColorSpaceNameAndDevice
- Changing the Color
- blendedColorWithFractionOfColor
- colorWithAlphaComponent
- highlightWithLevel
- shadowWithLevel
- Drawing
- drawSwatchInRect
- set
public NSColor()
public static NSColor blackColor()
NSGraphics.CalibratedRGBColorSpace
whose
grayscale value is 0.0 and whose alpha value is 1.0.See Also: blackComponent
public static NSColor blueColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 0.0, 0.0, 1.0 and whose alpha value is 1.0.See Also: blueComponent
public static NSColor brownColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 0.6, 0.4, 0.2 and whose alpha value is 1.0.public static NSColor clearColor()
NSGraphics.CalibratedWhiteColorSpace
whose
grayscale and alpha values are both 0.0.public static NSColor colorFromPasteboard(NSPasteboard pasteBoard)
null
if
the pasteboard doesn't contain color data. The
returned color's alpha component is set to 1.0 if ignoresAlpha returns true
.See Also: writeToPasteboard
public static NSColor colorWithCalibratedHSB(
float hue,
float saturation,
float brightness,
float alpha)
NSGraphics.CalibratedRGBColorSpace
,
whose opacity value is alpha, and
whose components in HSB space would be hue, saturation,
and brightness. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.)See Also: colorWithCalibratedRGB, colorWithDeviceHSB, getHue:saturation:brightness:alpha:
public static NSColor colorWithCalibratedRGB(
float red,
float green,
float blue,
float alpha)
NSGraphics.CalibratedRGBColorSpace
,
whose opacity value is alpha, and
whose RGB components are red, green,
and blue. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.)See Also: colorWithCalibratedHSB, colorWithDeviceRGB, getRed:green:blue:alpha:
public static NSColor colorWithCalibratedWhite(
float white,
float alpha)
NSGraphics.CalibratedWhiteColorSpace
,
whose opacity value is alpha, and
whose grayscale value is white. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.)See Also: colorWithDeviceWhite, getWhite:alpha:
public static NSColor colorWithCatalogName(
String listName,
String colorName)
NSGraphics.NamedColorSpace
,
by finding the color named colorName in
the catalog named listName, which
may be a standard catalog.See Also: catalogNameComponent, colorNameComponent, localizedCatalogNameComponent
public static NSColor colorWithDeviceCMYK(
float cyan,
float magenta,
float yellow,
float black,
float alpha)
NSGraphics.DeviceCMYKColorSpace
,
whose opacity value is alpha, and
whose CMYK components are cyan, magenta, yellow,
and black. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.) In PostScript, this colorspace corresponds directly to
the device-dependent operator setcmykcolor
.See Also: getCyan:magenta:yellow:black:alpha:
public static NSColor colorWithDeviceHSB(
float hue,
float saturation,
float brightness,
float alpha)
NSGraphics.DeviceRGBColorSpace
,
whose opacity value is alpha, and
whose components in HSB space would be hue, saturation,
and brightness. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.) In PostScript, this colorspace corresponds directly to
the device-dependent operator setrgbcolor
.See Also: colorWithCalibratedHSB, colorWithDeviceRGB, getHue:saturation:brightness:alpha:
public static NSColor colorWithDeviceRGB(
float red,
float green,
float blue,
float alpha)
NSGraphics.DeviceRGBColorSpace
,
whose opacity value is alpha, and
whose RGB components are red, green,
and blue. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.) In PostScript, this colorspace corresponds directly to
the device-dependent operator setrgbcolor
.See Also: colorWithCalibratedRGB, colorWithDeviceHSB, getRed:green:blue:alpha:
public static NSColor colorWithDeviceWhite(
float white,
float alpha)
NSGraphics.DeviceWhiteColorSpace
,
whose opacity value is alpha, and
whose grayscale value is white. (Values
below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted
as 1.0.) In PostScript, this colorspace corresponds directly to
the device-dependent operator setgray
.See Also: colorWithCalibratedWhite, getWhite:alpha:
public static NSColor controlBackgroundColor()
public static NSColor controlColor()
The return value of controlColor is also the system color used for window backgrounds. For general information about system colors, see "System Colors" .
public static NSColor controlDarkShadowColor()
See Also: controlShadowColor
public static NSColor controlHighlightColor()
See Also: controlLightHighlightColor
public static NSColor controlLightHighlightColor()
See Also: controlHighlightColor
public static NSColor controlShadowColor()
See Also: controlDarkShadowColor
public static NSColor controlTextColor()
See Also: disabledControlTextColor
public static NSColor cyanColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 0.0, 1.0, 1.0 and whose alpha value is 1.0.See Also: cyanComponent
public static NSColor darkGrayColor()
NSGraphics.CalibratedRGBColorSpace
whose
grayscale value is 1/3 and whose alpha value is 1.0.See Also: lightGrayColor, grayColor
public static NSColor disabledControlTextColor()
See Also: controlTextColor
public static NSColor grayColor()
NSGraphics.CalibratedWhiteColorSpace
whose
grayscale value is 0.5 and whose alpha value is 1.0.See Also: lightGrayColor, darkGrayColor
public static NSColor greenColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 0.0, 1.0, 0.0 and whose alpha value is 1.0.See Also: greenComponent
public static NSColor gridColor()
public static NSColor highlightColor()
See Also: highlightWithLevel
public static boolean ignoresAlpha()
true
if
the application doesn't support alpha. 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
.See Also: setIgnoresAlpha, alphaComponent
public static NSColor keyboardFocusIndicatorColor()
public static NSColor knobColor()
public static NSColor lightGrayColor()
NSGraphics.CalibratedRGBColorSpace
whose
grayscale value is 2/3 and whose alpha value is 1.0.See Also: grayColor, darkGrayColor
public static NSColor magentaColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 1.0, 0.0, 1.0 and whose alpha value is 1.0.See Also: magentaComponent
public static NSColor orangeColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 1.0, 0.5, 0.0 and whose alpha value is 1.0.public static NSColor purpleColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 0.5, 0.0, 0.5 and whose alpha value is 1.0.public static NSColor redColor()
See Also: redComponent
public static NSColor scrollBarColor()
public static NSColor selectedControlColor()
See Also: selectedControlTextColor
public static NSColor selectedControlTextColor()
See Also: selectedControlColor
public static NSColor selectedKnobColor()
See Also: knobColor
public static NSColor selectedMenuItemColor()
See Also: selectedMenuItemTextColor
public static NSColor selectedMenuItemTextColor()
See Also: selectedMenuItemColor
public static NSColor selectedTextBackgroundColor()
See Also: selectedTextColor
public static NSColor selectedTextColor()
See Also: selectedTextBackgroundColor
public static void setIgnoresAlpha(boolean flag)
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.See Also: ignoresAlpha, alphaComponent
public static NSColor shadowColor()
See Also: shadowWithLevel
public static NSColor textBackgroundColor()
See Also: textColor
public static NSColor textColor()
See Also: textBackgroundColor
public static NSColor whiteColor()
NSGraphics.CalibratedWhiteColorSpace
whose
grayscale and alpha values are both 1.0.See Also: whiteComponent
public static NSColor windowFrameColor()
See Also: windowFrameTextColor
public static NSColor windowFrameTextColor()
See Also: windowFrameColor
public static NSColor yellowColor()
NSGraphics.CalibratedRGBColorSpace
whose
RGB value is 1.0, 1.0, 0.0 and whose alpha value is 1.0.See Also: yellowComponent
public float alphaComponent()
See Also: getCyan:magenta:yellow:black:alpha:, getHue:saturation:brightness:alpha:, getRed:green:blue:alpha:, getWhite:alpha:
public float blackComponent()
See Also: getCyan:magenta:yellow:black:alpha:
public NSColor blendedColorWithFractionOfColor(
float fraction,
NSColor color)
NSGraphics.CalibratedRGBColorSpace
whose
component values are a weighted sum of the receiver's and color's. 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 to NSGraphics.CalibratedRGBColorSpace
.public float blueComponent()
See Also: getRed:green:blue:alpha:
public float brightnessComponent()
See Also: getHue:saturation:brightness:alpha:
public String catalogNameComponent()
NSGraphics.NamedColorSpace
.See Also: colorWithCatalogName, colorNameComponent, localizedCatalogNameComponent
public String colorNameComponent()
NSGraphics.NamedColorSpace
.See Also: colorWithCatalogName, catalogNameComponent, localizedCatalogNameComponent
public String colorSpaceName()
See Also: colorUsingColorSpaceName, colorUsingColorSpaceNameAndDevice
public NSColor colorUsingColorSpaceName(String colorSpace)
null
,
the most appropriate color space is used.Returns null
if
the specified conversion cannot be done.
See Also: colorSpaceName
public NSColor colorUsingColorSpaceNameAndDevice(
String colorSpace,
NSDictionary deviceDescription)
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.
See Also: colorSpaceName, colorUsingColorSpaceName
public NSColor colorWithAlphaComponent(float alpha)
See Also: alphaComponent, blendedColorWithFractionOfColor
public float cyanComponent()
See Also: getCyan:magenta:yellow:black:alpha:
public void drawSwatchInRect(NSRect rect)
public float greenComponent()
See Also: getRed:green:blue:alpha:
public NSColor highlightWithLevel(float highlightLevel)
NSGraphics.CalibratedRGBColorSpace
that
represents a blend between the receiver and the highlight color
returned by highlightColor. The
highlight color's contribution to the blend depends on highlightLevel,
which should be a number between 0.0 and 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 to NSGraphics.CalibratedRGBColorSpace
.
Invoke this method when you want to brighten the receiving NSColor
for use in highlights.
See Also: shadowWithLevel
public float hueComponent()
See Also: getHue:saturation:brightness:alpha:
public String localizedCatalogNameComponent()
See Also: colorWithCatalogName, colorNameComponent
public String localizedColorNameComponent()
See Also: colorWithCatalogName, catalogNameComponent, colorNameComponent, localizedCatalogNameComponent
public float magentaComponent()
See Also: getCyan:magenta:yellow:black:alpha:
public float redComponent()
See Also: getRed:green:blue:alpha:
public float saturationComponent()
See Also: getHue:saturation:brightness:alpha:
public void set()
public NSColor shadowWithLevel(float shadowLevel)
NSGraphics.CalibratedRGBColorSpace
that
represents a blend between the receiver and the shadow color returned
by shadowColor. The
shadow color's contribution to the blend depends on shadowLevel,
which should be a number between 0.0 and 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 to NSGraphics.CalibratedRGBColorSpace
.
Invoke this method when you want to darken the receiving NSColor
for use in shadows.
See Also: highlightWithLevel
public float whiteComponent()
See Also: getWhite:alpha:
public void writeToPasteboard(NSPasteboard pasteBoard)
See Also: colorFromPasteboard
public float yellowComponent()
See Also: getCyan:magenta:yellow:black:alpha:
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.