Color

public class Color

This document describes the Color object which represents color and sometimes opacity (alpha value). You can use Color objects to store color data, and pass them between various C4 objects such as Shape, Image, etc.

Color internally wraps a CGColorSpaceRef called colorSpace, as well as a CGColorRef. From these two objects Color is able to properly maintain color data and convert it to / from other color objects such as UIColor, CIColor, Color, etc.

  • Initializes and returns a new color object. Defaults to black with 0 opacity (i.e. clear). let c = Color()

    Declaration

    Swift

    public init()
  • Initializes and returns a new Color object based on specified color values. let c = Color(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0) - parameter red: The red value for the new color [0.0 … 1.0] - parameter green: The green value for the new color [0.0 … 1.0] - parameter blue: The blue value for the new color [0.0 … 1.0] - parameter alpha: The alpha value for the new color [0.0 … 1.0]

    Declaration

    Swift

    public init(red: Double, green: Double, blue: Double, alpha: Double)

    Parameters

    red

    The red value for the new color [0.0 … 1.0]

    green

    The green value for the new color [0.0 … 1.0]

    blue

    The blue value for the new color [0.0 … 1.0]

    alpha

    The alpha value for the new color [0.0 … 1.0]

  • Initializes and returns a new Color object based on a provided CGColor object. let c = Color(UIColor.redColor().CGColor) - parameter color: A CGColor object that will be used to create a new Color.

    Declaration

    Swift

    public init(_ color: CGColorRef)

    Parameters

    color

    A CGColor object that will be used to create a new Color.

  • Initializes and returns a new Color object based on a provided UIColor object. let c = Color(UIColor.redColor()) - parameter color: A UIColor object whose components will be extrated to create a new Color.

    Declaration

    Swift

    public convenience init(_ color: UIColor)

    Parameters

    color

    A UIColor object whose components will be extrated to create a new Color.

  • Initializes and returns a new Color object made up of a repeating pattern based on a specified Image. let p = Color("pattern") - parameter pattern: a String, the name of an image to use as a pattern.

    Declaration

    Swift

    public convenience init(_ pattern: String)

    Parameters

    pattern

    a String, the name of an image to use as a pattern.

  • Initializes and returns a new Color object based on specified color values. let c = Color(red: 255, green: 0, blue: 0, alpha: 255) - parameter red: The red value for the new color [0 … 255] - parameter green: The green value for the new color [0 … 255] - parameter blue: The blue value for the new color [0 … 255] - parameter alpha: The alpha value for the new color [0 … 255]

    Declaration

    Swift

    public convenience init(red: Int, green: Int, blue: Int, alpha: Double)

    Parameters

    red

    The red value for the new color [0 … 255]

    green

    The green value for the new color [0 … 255]

    blue

    The blue value for the new color [0 … 255]

    alpha

    The alpha value for the new color [0 … 255]

  • Initializes and returns a new Color object based on a specified hex value. Remember to precede with 0x and include the alpha component at the end (i.e. 7th + 8th characters) let c = Color(0xFF0000FF) - parameter hexValue: A color value expressed in hexadecimal.

    Declaration

    Swift

    public convenience init(_ hexValue: UInt32)

    Parameters

    hexValue

    A color value expressed in hexadecimal.

  • The set of 3 color values + alpha that define the current color. - returns: An array of 4 Double values in the range [0.0 … 1.0]

    Declaration

    Swift

    public var components: [Double]

    Return Value

    An array of 4 Double values in the range [0.0 … 1.0]

  • red

    The value of the red component of the current color, [0.0 … 1.0] let c = Color() let redVal = c.red - returns: Double value in the range [0.0 … 1.0]

    Declaration

    Swift

    public var red: Double

    Return Value

    Double value in the range [0.0 … 1.0]

  • The value of the green component of the current color let c = Color() let greenVal = c.green - returns: Double value in the range [0.0 … 1.0]

    Declaration

    Swift

    public var green: Double

    Return Value

    Double value in the range [0.0 … 1.0]

  • The value of the blue component of the current color let c = Color() let blueVal = c.blue - returns: Double value in the range [0.0 … 1.0]

    Declaration

    Swift

    public var blue: Double

    Return Value

    Double value in the range [0.0 … 1.0]

  • The value of the alpha component of the current color. let c = Color() let alphaVal = c.alpha - returns: Double value in the range [0.0 … 1.0]

    Declaration

    Swift

    public var alpha: Double

    Return Value

    Double value in the range [0.0 … 1.0]

  • A CGColor representation of the current color. let c = Color() let cg = c.CGColor - returns: CGColorRef object that matches the color’s internalColor property

    Declaration

    Swift

    public var CGColor: CGColorRef

    Return Value

    CGColorRef object that matches the color’s internalColor property

  • Creates and returns a color object that has the same color space and component values as the receiver, but has the specified alpha component. let c = aColor.colorWithAlpha(0.2) - parameter alpha: The opacity value of the new UIColor object. - returns: A new color with a modified alpha component.

    Declaration

    Swift

    public func colorWithAlpha(alpha: Double) -> Color

    Parameters

    alpha

    The opacity value of the new UIColor object.

    Return Value

    A new color with a modified alpha component.