NSColor
Extends NSColor
to support the Action Data controls and adds convenience methods for working with colors in a Codable
, Encodable
or Decodable
class.
Examples:
// Assign a color from a string
let color: NSColor ~= "#FF0000"
// Initialize a color from a hex string
let green = NSColor(fromHex: "00FF00")
// Convert color to a hex string
let white = NSColor.white.toHex()
-
Sets a
NSColor
from a hex string in the formatrrggbb
orrrggbbaa
where:rr
- Specifies the red component as a hex value in the range 00 to FF.gg
- Specifies the green component as a hex value in the range 00 to FF.bb
- Specifies the blue component as a hex value in the range 00 to FF.aa
- Specifies the alpha component as a hex value in the range 00 to FF.
The hex string can optionally start with the prefix of
#
.Examples:
// Assign a color from a string in macOS let color: NSColor ~= "#FF0000"
Declaration
Swift
public static func ~= ( left: inout NSColor, right: String)
-
Initializes a
NSColor
from a hex string in the formatrrggbb
orrrggbbaa
where:rr
- Specifies the red component as a hex value in the range 00 to FF.gg
- Specifies the green component as a hex value in the range 00 to FF.bb
- Specifies the blue component as a hex value in the range 00 to FF.aa
- Specifies the alpha component as a hex value in the range 00 to FF.
The hex string can optionally start with the prefix of
#
.Examples:
// Assign a color from a string in macOS let color = NSColor(fromHex: "#FF0000")
Declaration
Swift
public convenience init?(fromHex hex: String)
Parameters
hex
The hex value to convert to a
UIColor
. -
Initializes a
NSColor
for the given grayscale percent and alph value.Example:
let gray = NSColor(fromGrayScaleShade: 200)
Declaration
Swift
public convenience init(fromGrayScaleShade shade:Int, withAlphaPercent alphaPercent: Int = 100)
Parameters
shade
A number from 0 to 255 specifying the shade of gray to mix.
alphaPercent
A number from 0 to 100 specifying the opacity of the color.
-
Initializes a
NSColor
from the given red, green, blue and alpha values.Example:
let white = NSColor(red: 255, green: 255, blue: 255)
Declaration
Swift
public convenience init(red: Int, green: Int, blue: Int, alpha: Int = 100)
Parameters
red
A number between 0 and 255.
green
A number between 0 and 255.
blue
A number between 0 and 255.
alpha
A percentage between 0 and 100.
-
Initialize a
NSColor
from the given tuple of red, green, blue and alpha values where:red
- Is a number between 0 and 255.green
- Is a number between 0 and 255.blue
- Is a number between 0 and 255.alpha
- Is a percentage between 0 and 100.
Example:
let white = NSColor(fromRGBA: (red: 255, green: 255, blue: 255, alpha: 100))
Declaration
Swift
public convenience init (fromRGBA rgbComponents: (red: Int, green: Int, blue: Int, alpha: Int))
Parameters
rgbComponents
A tuple of red, green, blue and alpha values.
-
Initializes a
NSColor
from the given hue, saturation, brightness and alpha values.Example:
let red = NSColor(hue: 0, saturation: 100, brightness: 100)
Declaration
Swift
public convenience init (hue: Int, saturation: Int, brightness: Int, alpha: Int = 100)
Parameters
hue
A number between 0 and 360.
saturation
A percentage between 0 and 100.
brightness
A percentage between 0 and 100.
alpha
A percentage between 0 and 100.
-
Initializes a
NSColor
from the given tuple of hue, saturation, brightness and alpha values where:hue
- Is a number between 0 and 360.saturation
- Is a percentage between 0 and 100.brightness
- Is a percentage between 0 and 100.alpha
- Is a percentage between 0 and 100.
Example:
let red = NSColor(fromHSBA: (hue: 0, saturation: 100, brightness: 100, alpha: 100))
Declaration
Swift
public convenience init(fromHSBA hsbComponents: (hue: Int, saturation: Int, brightness: Int, alpha: Int))
Parameters
hsbComponents
A tuple of hue, saturation, brightness and alpha values.
-
Returns the red, green, blue and alpha components of the given color where:
red
- Is a number between 0 and 255.green
- Is a number between 0 and 255.blue
- Is a number between 0 and 255.alpha
- Is a percentage between 0 and 100.
Example:
let components = NSColor.red.rgbComponents print(components.red)
Declaration
Swift
public var rgbComponents: (red: Int, green: Int, blue: Int, alpha: Int)
-
Returns the alpha value of the color as a percentage between 0 and 100.
Example:
let alpha = NSColor.red.alphaValue
Declaration
Swift
public var alphaValue: Int
-
Returns the red value of the color as a number between 0 and 255.
Example:
let red = NSColor.red.redValue
Declaration
Swift
public var redValue: Int
-
Returns the green value of the color as a number between 0 and 255.
Example:
let green = NSColor.red.greenValue
Declaration
Swift
public var greenValue: Int
-
Returns the blue value of the color as a number between 0 and 255.
Example:
let blue = NSColor.red.blueValue
Declaration
Swift
public var blueValue: Int
-
Returns the hue, saturation, brightness (value) and alpha component of the given color where:
hue
- Is a number between 0 and 360.saturation
- Is a percentage between 0 and 100.brightness
- Is a percentage between 0 and 100.alpha
- Is a percentage between 0 and 100.
Example:
let components = NSColor.red.hsbComponents print(components.hue)
Declaration
Swift
public var hsbComponents: (hue: Int, saturation: Int, brightness: Int, alpha: Int)
-
Returns the hue value of the color as a number between 0 and 360.
Example:
let hue = NSColor.red.hueValue
Declaration
Swift
public var hueValue: Int
-
Returns the saturation value of the color as a percentage between 0 and 100.
Example:
let saturation = NSColor.red.saturationValue
Declaration
Swift
public var saturationValue: Int
-
Returns the brightness value of the color as a percentage between 0 and 100.
Example:
let brightness = NSColor.red.brightnessValue
Declaration
Swift
public var brightnessValue: Int
-
Returns the shade and alpha of a given gray scale color where:
shade
- Is a number between 0 and 255.alpha
- Is a percentage between 0 and 100.
Example:
let components = NSColor.gray.grayScaleComponents print(components.shade)
Declaration
Swift
public var grayScaleComponents: (shade: Int, alpha: Int)
-
Returns the gray scale shade of the color as a number between 0 and 255.
Example:
let shade = NSColor.gray.shadeValue
Declaration
Swift
public var shadeValue: Int
-
Converts a
NSColor
to a hex string in the formatrrggbb
orrrggbbaa
where:rr
- Specifies the red component as a hex value in the range 00 to FF.gg
- Specifies the green component as a hex value in the range 00 to FF.bb
- Specifies the blue component as a hex value in the range 00 to FF.aa
- Specifies the alpha component as a hex value in the range 00 to FF.
The hex string can optionally start with the prefix of
#
.Examples:
// Assign a color from a string in macOS let colorHex = NSColor.red.toHex(); let colorHexShort = NSColor.red.toHex(withPrefix: false, includeAlpha: false);
- hash: If
true
, the string will be prefixed with the#
character. - alpha: If
true
, the string will include the alpha information.
Declaration
Swift
public func toHex(withPrefix hash: Bool = true, includeAlpha alpha: Bool = true) -> String
Return Value
The
UIColor
represented as a hex string. -
Returns a color based off of this color with the given alpha value.
Example:
let color = NSColor.white.withAlphaValue(50)
Declaration
Swift
public func withAlphaValue(_ alpha: Int) -> NSColor
Parameters
alpha
A percentage between 0 and 100.
-
Returns a color based off of this color with the given red value.
Example:
let color = NSColor.white.withRedValue(255)
Declaration
Swift
public func withRedValue(_ red: Int) -> NSColor
Parameters
red
A number between 0 and 255.
-
Returns a color based off of this color with the given green value.
Example:
let color = NSColor.white.withGreenValue(255)
Declaration
Swift
public func withGreenValue(_ green: Int) -> NSColor
Parameters
green
A number between 0 and 255.
-
Returns a color based off of this color with the given blue value.
Example:
let color = NSColor.white.withBlueValue(255)
Declaration
Swift
public func withBlueValue(_ blue: Int) -> NSColor
Parameters
blue
A number between 0 and 255.
-
Returns a color based off of this color with the given hue value.
Example:
let color = NSColor.white.withHueValue(100)
Declaration
Swift
public func withHueValue(_ hue: Int) -> NSColor
Parameters
hue
A number between 0 and 360.
-
Returns a color based off of this color with the given saturation value.
Example:
let color = NSColor.white.withSaturationValue(100)
Declaration
Swift
public func withSaturationValue(_ saturation: Int) -> NSColor
Parameters
saturation
A percentage between 0 and 100.
-
Returns a color based off of this color with the given brightness value.
Example:
let color = NSColor.white.withBrightnessValue(100)
Declaration
Swift
public func withBrightnessValue(_ brightness: Int) -> NSColor
Parameters
brightness
A percentage between 0 and 100.