import AppKit @main struct PanelAssetChecks { static func luminance(_ color: NSColor) -> Double { func linear(_ x: CGFloat) -> Double { let value = Double(x) return value <= 0.04045 ? value / 12.92 : pow((value + 0.055) / 1.055, 2.4) } return 0.2126 * linear(color.redComponent) + 0.7152 * linear(color.greenComponent) + 0.0722 * linear(color.blueComponent) } static func main() { let bundle = Bundle(path: CommandLine.arguments[1])! for appearance in [NSAppearance.Name.aqua, .darkAqua] { NSAppearance(named: appearance)!.performAsCurrentDrawingAppearance { for name in ["PanelBackground", "PanelCard", "PanelTooltip", "MetricCPU", "MetricGPU", "MetricMemory", "MetricNetwork", "MetricDisk", "MetricBattery", "SeriesUser", "SeriesSystem", "SeriesUpload"] { guard let color = NSColor(named: NSColor.Name(name), bundle: bundle)?.usingColorSpace(.sRGB) else { fatalError("Missing color: \(name)") } precondition(abs(color.alphaComponent - 1) < 0.005, "Unexpected alpha \(name)") if name.hasPrefix("Metric") || name.hasPrefix("Series") { let card = NSColor(named: "PanelCard", bundle: bundle)!.usingColorSpace(.sRGB)! let a = luminance(color), b = luminance(card) precondition((max(a, b) + 0.05) / (min(a, b) + 0.05) >= 3, "Insufficient graphical contrast: \(name) / \(appearance)") } } let names = ["MetricCPU", "SeriesUser", "SeriesSystem"] let colors = names.map { NSColor(named: NSColor.Name($0), bundle: bundle)!.usingColorSpace(.sRGB)! } for i in 0..= 0.1, "CPU series hues too close") } } } } print("PASS: compiled light/dark semantic colors exist, panel/card opaque, semantic colors opaque, CPU series distinct and graphical contrast >= 3:1") } }