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

View File

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