Skip to content

Instantly share code, notes, and snippets.

@billbonney
Last active April 3, 2018 02:13
Show Gist options
  • Save billbonney/f59b494d3c2aa47fc45cb248a6012b73 to your computer and use it in GitHub Desktop.
Save billbonney/f59b494d3c2aa47fc45cb248a6012b73 to your computer and use it in GitHub Desktop.
Simple Color Picker UIView for iOS
//
// Copyright (c) 2018 Bill Bonney <bill@communistech.com>. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import UIKit
@objc protocol ColorPickerDelegate{
func picked(color: UIColor)
}
class ColorPicker: UIView {
var cursorColor = UIColor.white
var cursorWidth = CGFloat(3.0)
var _selectedColor = UIColor.black
var selectedColor: UIColor {
set {
selectedColor(newValue)
}
get {
return _selectedColor
}
}
weak var delegate: ColorPickerDelegate?
override func draw(_ rect: CGRect) {
// Color Bar
let width = Int(self.frame.size.width)
for i in 0..<width {
let xPosition = CGFloat(i)
UIColor(hue: xPosition/self.frame.size.width, // convert to a value between 0.0 -> 1.0
saturation: 1.0,
brightness: 1.0,
alpha: 1.0).set()
let colorBar = CGRect(x: xPosition, y: 0.0,
width: 1.0, height: self.frame.size.height);
UIRectFill(colorBar);
}
// Cursor Bar
let positionX = getCursorPosition(forColor: selectedColor)
cursorColor.set()
let cursorBar = CGRect(x: positionX, y: 0.0, width: cursorWidth, height: self.frame.size.height)
UIRectFill(cursorBar)
}
// Changes the selected color, updates the UI, and notifies the delegate.
private func selectedColor(_ color: UIColor){
if (color != selectedColor) {
_selectedColor = color
setNeedsDisplay()
self.delegate?.picked(color: selectedColor)
}
}
private func getCursorPosition(forColor color: UIColor) -> CGFloat {
var hue: CGFloat = 0.0
var saturation: CGFloat = 1.0
var brightness: CGFloat = 1.0
var alpha: CGFloat = 1.0
if color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
return CGFloat(hue * self.frame.size.width);
}
return 0.0
}
private func updateColor(_ touch: UITouch){
let xPosition = touch.location(in: self).x
if xPosition >= 0.0 && xPosition <= (frame.size.width-cursorWidth) {
selectedColor = UIColor(hue: (xPosition / self.frame.size.width),
saturation: 1.0, brightness: 1.0, alpha: 1.0)
self.delegate?.picked(color: selectedColor)
self.setNeedsDisplay()
}
}
}
extension ColorPicker {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
updateColor(touch)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
updateColor(touch)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
updateColor(touch)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment