WTUIColorExtensions 1.1.1

WTUIColorExtensions 1.1.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Wagner Truppel.



  • By
  • Wagner Truppel

WTUIColorExtensions

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.