2019-09-30 02:40:12 +02:00
|
|
|
//
|
2019-11-06 01:05:57 +01:00
|
|
|
// IconImage.swift
|
2019-09-30 02:40:12 +02:00
|
|
|
// NetNewsWire
|
|
|
|
//
|
2019-11-06 01:05:57 +01:00
|
|
|
// Created by Maurice Parker on 11/5/19.
|
2019-09-30 02:40:12 +02:00
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2020-01-13 01:38:04 +01:00
|
|
|
#if os(macOS)
|
|
|
|
import AppKit
|
|
|
|
#else
|
|
|
|
import UIKit
|
|
|
|
#endif
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
import RSCore
|
|
|
|
|
|
|
|
final class IconImage {
|
|
|
|
|
|
|
|
lazy var isDark: Bool = {
|
|
|
|
return image.isDark()
|
|
|
|
}()
|
|
|
|
|
|
|
|
let image: RSImage
|
|
|
|
|
|
|
|
init(_ image: RSImage) {
|
|
|
|
self.image = image
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
extension NSImage {
|
|
|
|
func isDark() -> Bool {
|
|
|
|
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)?.isDark() ?? false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
extension UIImage {
|
|
|
|
func isDark() -> Bool {
|
|
|
|
return self.cgImage?.isDark() ?? false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-30 02:40:12 +02:00
|
|
|
|
|
|
|
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]
|
2020-01-11 20:25:31 +01:00
|
|
|
let a = ptr[i + 3]
|
2019-09-30 02:40:12 +02:00
|
|
|
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
|
|
|
|
|
2020-01-11 20:25:31 +01:00
|
|
|
if Double(a) > 0 {
|
|
|
|
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)
|
2020-01-11 20:25:31 +01:00
|
|
|
if totalLuminance == 0 {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return avgLuminance < 40
|
|
|
|
}
|
2019-09-30 02:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-17 02:44:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
enum IconSize: Int, CaseIterable {
|
|
|
|
case small = 1
|
|
|
|
case medium = 2
|
|
|
|
case large = 3
|
|
|
|
|
|
|
|
private static let smallDimension = CGFloat(integerLiteral: 24)
|
|
|
|
private static let mediumDimension = CGFloat(integerLiteral: 36)
|
|
|
|
private static let largeDimension = CGFloat(integerLiteral: 48)
|
|
|
|
|
|
|
|
var size: CGSize {
|
|
|
|
switch self {
|
|
|
|
case .small:
|
|
|
|
return CGSize(width: IconSize.smallDimension, height: IconSize.smallDimension)
|
|
|
|
case .medium:
|
|
|
|
return CGSize(width: IconSize.mediumDimension, height: IconSize.mediumDimension)
|
|
|
|
case .large:
|
|
|
|
return CGSize(width: IconSize.largeDimension, height: IconSize.largeDimension)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|