-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHybridViewModelColorProperty.swift
More file actions
54 lines (44 loc) · 1.37 KB
/
HybridViewModelColorProperty.swift
File metadata and controls
54 lines (44 loc) · 1.37 KB
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
import NitroModules
import RiveRuntime
class HybridViewModelColorProperty: HybridViewModelColorPropertySpec, ValuedPropertyProtocol {
var property: ColorPropertyType!
lazy var helper = PropertyListenerHelper(property: property!)
init(property: ColorPropertyType) {
self.property = property
super.init()
}
var value: Double {
get {
return property.value.toHexDouble()
}
set {
property.value = UIColor(argb: Int(newValue))
}
}
func addListener(onChanged: @escaping (Double) -> Void) throws -> () -> Void {
return helper.addListener { (color: UIColor) in
onChanged(color.toHexDouble())
}
}
}
extension UIColor {
convenience init(argb: Int) {
let alpha = CGFloat((argb >> 24) & 0xFF) / 255.0
let red = CGFloat((argb >> 16) & 0xFF) / 255.0
let green = CGFloat((argb >> 8) & 0xFF) / 255.0
let blue = CGFloat(argb & 0xFF) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha)
}
func toHexDouble() -> Double {
var red: CGFloat = 0
var green: CGFloat = 0
var blue: CGFloat = 0
var alpha: CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let r = UInt32(red * 255)
let g = UInt32(green * 255)
let b = UInt32(blue * 255)
let a = UInt32(alpha * 255)
return Double((a << 24) | (r << 16) | (g << 8) | b)
}
}