forked from donjin123/ColorPickerWheel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorWheelView.swift
240 lines (208 loc) · 8.39 KB
/
ColorWheelView.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//
// ColorWheelView.swift
// color
//
// Created by Donjin_Master on 05/10/2018.
// Copyright © 2018 Donjin. All rights reserved.
import UIKit
public protocol ColorWheelProtocol {
func colorPick(color: UIColor)
}
@IBDesignable
public class ColorWheelView: UIView {
/// wheel center with scale
private var wCenters: CGPoint = CGPoint.zero
/// wheel width with scale
private var wWidths: Int = 0
/// wheel height with scale
private var wHeights: Int = 0
/// wheel radius with scale
private var wRadiuss: CGFloat = 0
private var scale = UIScreen.main.scale
private var wheelImageView: UIImageView?
private var shadow: UIView!
private var picker: UIView!
private var lastLocation: CGPoint!
private var brightnessVal: CGFloat = 1
@IBInspectable
public var brightness: CGFloat {
get {return self.brightnessVal}
set {
if newValue > 1.0 {
self.brightnessVal = 1.0
} else if newValue < 0.0 {
self.brightnessVal = 0.0
} else {
self.brightnessVal = newValue
}
self.shadow.alpha = 1 - self.brightnessVal
self.sendRGBValue(x: self.picker.center.x * self.scale,
y: self.picker.center.y * self.scale)
}
}
var delegate: ColorWheelProtocol?
public override init(frame: CGRect) {
super.init(frame: frame)
guard self.bounds != CGRect.zero else {
// avoid error when xib preview
return
}
self.prepare()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
guard self.bounds != CGRect.zero else {
// avoid error when xib preview
return
}
self.prepare()
}
override public func prepareForInterfaceBuilder() {
// For storyboard/ xib only
// won't run at program initialization
self.prepare()
}
private func prepare() {
var rect: CGRect = self.bounds
if rect.width != rect.height {
let minVal: CGFloat = min(rect.width, rect.height)
rect = CGRect(origin: frame.origin,
size: CGSize(width: minVal,
height: minVal))
}
self.setup(rect: rect)
self.setColorWheel()
self.setColorPicker()
self.sendRGBValue(x: self.picker.center.x * self.scale,
y: self.picker.center.y * self.scale)
}
private func setup(rect: CGRect) {
self.layer.borderWidth = 0
self.layer.cornerRadius = 0.5 * rect.size.width
self.layer.masksToBounds = true
self.backgroundColor = UIColor.white
self.wRadiuss = rect.width / 2 * self.scale
self.wWidths = Int(rect.width * self.scale)
self.wHeights = Int(rect.height * self.scale)
self.wCenters = CGPoint(x: self.bounds.width * self.scale / 2,
y: self.bounds.height * self.scale / 2)
}
}
// MARK: Color wheel related
extension ColorWheelView {
private func setColorWheel() {
let circleImg = self.buildHueCircle()
if self.wheelImageView != nil {
self.wheelImageView!.image = circleImg
} else {
self.wheelImageView = UIImageView(image: circleImg)
self.insertSubview(self.wheelImageView!, at: 0)
self.createShadow()
}
}
private func buildHueCircle() -> UIImage? {
let space = CGColorSpaceCreateDeviceRGB()
let bWidth = Int(self.bounds.width * self.scale)
let bHeight = Int(self.bounds.height * self.scale)
let context = CGContext(data: nil, width: bWidth, height: bHeight, bitsPerComponent: 8, bytesPerRow: bWidth * 4, space: space, bitmapInfo: Pixel.bitmapInfo)!
let buffer = context.data!
let pixels = buffer.bindMemory(to: Pixel.self, capacity: bWidth * bHeight)
let heightStart = Int(self.wCenters.y - 0.5 * CGFloat(self.wHeights))
let widthStart = Int(self.wCenters.x - 0.5 * CGFloat(self.wWidths))
for y in heightStart ..< heightStart + self.wHeights {
for x in widthStart ..< widthStart + self.wWidths {
let pixel = self.pixel(y: y, x: x)
pixels[y * bWidth + x] = pixel
}
}
let cgImage = context.makeImage()!
return UIImage(cgImage: cgImage, scale: self.scale, orientation: .up)
}
private func pixel(y: Int, x: Int) -> Pixel{
var pixel: Pixel
let angle = fmod(atan2(CGFloat(y) - self.wCenters.y, CGFloat(x) - wCenters.x) + 2 * .pi, 2 * .pi)
let distance = hypot(CGFloat(x) - wCenters.x, CGFloat(y) - wCenters.y)
let saturation = min(1, distance / CGFloat(self.wWidths) * self.scale)
let value = UIColor(hue: angle / 2 / .pi, saturation: saturation, brightness: 1, alpha: 1)
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
value.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
if distance <= self.wRadiuss {
pixel = Pixel(red: UInt8(red * 255.0),
green: UInt8(green * 255.0),
blue: UInt8(blue * 255.0),
alpha: UInt8(alpha * 255.0))
} else {
pixel = Pixel(red: 255, green: 255, blue: 255, alpha: 0)
}
return pixel
}
private func createShadow() {
let width = CGFloat(self.wWidths) / self.scale
let frame = CGRect(x: 0, y: 0, width: width, height: width)
self.shadow = UIView(frame: frame)
self.shadow.backgroundColor = UIColor.black
self.shadow.layer.cornerRadius = 0.5 * width
self.shadow.alpha = 0
self.insertSubview(self.shadow,
aboveSubview: self.wheelImageView!)
}
}
// MARK: Color picker
extension ColorWheelView {
private func setColorPicker() {
let frame = CGRect(x: 0, y: 0, width: 18, height: 18)
self.picker = UIView(frame: frame)
let size = self.bounds.size
self.picker.center = CGPoint(x: size.width/2, y: size.height/2)
self.picker.layer.cornerRadius = 9
self.picker.layer.masksToBounds = false
self.picker.layer.borderColor = UIColor.black.cgColor
self.picker.layer.borderWidth = 0.5
self.picker.backgroundColor = UIColor.clear
self.drawCross(view: self.picker)
self.addSubview(self.picker)
let pan = UIPanGestureRecognizer(target: self, action: #selector(self.detectPan(ges:)))
self.picker.addGestureRecognizer(pan)
}
private func drawCross(view: UIView) {
let line1 = CALayer()
line1.frame = CGRect(x: view.bounds.width/2, y: 0, width: 0.5, height: view.bounds.height)
line1.backgroundColor = UIColor.black.cgColor
view.layer.addSublayer(line1)
let line2 = CALayer()
line2.frame = CGRect(x: 0, y: view.bounds.height/2, width: view.bounds.width, height: 0.5)
line2.backgroundColor = UIColor.black.cgColor
view.layer.addSublayer(line2)
}
@objc private func detectPan(ges: UIPanGestureRecognizer) {
if ges.state == .began {
self.lastLocation = self.picker.center
} else {
let translation = ges.translation(in: self)
let x = self.lastLocation.x + translation.x
let y = self.lastLocation.y + translation.y
let dis = hypot(CGFloat(x * self.scale) - self.wCenters.x, CGFloat(y * self.scale) - self.wCenters.y)
if dis <= self.wRadiuss {
self.picker.center = CGPoint(x: x, y: y)
self.sendRGBValue(x: x * self.scale,
y: y * self.scale)
}
}
}
private func sendRGBValue(x: CGFloat, y: CGFloat) {
let pixel = self.pixel(y: Int(y),
x: Int(x))
let red = CGFloat(pixel.red)
let green = CGFloat(pixel.green)
let blue = CGFloat(pixel.blue)
let color = UIColor(red: red/255.0, green: green/255.0, blue: blue/255.0, alpha: CGFloat(pixel.alpha)/255.0)
var hue: CGFloat = 0
var sat: CGFloat = 0
color.getHue(&hue, saturation: &sat, brightness: nil, alpha: nil)
let real = UIColor(hue: hue, saturation: sat, brightness: self.brightnessVal, alpha: 1)
self.delegate?.colorPick(color: real)
}
}