Color

public struct Color

Constants

Hexadecimal

  • The maximum representable integer for each color component.

    Declaration

    Swift

    private static let maxHex: Int
  • Initializes a new Color instance of given hexadecimal color string.

    Given string will be stripped from a single leading “#”, if applicable. Resulting string must met any of the following criteria:

    • Is a string with 8-characters and therefore a fully fledged hexadecimal color representation including an alpha component. Given value will remain untouched before conversion. Example: ffeebbaa
    • Is a string with 6-characters and therefore a fully fledged hexadecimal color representation excluding an alpha component. Given RGB color components will remain untouched and an alpha component of 0xff (opaque) will be extended before conversion. Example: ffeebb -> ffeebbff
    • Is a string with 4-characters and therefore a shortened hexadecimal color representation including an alpha component. Each single character will be doubled before conversion. Example: feba -> ffeebbaa
    • Is a string with 3-characters and therefore a shortened hexadecimal color representation excluding an alpha component. Given RGB color character will be doubled and an alpha of component of 0xff (opaque) will be extended before conversion. Example: feb -> ffeebbff

    Throws

    .invalidColor if given string does not match any of the above mentioned criteria or is not a valid hex color.

    Declaration

    Swift

    public init(hex string: String, leadingAlpha: Bool = false) throws

    Parameters

    string

    The hexadecimal color string.

    leadingAlpha

    Indicate whether given string should be treated as ARGB (true) or RGBA (false)

  • Initializes a new Color instance of given hexadecimal color values.

    Declaration

    Swift

    public init(hex color: Int, leadingAlpha: Bool = false)

    Parameters

    color

    The hexadecimal color value, incl. red, green, blue and alpha

    leadingAlpha

    Indicate whether given code should be treated as ARGB (true) or RGBA (false)

Private helper

  • Sanitizes given hexadecimal color string (strips # and forms proper length).

    Throws

    .invalidColor if given string is not of proper length

    Declaration

    Swift

    private static func sanitize(hex string: String, leadingAlpha: Bool) throws -> String

    Parameters

    string

    The hexadecimal color string to sanitize

    leadingAlpha

    Indicate whether given and returning string should be treated as ARGB (true) or RGBA (false)

    Return Value

    The sanitized hexadecimal color string