2019-09-30 02:40:12 +02:00
|
|
|
//
|
|
|
|
// 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)
|
2019-10-30 02:48:07 +01:00
|
|
|
var pixelCount = 0
|
|
|
|
var totalLuminance = 0.0
|
2019-09-30 02:40:12 +02:00
|
|
|
|
|
|
|
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 luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
|
|
|
|
|
2019-10-30 02:48:07 +01:00
|
|
|
totalLuminance += luminance
|
|
|
|
pixelCount += 1
|
2019-09-30 02:40:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-10-30 02:48:07 +01:00
|
|
|
let avgLuminance = totalLuminance / Double(pixelCount)
|
|
|
|
return avgLuminance < 37.5
|
2019-09-30 02:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UIImage {
|
|
|
|
func isDark() -> Bool {
|
|
|
|
return self.cgImage?.isDark() ?? false
|
|
|
|
}
|
|
|
|
}
|