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
|
2024-03-21 04:49:15 +01:00
|
|
|
import Core
|
2019-11-06 01:05:57 +01:00
|
|
|
|
|
|
|
final class IconImage {
|
|
|
|
|
|
|
|
lazy var isDark: Bool = {
|
|
|
|
return image.isDark()
|
|
|
|
}()
|
|
|
|
|
2020-03-13 11:43:17 +01:00
|
|
|
lazy var isBright: Bool = {
|
|
|
|
return image.isBright()
|
|
|
|
}()
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
let image: RSImage
|
2020-08-10 20:53:21 +02:00
|
|
|
let isSymbol: Bool
|
2020-12-06 21:25:22 +01:00
|
|
|
let isBackgroundSupressed: Bool
|
2020-08-10 20:53:21 +02:00
|
|
|
let preferredColor: CGColor?
|
2020-07-21 04:01:25 +02:00
|
|
|
|
2020-12-06 21:25:22 +01:00
|
|
|
init(_ image: RSImage, isSymbol: Bool = false, isBackgroundSupressed: Bool = false, preferredColor: CGColor? = nil) {
|
2019-11-06 01:05:57 +01:00
|
|
|
self.image = image
|
2020-07-21 04:01:25 +02:00
|
|
|
self.isSymbol = isSymbol
|
2020-08-10 20:53:21 +02:00
|
|
|
self.preferredColor = preferredColor
|
2020-12-06 21:25:22 +01:00
|
|
|
self.isBackgroundSupressed = isBackgroundSupressed
|
2019-11-06 01:05:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
extension NSImage {
|
|
|
|
func isDark() -> Bool {
|
|
|
|
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)?.isDark() ?? false
|
|
|
|
}
|
2020-03-13 11:43:17 +01:00
|
|
|
|
|
|
|
func isBright() -> Bool {
|
2020-03-13 16:51:49 +01:00
|
|
|
return self.cgImage(forProposedRect: nil, context: nil, hints: nil)?.isBright() ?? false
|
2020-03-13 11:43:17 +01:00
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
extension UIImage {
|
|
|
|
func isDark() -> Bool {
|
|
|
|
return self.cgImage?.isDark() ?? false
|
|
|
|
}
|
2020-03-13 11:43:17 +01:00
|
|
|
|
|
|
|
func isBright() -> Bool {
|
|
|
|
return self.cgImage?.isBright() ?? false
|
|
|
|
}
|
2019-11-06 01:05:57 +01:00
|
|
|
}
|
|
|
|
#endif
|
2019-09-30 02:40:12 +02:00
|
|
|
|
2020-03-13 11:43:17 +01:00
|
|
|
fileprivate enum ImageLuminanceType {
|
|
|
|
case regular, bright, dark
|
|
|
|
}
|
2021-04-03 17:40:46 +02:00
|
|
|
|
2019-09-30 02:40:12 +02:00
|
|
|
extension CGImage {
|
|
|
|
|
2020-03-13 11:43:17 +01:00
|
|
|
func isBright() -> Bool {
|
2021-04-03 17:40:46 +02:00
|
|
|
guard let luminanceType = getLuminanceType() else {
|
2020-03-13 11:43:17 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return luminanceType == .bright
|
|
|
|
}
|
|
|
|
|
2019-09-30 02:40:12 +02:00
|
|
|
func isDark() -> Bool {
|
2021-04-03 17:40:46 +02:00
|
|
|
guard let luminanceType = getLuminanceType() else {
|
2020-03-13 11:43:17 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return luminanceType == .dark
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:40:46 +02:00
|
|
|
fileprivate func getLuminanceType() -> ImageLuminanceType? {
|
|
|
|
|
|
|
|
// This has been rewritten with information from https://christianselig.com/2021/04/efficient-average-color/
|
|
|
|
|
|
|
|
// First, resize the image. We do this for two reasons, 1) less pixels to deal with means faster
|
|
|
|
// calculation and a resized image still has the "gist" of the colors, and 2) the image we're dealing
|
|
|
|
// with may come in any of a variety of color formats (CMYK, ARGB, RGBA, etc.) which complicates things,
|
|
|
|
// and redrawing it normalizes that into a base color format we can deal with.
|
|
|
|
// 40x40 is a good size to resize to still preserve quite a bit of detail but not have too many pixels
|
|
|
|
// to deal with. Aspect ratio is irrelevant for just finding average color.
|
|
|
|
let size = CGSize(width: 40, height: 40)
|
|
|
|
|
|
|
|
let width = Int(size.width)
|
|
|
|
let height = Int(size.height)
|
|
|
|
let totalPixels = width * height
|
|
|
|
|
|
|
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
|
|
|
|
|
|
|
// ARGB format
|
|
|
|
let bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue
|
|
|
|
|
|
|
|
// 8 bits for each color channel, we're doing ARGB so 32 bits (4 bytes) total, and thus if the image is n pixels wide,
|
|
|
|
// and has 4 bytes per pixel, the total bytes per row is 4n. That gives us 2^8 = 256 color variations for each RGB channel
|
|
|
|
// or 256 * 256 * 256 = ~16.7M color options in total. That seems like a lot, but lots of HDR movies are in 10 bit, which
|
|
|
|
// is (2^10)^3 = 1 billion color options!
|
|
|
|
guard let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
|
|
|
|
|
|
|
|
// Draw our resized image
|
|
|
|
context.draw(self, in: CGRect(origin: .zero, size: size))
|
|
|
|
|
|
|
|
guard let pixelBuffer = context.data else { return nil }
|
|
|
|
|
|
|
|
// Bind the pixel buffer's memory location to a pointer we can use/access
|
|
|
|
let pointer = pixelBuffer.bindMemory(to: UInt32.self, capacity: width * height)
|
|
|
|
|
2019-10-30 02:48:07 +01:00
|
|
|
var totalLuminance = 0.0
|
2019-09-30 02:40:12 +02:00
|
|
|
|
2021-04-03 17:40:46 +02:00
|
|
|
// Column of pixels in image
|
|
|
|
for x in 0 ..< width {
|
|
|
|
// Row of pixels in image
|
|
|
|
for y in 0 ..< height {
|
|
|
|
// To get the pixel location just think of the image as a grid of pixels, but stored as one long row
|
|
|
|
// rather than columns and rows, so for instance to map the pixel from the grid in the 15th row and 3
|
|
|
|
// columns in to our "long row", we'd offset ourselves 15 times the width in pixels of the image, and
|
|
|
|
// then offset by the amount of columns
|
|
|
|
let pixel = pointer[(y * width) + x]
|
|
|
|
|
|
|
|
let r = red(for: pixel)
|
|
|
|
let g = green(for: pixel)
|
|
|
|
let b = blue(for: pixel)
|
|
|
|
|
|
|
|
let luminance = (0.299 * Double(r) + 0.587 * Double(g) + 0.114 * Double(b))
|
|
|
|
|
2020-01-11 20:25:31 +01:00
|
|
|
totalLuminance += luminance
|
|
|
|
}
|
2019-09-30 02:40:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 17:40:46 +02:00
|
|
|
let avgLuminance = totalLuminance / Double(totalPixels)
|
2020-03-13 11:43:17 +01:00
|
|
|
if totalLuminance == 0 || avgLuminance < 40 {
|
|
|
|
return .dark
|
2020-03-13 16:45:02 +01:00
|
|
|
} else if avgLuminance > 180 {
|
2020-03-13 11:43:17 +01:00
|
|
|
return .bright
|
2020-01-11 20:25:31 +01:00
|
|
|
} else {
|
2020-03-13 11:43:17 +01:00
|
|
|
return .regular
|
2020-01-11 20:25:31 +01:00
|
|
|
}
|
2019-09-30 02:40:12 +02:00
|
|
|
}
|
|
|
|
|
2021-04-03 17:40:46 +02:00
|
|
|
private func red(for pixelData: UInt32) -> UInt8 {
|
|
|
|
return UInt8((pixelData >> 16) & 255)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func green(for pixelData: UInt32) -> UInt8 {
|
|
|
|
return UInt8((pixelData >> 8) & 255)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func blue(for pixelData: UInt32) -> UInt8 {
|
|
|
|
return UInt8((pixelData >> 0) & 255)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|