Skip to content

An extension to `UIColor` adding the ability to generate pseudo-random color instances plus some other goodies.

License

Notifications You must be signed in to change notification settings

wltrup/iOS-Swift-WTUIColorExtensions

Repository files navigation

WTUIColorExtensions

CI Status Version License Platform

IMPORTANT

2020.02.21: This project has been deprecated and is replaced by ColorExtensions.

What

WTUIColorExtensions adds to UIColor the ability to generate pseudo-random color instances and methods to extract color components in a more direct way than using inout arguments and the default get... methods. Here's the full set of API additions:

    public static var randomRGB: UIColor { get }
    public static var randomRGBA: UIColor { get }

    public static var randomHSB: UIColor { get }
    public static var randomHSBA: UIColor { get }

    public static var randomWhite: UIColor { get }
    public static var randomWhiteAlpha: UIColor { get }

    public enum ColorError : Error {
        case invalidColorSpace
    }

    public struct RGBAComponents {
        public let red: CGFloat
        public let green: CGFloat
        public let blue: CGFloat
        public let alpha: CGFloat
    }
    public func rgbaComponents() throws -> RGBAComponents

    public struct HSBAComponents {
        public let hue: CGFloat
        public let saturation: CGFloat
        public let brightness: CGFloat
        public let alpha: CGFloat
    }
    public func hsbaComponents() throws -> HSBAComponents

    public struct WhiteAlphaComponents {
        public let white: CGFloat
        public let alpha: CGFloat
    }
    public func whiteAlphaComponents() throws -> WhiteAlphaComponents

    // NEW in v. 1.1.0
    public func hexValue() throws -> String
    public func hexValueWithAlpha() throws -> String

    // NEW in v. 1.1.0
    public static func quadraticLuma(red: CGFloat, green: CGFloat, blue: CGFloat) throws -> CGFloat
    public func quadraticLuma() throws -> CGFloat

    // NEW in v. 1.1.0
    public func contrastingColor(continuous: Bool, threshold: CGFloat, sameAlpha: Bool) throws -> UIColor
}

Why

Because generating random colors is a common enough need and because having to use inout arguments to extract color components is annoying.

Tests

WTUIColorExtensions adds several extensions, all tested by 26 tests with nearly 95% coverage. The missing 5% test-coverage is due to the fact that rgbaComponents() and hsbaComponents() don't actually throw any invalid color space errors on iOS, although in theory they could. In order to future-proof this library, I've assumed that those methods might fail but I can't currently test that code path.

Changelog

Changes to WTUIColorExtensions are listed here.

Installation

WTUIColorExtensions is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "WTUIColorExtensions"

Author

Wagner Truppel, [email protected]

License

WTUIColorExtensions is available under the MIT license. See the LICENSE file for more info.

About

An extension to `UIColor` adding the ability to generate pseudo-random color instances plus some other goodies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published