-
Notifications
You must be signed in to change notification settings - Fork 34
/
SwiftHSVColorPicker.swift
executable file
·90 lines (75 loc) · 3.49 KB
/
SwiftHSVColorPicker.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// SwiftHSVColorPicker.swift
// SwiftHSVColorPicker
//
// Created by johankasperi on 2015-08-20.
//
import UIKit
public class SwiftHSVColorPicker: UIView {
var colorWheel: ColorWheel!
var brightnessView: BrightnessView!
var selectedColorView: SelectedColorView!
public var color: UIColor!
var hue: CGFloat = 1.0
var saturation: CGFloat = 1.0
var brightness: CGFloat = 1.0
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.clearColor()
}
override public init(frame: CGRect) {
super.init(frame: frame)
}
public func setViewColor(color: UIColor) {
var hue: CGFloat = 0.0, saturation: CGFloat = 0.0, brightness: CGFloat = 0.0, alpha: CGFloat = 0.0
let ok: Bool = color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
if (!ok) {
print("SwiftHSVColorPicker: exception <The color provided to SwiftHSVColorPicker is not convertible to HSV>")
}
self.hue = hue
self.saturation = saturation
self.brightness = brightness
self.color = color
setup()
}
func setup() {
// Remove all subviews
let views = self.subviews
for view in views {
view.removeFromSuperview()
}
let selectedColorViewHeight: CGFloat = 44.0
let brightnessViewHeight: CGFloat = 26.0
// let color wheel get the maximum size that is not overflow from the frame for both width and height
let colorWheelSize = min(self.bounds.width, self.bounds.height - selectedColorViewHeight - brightnessViewHeight)
// let the all the subviews stay in the middle of universe horizontally
let centeredX = (self.bounds.width - colorWheelSize) / 2.0
// Init SelectedColorView subview
selectedColorView = SelectedColorView(frame: CGRect(x: centeredX, y:0, width: colorWheelSize, height: selectedColorViewHeight), color: self.color)
// Add selectedColorView as a subview of this view
self.addSubview(selectedColorView)
// Init new ColorWheel subview
colorWheel = ColorWheel(frame: CGRect(x: centeredX, y: CGRectGetMaxY(selectedColorView.frame), width: colorWheelSize, height: colorWheelSize), color: self.color)
colorWheel.delegate = self
// Add colorWheel as a subview of this view
self.addSubview(colorWheel)
// Init new BrightnessView subview
brightnessView = BrightnessView(frame: CGRect(x: centeredX, y: CGRectGetMaxY(colorWheel.frame), width: colorWheelSize, height: brightnessViewHeight), color: self.color)
brightnessView.delegate = self
// Add brightnessView as a subview of this view
self.addSubview(brightnessView)
}
func hueAndSaturationSelected(hue: CGFloat, saturation: CGFloat) {
self.hue = hue
self.saturation = saturation
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
brightnessView.setViewColor(self.color)
selectedColorView.setViewColor(self.color)
}
func brightnessSelected(brightness: CGFloat) {
self.brightness = brightness
self.color = UIColor(hue: self.hue, saturation: self.saturation, brightness: self.brightness, alpha: 1.0)
colorWheel.setViewBrightness(brightness)
selectedColorView.setViewColor(self.color)
}
}