mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Add a background if the image is too dark and we are in dark mode.
This commit is contained in:
47
iOS/UIKit Extensions/UIImage-Extensions.swift
Normal file
47
iOS/UIKit Extensions/UIImage-Extensions.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
//
|
||||
// UIImage-Extensions.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Maurice Parker on 9/29/19.
|
||||
// Copyright © 2019 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension CGImage {
|
||||
|
||||
func isDark() -> Bool {
|
||||
guard let imageData = self.dataProvider?.data else { return false }
|
||||
guard let ptr = CFDataGetBytePtr(imageData) else { return false }
|
||||
|
||||
let length = CFDataGetLength(imageData)
|
||||
var visiblePixels = 0
|
||||
var darkPixels = 0
|
||||
|
||||
for i in stride(from: 0, to: length, by: 4) {
|
||||
|
||||
let r = ptr[i]
|
||||
let g = ptr[i + 1]
|
||||
let b = ptr[i + 2]
|
||||
let a = ptr[i + 3]
|
||||
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
|
||||
|
||||
if Double(a) > 0.0 {
|
||||
visiblePixels += 1
|
||||
if luminance < 50 {
|
||||
darkPixels += 1
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return Double(darkPixels) / Double(visiblePixels) > 0.4
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension UIImage {
|
||||
func isDark() -> Bool {
|
||||
return self.cgImage?.isDark() ?? false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user