Skip to content

Commit b4474e6

Browse files
committed
Uses color of button for tool picker background
1 parent 6d0e00d commit b4474e6

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import UIKit
2+
3+
extension UIImage {
4+
var firstPixelColor: UIColor? {
5+
guard let inputImage = CIImage(image: self) else {
6+
return nil
7+
}
8+
var bitmap = [UInt8](repeating: 0, count: 4)
9+
let context = CIContext(options: [.workingColorSpace: kCFNull as Any])
10+
let bounds = CGRect(x: 0, y: 0, width: size.width, height: size.height)
11+
context.render(inputImage, toBitmap: &bitmap, rowBytes: 4, bounds: bounds, format: .RGBA8, colorSpace: nil)
12+
let red = CGFloat(bitmap[0]) / 255
13+
let green = CGFloat(bitmap[1]) / 255
14+
let blue = CGFloat(bitmap[2]) / 255
15+
let alpha = CGFloat(bitmap[3]) / 255
16+
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
17+
}
18+
}

Sources/KeyboardToolbar/Helpers/UIView+Helpers.swift

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,15 @@ extension UIView {
55
guard let window = window else {
66
return nil
77
}
8-
let rect = window.convert(frame, from: superview)
9-
let image = UIGraphicsImageRenderer(bounds: rect).image { _ in
10-
window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
8+
let buttonFrame = window.convert(frame, from: superview)
9+
let bounds = CGRect(x: buttonFrame.minX + point.x, y: buttonFrame.minY, width: 1, height: 1)
10+
let imageRenderer = UIGraphicsImageRenderer(bounds: bounds)
11+
let format = UIGraphicsImageRendererFormat()
12+
format.scale = 1
13+
format.opaque = true
14+
let image = imageRenderer.image { _ in
15+
window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
1116
}
12-
return image.color(at: point)
13-
}
14-
}
15-
16-
private extension UIImage {
17-
func color(at point: CGPoint) -> UIColor? {
18-
guard let pixelData = cgImage?.dataProvider?.data else {
19-
return nil
20-
}
21-
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
22-
let pixelInfo = ((Int(size.width) * Int(point.y)) + Int(point.x)) * 4
23-
let r = CGFloat(data[pixelInfo]) / CGFloat(255)
24-
let g = CGFloat(data[pixelInfo + 1]) / CGFloat(255)
25-
let b = CGFloat(data[pixelInfo + 2]) / CGFloat(255)
26-
let a = CGFloat(data[pixelInfo + 3]) / CGFloat(255)
27-
return UIColor(red: r, green: g, blue: b, alpha: a)
17+
return image.firstPixelColor
2818
}
2919
}

Sources/KeyboardToolbar/Views/KeyboardToolButton.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ final class KeyboardToolButton: UIButton {
3838
}
3939
}
4040
}
41+
private var toolPickerBackgroundColor: UIColor {
42+
// Get the background color from a screenshot to ensure we get the right color for all keyboards.
43+
// This is in particular relevant when using dark keyboards where both the keyboard and the buttons are transulcent.
44+
let colorSamplePoint = CGPoint(x: frame.size.width / 2, y: 0)
45+
return color(at: colorSamplePoint) ?? .black
46+
}
4147

4248
init(item: KeyboardToolGroupItem) {
4349
self.item = item
@@ -118,10 +124,7 @@ private extension KeyboardToolButton {
118124
}
119125

120126
@objc private func touchDown(_ sender: UIButton, event: UIEvent) {
121-
// Get the background color from a screenshot to ensure we get the right color for all keyboards.
122-
// This is in particular relevant when using dark keyboards where both the keyboard and the buttons are transulcent.
123-
let colorSamplePoint = CGPoint(x: bounds.midX, y: 2)
124-
toolPickerBackgroundView.fillColor = color(at: colorSamplePoint) ?? .black
127+
toolPickerBackgroundView.fillColor = toolPickerBackgroundColor
125128
UIDevice.current.playInputClick()
126129
cancelToolPickerTimer()
127130
guard !item.allTools.isEmpty else {

0 commit comments

Comments
 (0)