@@ -34,12 +34,19 @@ class HomeManager {
34
34
for j in 0 ..< colors {
35
35
var bitmap = [ UInt8] ( repeating: 0 , count: 4 )
36
36
let context = CIContext ( )
37
- let inputExtent = CIVector ( x: ( extent. size. width/ CGFloat( colors) ) * CGFloat( i) , y: ( extent. size. height/ CGFloat( colors) ) * CGFloat( j) , z: extent. size. width/ CGFloat( colors) , w: extent. size. height/ CGFloat( colors) )
38
- let filter = CIFilter ( name: " CIAreaAverage " , withInputParameters: [ kCIInputImageKey: image, kCIInputExtentKey: inputExtent] ) !
39
- let outputImage = filter. outputImage!
37
+ let x = ( extent. size. width / CGFloat( colors) ) * CGFloat( i)
38
+ let y = ( extent. size. height / CGFloat( colors) ) * CGFloat( j)
39
+ let width = extent. size. width / CGFloat( colors)
40
+ let height = extent. size. height / CGFloat( colors)
41
+ let inputExtent = CIVector ( x: x, y: y, z: width, w: height)
42
+ let inputParameters = [ kCIInputImageKey: image, kCIInputExtentKey: inputExtent]
43
+ guard let filter = CIFilter ( name: " CIAreaAverage " , withInputParameters: inputParameters) , let outputImage = filter. outputImage else {
44
+ break
45
+ }
40
46
let outputExtent = outputImage. extent
41
47
assert ( outputExtent. size. width == 1 && outputExtent. size. height == 1 )
42
- context. render ( outputImage, toBitmap: & bitmap, rowBytes: 4 , bounds: CGRect ( x: 0 , y: 0 , width: 1 , height: 1 ) , format: kCIFormatRGBA8, colorSpace: CGColorSpaceCreateDeviceRGB ( ) )
48
+ let bounds = CGRect ( x: 0 , y: 0 , width: 1 , height: 1 )
49
+ context. render ( outputImage, toBitmap: & bitmap, rowBytes: 4 , bounds: bounds, format: kCIFormatRGBA8, colorSpace: CGColorSpaceCreateDeviceRGB ( ) )
43
50
samples. append ( bitmap)
44
51
}
45
52
}
@@ -52,26 +59,27 @@ class HomeManager {
52
59
}
53
60
var colorIndex = 0
54
61
for lightbulbService in lightbulbServices {
62
+ let color = colors [ colorIndex % colors. count]
55
63
var HSBA = [ CGFloat] ( repeating: 0 , count: 4 )
56
- colors [ colorIndex % colors . count ] . getHue ( & HSBA[ 0 ] , saturation: & HSBA[ 1 ] , brightness: & HSBA[ 2 ] , alpha: & HSBA[ 3 ] )
64
+ color . getHue ( & HSBA[ 0 ] , saturation: & HSBA[ 1 ] , brightness: & HSBA[ 2 ] , alpha: & HSBA[ 3 ] )
57
65
for characteristic in lightbulbService. characteristics {
58
66
if updating [ characteristic] == nil {
59
67
updating [ characteristic] = false
60
68
}
61
69
guard updating [ characteristic] == false else {
62
70
break
63
71
}
64
- if characteristic. characteristicType == HMCharacteristicTypePowerState {
65
- update ( characteristic , floatValue : 1 )
66
- }
67
- if characteristic . characteristicType == HMCharacteristicTypeBrightness {
68
- update ( characteristic, floatValue: Float ( HSBA [ 2 ] ) )
69
- }
70
- if characteristic . characteristicType == HMCharacteristicTypeSaturation {
71
- update ( characteristic , floatValue : Float ( HSBA [ 1 ] ) )
72
- }
73
- if characteristic . characteristicType == HMCharacteristicTypeHue {
74
- update ( characteristic , floatValue : Float ( HSBA [ 0 ] ) )
72
+ switch characteristic. characteristicType {
73
+ case HMCharacteristicTypePowerState :
74
+ update ( characteristic , floatValue : 1 )
75
+ case HMCharacteristicTypeBrightness :
76
+ update ( characteristic, floatValue: Float ( HSBA [ 2 ] ) )
77
+ case HMCharacteristicTypeSaturation :
78
+ update ( characteristic , floatValue : Float ( HSBA [ 1 ] ) )
79
+ case HMCharacteristicTypeHue :
80
+ update ( characteristic , floatValue : Float ( HSBA [ 0 ] ) )
81
+ default :
82
+ break
75
83
}
76
84
}
77
85
colorIndex += 1
@@ -88,7 +96,8 @@ extension HomeManager {
88
96
updating [ characteristic] = true
89
97
characteristic. writeValue ( value, completionHandler: { error in
90
98
if error != nil {
91
- DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + . seconds( 1 ) , execute: { [ weak self] in
99
+ let deadline : DispatchTime = . now( ) + . seconds( 1 )
100
+ DispatchQueue . global ( ) . asyncAfter ( deadline: deadline, execute: { [ weak self] in
92
101
self ? . updating [ characteristic] = false
93
102
} )
94
103
} else {
@@ -112,7 +121,11 @@ extension HomeManager {
112
121
let diffRed = abs ( CGFloat ( components [ 0 ] ) - CGFloat( diffComponents [ 0 ] ) )
113
122
let diffGreen = abs ( CGFloat ( components [ 1 ] ) - CGFloat( diffComponents [ 1 ] ) )
114
123
let diffBlue = abs ( CGFloat ( components [ 2 ] ) - CGFloat( diffComponents [ 2 ] ) )
115
- let color = UIColor ( red: CGFloat ( components [ 0 ] ) / 255.0 , green: CGFloat ( components [ 1 ] ) / 255.0 , blue: CGFloat ( components [ 2 ] ) / 255.0 , alpha: CGFloat ( components [ 3 ] ) / 255.0 )
124
+ let red = CGFloat ( components [ 0 ] ) / 255
125
+ let green = CGFloat ( components [ 1 ] ) / 255
126
+ let blue = CGFloat ( components [ 2 ] ) / 255
127
+ let alpha = CGFloat ( components [ 3 ] ) / 255
128
+ let color = UIColor ( red: red, green: green, blue: blue, alpha: alpha)
116
129
diffColors [ color] = ( diffColors [ color] ?? 0 ) + diffRed + diffGreen + diffBlue
117
130
}
118
131
}
0 commit comments