UIColor
Extends UIColor
to support the Action Data controls and adds convenience methods for working with colors in a Codable
, Encodable
or Decodable
class.
iOS, tvOS and watchOS Examples:
// Assign a color from a string
let color: UIColor ~= "#FF0000"
// Initialize a color from a hex string
let green = UIColor(fromHex: "00FF00")
// Convert color to a hex string
let white = UIColor.white.toHex()
-
Sets a
UIColor
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 iOS, tvOS or watchOS let color: UIColor ~= "#FF0000"
Declaration
Swift
public static func ~= ( left: inout UIColor, right: String)
-
Initializes a
UIColor
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 iOS, tvOS or watchOS let color = UIColor(fromHex: "#FF0000")
Declaration
Swift
public convenience init?(fromHex hex: String)
Parameters
hex
The hex value to convert to a
UIColor
. -
Initializes a
UIColor
for the given grayscale percent and alph value.Example:
let gray = UIColor(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
UIColor
from the given red, green, blue and alpha values.Example:
let white = UIColor(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
UIColor
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 = UIColor(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
UIColor
from the given hue, saturation, brightness and alpha values.Example:
let red = UIColor(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
UIColor
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 = UIColor(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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.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 = UIColor.gray.grayScaleComponents print(components.shade)
Remarks
This property assumes that the color is actually a shade of gray. Internally thered
component is returned as theshade
value.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 = UIColor.gray.shadeValue
Declaration
Swift
public var shadeValue: Int
-
Converts a
UIColor
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 iOS, tvOS or watchOS let colorHex = UIColor.red.toHex(); let colorHexShort = UIColor.red.toHex(withPrefix: false, includeAlpha: false);
Declaration
Swift
public func toHex(withPrefix hash: Bool = true, includeAlpha alpha: Bool = true) -> String
Parameters
hash
If
true
, the string will be prefixed with the#
character.alpha
If
true
, the string will include the alpha information.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 = UIColor.white.withAlphaValue(50)
Declaration
Swift
public func withAlphaValue(_ alpha: Int) -> UIColor
Parameters
alpha
A percentage between 0 and 100.
-
Returns a color based off of this color with the given red value.
Example:
let color = UIColor.white.withRedValue(255)
Declaration
Swift
public func withRedValue(_ red: Int) -> UIColor
Parameters
red
A number between 0 and 255.
-
Returns a color based off of this color with the given green value.
Example:
let color = UIColor.white.withGreenValue(255)
Declaration
Swift
public func withGreenValue(_ green: Int) -> UIColor
Parameters
green
A number between 0 and 255.
-
Returns a color based off of this color with the given blue value.
Example:
let color = UIColor.white.withBlueValue(255)
Declaration
Swift
public func withBlueValue(_ blue: Int) -> UIColor
Parameters
blue
A number between 0 and 255.
-
Returns a color based off of this color with the given hue value.
Example:
let color = UIColor.white.withHueValue(100)
Declaration
Swift
public func withHueValue(_ hue: Int) -> UIColor
Parameters
hue
A number between 0 and 360.
-
Returns a color based off of this color with the given saturation value.
Example:
let color = UIColor.white.withSaturationValue(100)
Declaration
Swift
public func withSaturationValue(_ saturation: Int) -> UIColor
Parameters
saturation
A percentage between 0 and 100.
-
Returns a color based off of this color with the given brightness value.
Example:
let color = UIColor.white.withBrightnessValue(100)
Declaration
Swift
public func withBrightnessValue(_ brightness: Int) -> UIColor
Parameters
brightness
A percentage between 0 and 100.