zakkhoyt / ColorPicKit

A collection of UIControls for picking colors on iPhone, iPad, and other iOS devices. Gradient slider, Hue slider, RGB sliders, HSB sliders, CMYK sliders, ImagePixelPicker, HSB Wheel, HSB Spectrum. Color related class extensions
MIT License
16 stars 4 forks source link

ColorPicKit

A collection of UIControls and class extensions for selecting and defining colors

![](https://img.shields.io/badge/Supports-Swift 3-green.svg) ![](https://img.shields.io/badge/Supports-Xcode 8-green.svg)

ColorPicKit Video

UIControls

ColorPicKit provides the following UIControls:

Usage

InterfaceBuilder

Programmatically


func setupWheel() {
    self.hsbWheel = HSBAWheel(frame: self.view.bounds)
    hsbWheel.addTarget(self, action: #selector(hsbWheelValueChanged), for: .valueChanged)
}
func hsbWheelValueChanged(sender: HSBAWheel) {
    let color = sender.color
}

Example

ColorPicKit includes a full example project. Clone this repository and open ColorPicKitExample.xcodeproj

Cocoapods

This pod is not yet in the Cocoapods trunk, so you will need to install using a branch.


pod 'ColorPicKit', :git => 'https://github.com/zakkhoyt/ColorPicKit', :branch => '1.2.3'

UIColor utilities

ColorPicKit also exposes several functions and data structures for working with hex strings, RGBA, HSBA, HSLA, CMYKA, YUVA, and interpolating a color between two known colors.


extension UIColor {

    public convenience init(hexString:String)
    public func hexString() -> String
    public class func colorWith(hexString: String, alpha: CGFloat = 1.0) -> UIColor
    public class func interpolateAt(value: CGFloat, betweenColor1 color1: UIColor, andColor2 color2: UIColor) -> UIColor

    // Create a color with any color type struct
    init(rgba: RGBA)
    init(hsba: HSBA)
    init(hsla: HSLA)
    init(cmyka: CMYKA)
    init(yuva: YUVA)

    // Easily convert to a different color format
    func rgba() -> RGBA
    func hsba() -> HSBA
    func hsla() -> HSLA
    func cmyka() -> CMYKA
    func yuva() -> YUVA
}

Where RGBA, HSBA, HSLA, CMYKA, and YUVA are defined as structs

// RGBA
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

// HSBA
init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)

// HSLA
init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat)

// CMYKA
init(cyan: CGFloat, magenta: CGFloat, yellow: CGFloat, black: CGFloat, alpha: CGFloat)

// YUVA
init(y: CGFloat, u: CGFloat, v: CGFloat, alpha: CGFloat)

UIImage and CVPixelBuffer utilities

Additionally, ColorPicKit exposes instance methods and class functions which allow you to convert UIImage to CVPixelBuffer, get CVPixelBuffer properties, and query a single pixel color.


extension UIImage {

    // Class functions
    public class func pixelColorOf(image: UIImage, at point: CGPoint) -> UIColor?
    public class func pixelBufferPropertiesOf(image: UIImage) -> (pixelBuffer: CVPixelBuffer?, size:CGSize, bytesPerRow: Int)
    public class func pixelBufferOf(image: UIImage) -> CVPixelBuffer?
    public class func getSizeOf(pixelBuffer: CVPixelBuffer) -> CGSize
    public class func getBytesPerRowOf(pixelBuffer: CVPixelBuffer) -> Int

    // Instance methods
    public func pixelColorAt(point: CGPoint) -> UIColor?
    public func pixelBuffer() -> CVPixelBuffer?
    public func pixelBufferProperties() -> (pixelBuffer: CVPixelBuffer?, size:CGSize, bytesPerRow: Int)
}
Examples
// Get color of center pixel
guard let image = UIImage(named: "mars_earth") else {
    return
}
let center = CGPoint(x: image.size.width / 2.0, y: image.size.height / 2.0)
let color = image.pixelColorOf(point: center)