Color

public typealias Color = UIColor

This extesion adds some useful functions to UIColor or NSColor.

  • Create a color from HEX with alpha.

    Declaration

    Swift

    public convenience init(hex: Int, alpha: CGFloat = 1.0)

    Parameters

    hex

    HEX value.

    alpha

    Alpha value.

  • Create a color from a HEX string. It supports the following type:

    • #ARGB, ARGB if alphaFirst is true. #RGBA, RGBA if alphaFirst is false.
    • #ARGB.
    • #RRGGBB.
    • AARRGGBB, AARRGGBB if alphaFirst is true. #RRGGBBAA, RRGGBBAA if firstIsAlpha is false.

    Declaration

    Swift

    public convenience init(hex: String, alphaFirst: Bool = false)

    Parameters

    hexString

    HEX string.

    alphaFirst

    Set it to true if alpha value is the first in the HEX string. If alpha value is the last one, set it to false. Default is false.

  • A good contrasting color, it will be either black or white.

    Declaration

    Swift

    public func contrasting() -> Color

    Return Value

    Returns the color.

  • A complementary color that should look good.

    Declaration

    Swift

    public func complementary() -> Color?

    Return Value

    Returns the color.

  • Check if the color is in RGB format.

    Declaration

    Swift

    public func canProvideRGBComponents() -> Bool

    Return Value

    Returns if the color is in RGB format.

  • Create a random color.

    Declaration

    Swift

    public static func random(alpha: CGFloat = 1.0) -> Color

    Parameters

    alpha

    Alpha value.

    Return Value

    Returns the UIColor or NSColor instance.

  • Create an UIColor or NSColor from a given string. Example: blue or hex string.

    Declaration

    Swift

    public static func color(string color: String) -> Color

    Parameters

    color

    String with color.

    Return Value

    Returns the created UIColor or NSColor.

  • Create an UIColor from a given string like blue or an hex string.

    Declaration

    Swift

    public convenience init(string color: String)

    Parameters

    color

    String with color.

  • Create a NSColor from a given string like blue or an hex string.

  • Creates and returns a color object that has the same color space and component values as the given color, but has the specified alpha component.

    Declaration

    Swift

    public static func color(color: Color, alpha: CGFloat) -> Color

    Parameters

    color

    UIColor or NSColor value.

    alpha

    Alpha value.

    Return Value

    Returns an UIColor or NSColor instance.