Merge pull request #1892 from flowinho/ios-release

Add background to pure white icons
This commit is contained in:
Maurice Parker 2020-03-13 10:04:56 -05:00 committed by GitHub
commit d0cf04bd4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 8 deletions

View File

@ -20,6 +20,10 @@ final class IconImage {
return image.isDark()
}()
lazy var isBright: Bool = {
return image.isBright()
}()
let image: RSImage
init(_ image: RSImage) {
@ -33,22 +37,48 @@ final class IconImage {
func isDark() -> Bool {
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)?.isDark() ?? false
}
func isBright() -> Bool {
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)?.Bright() ?? false
}
}
#else
extension UIImage {
func isDark() -> Bool {
return self.cgImage?.isDark() ?? false
}
func isBright() -> Bool {
return self.cgImage?.isBright() ?? false
}
}
#endif
fileprivate enum ImageLuminanceType {
case regular, bright, dark
}
extension CGImage {
func isDark() -> Bool {
guard let imageData = self.dataProvider?.data else { return false }
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
func isBright() -> Bool {
guard let imageData = self.dataProvider?.data, let luminanceType = getLuminanceType(from: imageData) else {
return false
}
return luminanceType == .bright
}
let length = CFDataGetLength(imageData)
func isDark() -> Bool {
guard let imageData = self.dataProvider?.data, let luminanceType = getLuminanceType(from: imageData) else {
return false
}
return luminanceType == .dark
}
fileprivate func getLuminanceType(from data: CFData) -> ImageLuminanceType? {
guard let ptr = CFDataGetBytePtr(data) else {
return nil
}
let length = CFDataGetLength(data)
var pixelCount = 0
var totalLuminance = 0.0
@ -68,10 +98,12 @@ extension CGImage {
}
let avgLuminance = totalLuminance / Double(pixelCount)
if totalLuminance == 0 {
return true
if totalLuminance == 0 || avgLuminance < 40 {
return .dark
} else if avgLuminance > 70 {
return .bright
} else {
return avgLuminance < 40
return .regular
}
}

View File

@ -24,8 +24,15 @@ final class IconView: UIView {
self.setNeedsLayout()
}
} else {
self.setNeedsLayout()
if self.iconImage?.isBright ?? false {
self.isDisconcernable = false
self.setNeedsLayout()
} else {
self.isDisconcernable = true
self.setNeedsLayout()
}
}
self.setNeedsLayout()
}
}
}