mirror of
https://github.com/Ranchero-Software/NetNewsWire.git
synced 2025-01-11 01:04:05 +01:00
Add isBright function to Shared Logic
This commit is contained in:
parent
0a3713e078
commit
d282181269
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user