Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Add Peel's Terminal themes and the profile Terminal reads

Țugui Dragoș7e192ac39387
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift
new file mode 100644
index 00000000..4d9d9219
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalProfile.swift
@@ -0,0 +1,66 @@
+import AppKit
+
+public enum TerminalProfile {
+ public static func settings(for theme: TerminalTheme) throws -> [String: Any] {
+ var settings: [String: Any] = [
+ "name": theme.profileName,
+ "type": "Window Settings",
+ "ProfileCurrentVersion": 2.09,
+ "BackgroundColor": try archive(theme.background, alpha: 0.95),
+ "BackgroundBlur": 0.5,
+ "BackgroundBlurInactive": 0.0,
+ "BackgroundSettingsForInactiveWindows": true,
+ "TextColor": try archive(theme.text),
+ "TextBoldColor": try archive(theme.text),
+ "CursorColor": try archive(theme.cursor),
+ "SelectionColor": try archive(theme.selection),
+ "Font": font(),
+ "FontAntialias": true,
+ "FontHeightSpacing": 1.0,
+ "FontWidthSpacing": 1,
+ "DynamicANSIForegroundColors": false,
+ "columnCount": 120,
+ "rowCount": 30,
+ ]
+ for (index, color) in theme.ansi.enumerated() {
+ settings["ANSI\(index < 8 ? "" : "Bright")\(ansiNames[index % 8])Color"] = try archive(color)
+ }
+ return settings
+ }
+
+ private static let ansiNames = ["Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"]
+
+ private static func archive(_ color: UInt32, alpha: CGFloat = 1) throws -> Data {
+ let sRGB = NSColor(
+ srgbRed: CGFloat(color >> 16 & 0xFF) / 255,
+ green: CGFloat(color >> 8 & 0xFF) / 255,
+ blue: CGFloat(color & 0xFF) / 255,
+ alpha: alpha
+ )
+ return try NSKeyedArchiver.archivedData(withRootObject: sRGB, requiringSecureCoding: true)
+ }
+
+ private static func font() -> Data {
+ let archiver = NSKeyedArchiver(requiringSecureCoding: true)
+ archiver.setClassName("NSFont", for: TerminalFont.self)
+ archiver.encode(TerminalFont(), forKey: NSKeyedArchiveRootObjectKey)
+ archiver.finishEncoding()
+ return archiver.encodedData
+ }
+}
+
+final class TerminalFont: NSObject, NSSecureCoding {
+ static let supportsSecureCoding = true
+
+ override init() {}
+
+ required init?(coder: NSCoder) {
+ nil
+ }
+
+ func encode(with coder: NSCoder) {
+ coder.encode("SFMonoTerminal-Regular", forKey: "NSName")
+ coder.encode(12.0, forKey: "NSSize")
+ coder.encode(Int32(16), forKey: "NSfFlags")
+ }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalTheme.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalTheme.swift
new file mode 100644
index 00000000..efe104ab
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalTheme.swift
@@ -0,0 +1,12 @@
+public struct TerminalTheme: Sendable, Hashable, Identifiable {
+ public let name: String
+ public let background: UInt32
+ public let text: UInt32
+ public let cursor: UInt32
+ public let selection: UInt32
+ public let ansi: [UInt32]
+
+ public var id: String { name }
+
+ public var profileName: String { "Peel \(name)" }
+}
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeCatalog.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeCatalog.swift
new file mode 100644
index 00000000..5de2bab0
--- /dev/null
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeCatalog.swift
@@ -0,0 +1,236 @@
+public enum TerminalThemeCatalog {
+ public static let all: [TerminalTheme] = [
+ TerminalTheme(
+ name: "Aerogel",
+ background: 0x151920, text: 0xCBDDE4, cursor: 0xF2A47A, selection: 0x4A3A36,
+ ansi: [
+ 0x282D33, 0xF9899E, 0x6AA96B, 0xF2A47A, 0x54B2F2, 0xA58BED, 0x0ED8D8, 0xC3D3DA,
+ 0x757E83, 0xFCA3B5, 0x88BB89, 0xF5B695, 0x79C4F8, 0xB8A3F1, 0x51ECE9, 0xD8EAF1,
+ ]
+ ),
+ TerminalTheme(
+ name: "Anodise",
+ background: 0x171718, text: 0xE2E2E4, cursor: 0xE8B54A, selection: 0x493D24,
+ ansi: [
+ 0x2E2E2F, 0xFF6A53, 0x39B871, 0xE8B54A, 0x6FA8DC, 0xCA80DB, 0x46C7C2, 0xD0D0D2,
+ 0x7C7C7D, 0xFF8976, 0x61C68D, 0xF0C771, 0x8CB9E3, 0xD59AE3, 0x71D8D4, 0xE6E6E8,
+ ]
+ ),
+ TerminalTheme(
+ name: "Borrow",
+ background: 0x1B1F26, text: 0xD6DBE4, cursor: 0xF09A18, selection: 0x4E3C23,
+ ansi: [
+ 0x2F3238, 0xFF6A6A, 0x65C28E, 0xF09A18, 0x5AA6F0, 0xAB8BF2, 0x4FC2CE, 0xCDD2DA,
+ 0x7B7F85, 0xFF8988, 0x85CFA5, 0xF3AE46, 0x7BB8F3, 0xBCA3F5, 0x78D4DE, 0xE3E8F1,
+ ]
+ ),
+ TerminalTheme(
+ name: "Cavitation",
+ background: 0x0F131A, text: 0xCADCE3, cursor: 0x40D0ED, selection: 0x1B404C,
+ ansi: [
+ 0x23282C, 0xFF52BE, 0x5BB7A4, 0xCE8C54, 0x6E9FCF, 0xD0A4DB, 0x00A7BB, 0xC2D1D7,
+ 0x747C81, 0xFE77CD, 0x7CC5B6, 0xD8A376, 0x8CB3D9, 0xDBB6E1, 0x4BB9CB, 0xD5E7EE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Cherenkov",
+ background: 0x0E2138, text: 0xCADEE6, cursor: 0xEE7DFA, selection: 0x443766,
+ ansi: [
+ 0x253546, 0xEE97A2, 0x4FBE6F, 0xCAA20A, 0x1EA5EA, 0xE572F2, 0x0DE6D6, 0xC1D5DF,
+ 0x72818E, 0xF3AEB6, 0x72CB8C, 0xD5B53B, 0x49B8EF, 0xEA8EF5, 0x5FF2EB, 0xD7EBF3,
+ ]
+ ),
+ TerminalTheme(
+ name: "Cinnabar",
+ background: 0x1B1512, text: 0xE2D6D1, cursor: 0xF28F83, selection: 0x4E322D,
+ ansi: [
+ 0x312C28, 0xF28F83, 0x0CCEA0, 0xF2BA24, 0x27A5F2, 0xFD5AB0, 0x11B4B3, 0xD8CDC9,
+ 0x817A75, 0xF5A59C, 0x58D9B7, 0xF1C950, 0x52B7F5, 0xFE7CC0, 0x45C6C5, 0xEFE3DE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Cochineal",
+ background: 0x2A1219, text: 0xEDD6DA, cursor: 0xFF647F, selection: 0x5D2430,
+ ansi: [
+ 0x3D2B2F, 0xFF647B, 0x8FC9A4, 0xE8B54A, 0x96A8E8, 0xC58FE0, 0x49BFBD, 0xE1CBCF,
+ 0x8A787C, 0xFF8396, 0xABD6B6, 0xF0C771, 0xABB9ED, 0xD1A5E6, 0x86D1C9, 0xF8E1E5,
+ ]
+ ),
+ TerminalTheme(
+ name: "Coherence",
+ background: 0x0D0E11, text: 0xD4D9E1, cursor: 0x16DFF4, selection: 0x0F4047,
+ ansi: [
+ 0x242427, 0xF76E59, 0x0EF477, 0xF4D00C, 0x529AF7, 0xBB7CF7, 0x16DFF4, 0xC9CED4,
+ 0x77797C, 0xF98C7B, 0x5EF79C, 0xFFE252, 0x74AFF9, 0xC997F9, 0x57F1FD, 0xDEE3EC,
+ ]
+ ),
+ TerminalTheme(
+ name: "Coherence High Contrast",
+ background: 0x000000, text: 0xFFFFFF, cursor: 0x12EBFF, selection: 0x04383D,
+ ansi: [
+ 0x292929, 0xFF8A8A, 0x10FF7D, 0xFFE20E, 0xBAD2FF, 0xE9C3FF, 0x12EBFF, 0xCCCCCC,
+ 0x818181, 0xFFA1A1, 0x51FFA2, 0xFFEE58, 0xD1E0FF, 0xEED3FE, 0x6BF8FF, 0xFFFFFF,
+ ]
+ ),
+ TerminalTheme(
+ name: "Dichroic",
+ background: 0x0F1C19, text: 0xD3E1DD, cursor: 0x3FE8BE, selection: 0x1A4D40,
+ ansi: [
+ 0x25302E, 0xFF7A7A, 0x3CFFCE, 0xD8C07A, 0x7FA8D8, 0xF268C0, 0x14BFA0, 0xC5D3CF,
+ 0x727F7B, 0xFF9595, 0x79FADA, 0xE1D098, 0x99B9E0, 0xF586CD, 0x47CFB6, 0xDBE9E5,
+ ]
+ ),
+ TerminalTheme(
+ name: "Effect",
+ background: 0x101A16, text: 0xCCD8D2, cursor: 0xFF6757, selection: 0x492A23,
+ ansi: [
+ 0x262F2C, 0xFF6655, 0x5FD8C0, 0xFFA03C, 0x8E9CBE, 0xC08FD0, 0x4CC5D6, 0xC7D2CD,
+ 0x747E7A, 0xFF8578, 0x8CE1D0, 0xFFB363, 0xA5B0CB, 0xCDA5D9, 0x83D3D2, 0xDBE8E1,
+ ]
+ ),
+ TerminalTheme(
+ name: "Epitaxy",
+ background: 0x1F1F28, text: 0xD7DCE5, cursor: 0x77ABF9, selection: 0x34405A,
+ ansi: [
+ 0x34353C, 0xF57570, 0x60D53D, 0xF2C17A, 0x70A4F2, 0xCC77FD, 0x13C5B5, 0xCED2DB,
+ 0x7C7F85, 0xF8918D, 0x7DE16A, 0xF2CE9B, 0x8DB6F5, 0xD793FE, 0x49D7CA, 0xE4E9F2,
+ ]
+ ),
+ TerminalTheme(
+ name: "Fraunhofer",
+ background: 0x12151D, text: 0xDDE1E8, cursor: 0xAF9CF9, selection: 0x383552,
+ ansi: [
+ 0x2A2C33, 0xFF6849, 0x7CE85C, 0xFFC93C, 0x4FC3F7, 0xC07BFF, 0x00E5C0, 0xCBCFD6,
+ 0x787B81, 0xFF876F, 0x96F486, 0xFFD769, 0x77D4FF, 0xCD95FF, 0x4EEBD6, 0xE1E5EC,
+ ]
+ ),
+ TerminalTheme(
+ name: "Glacier",
+ background: 0x243343, text: 0xDCEAF2, cursor: 0x0ECCCB, selection: 0x1F5864,
+ ansi: [
+ 0x394652, 0xF9828A, 0x6BECBE, 0xF6B464, 0x9BD0F6, 0xAEA0F0, 0x0ECCCB, 0xCDDCE5,
+ 0x7E8A93, 0xFB9CA2, 0x96F1D0, 0xFBC686, 0xB9DEFA, 0xBEB3F3, 0x4CDFDB, 0xE5F3FB,
+ ]
+ ),
+ TerminalTheme(
+ name: "Hadal",
+ background: 0x0C1F2B, text: 0xCCE2E7, cursor: 0x97CCF2, selection: 0x2D485B,
+ ansi: [
+ 0x23333C, 0xCA8A98, 0x0CC681, 0xB69F4B, 0x97CCF2, 0xF273EF, 0x0FCDD2, 0xBFD5DB,
+ 0x708087, 0xD5A2AD, 0x41D49D, 0xC5B26F, 0xB5DBF7, 0xF58FF2, 0x4DE0E1, 0xD6ECF1,
+ ]
+ ),
+ TerminalTheme(
+ name: "Incandescence",
+ background: 0x171110, text: 0xE6D8D6, cursor: 0xE8CC6A, selection: 0x493E26,
+ ansi: [
+ 0x2D2826, 0xFC656D, 0x17B000, 0xE8CC6A, 0x12A3E0, 0xFF58A2, 0x14B2B1, 0xD8CCC9,
+ 0x817977, 0xFD848B, 0x3BBE46, 0xEBD690, 0x41B5E6, 0xFF7BB5, 0x47C4C4, 0xEFE1DF,
+ ]
+ ),
+ TerminalTheme(
+ name: "Noctiluca",
+ background: 0x0D1418, text: 0xCBDDDC, cursor: 0x11D9A9, selection: 0x0E433B,
+ ansi: [
+ 0x232A2C, 0xFD6655, 0x11D9A9, 0xAEBD0F, 0x3AB2F2, 0x9291F2, 0x23A8B3, 0xC3D2D2,
+ 0x737C7D, 0xFE8577, 0x58E2C1, 0xBDCE44, 0x64C4F8, 0xA8A7F5, 0x4EBAC3, 0xD6E8E7,
+ ]
+ ),
+ TerminalTheme(
+ name: "Palimpsest",
+ background: 0x242019, text: 0xE8E1D2, cursor: 0xD9A94A, selection: 0x4F4125,
+ ansi: [
+ 0x3A3631, 0xDB8676, 0xB3C6A4, 0xD98C4A, 0x8FA0B8, 0xB695AE, 0x7EA6A0, 0xD9D1C5,
+ 0x837F78, 0xE39F92, 0xC8D7BC, 0xE1A36E, 0xA5B3C6, 0xC6ABBF, 0x98B8B3, 0xEDE6D6,
+ ]
+ ),
+ TerminalTheme(
+ name: "Passepartout",
+ background: 0x33333A, text: 0xE6E4DE, cursor: 0xD8A83C, selection: 0x5A4F3A,
+ ansi: [
+ 0x2A2A2B, 0xFFAFAD, 0x8FB080, 0xD8A83C, 0x92A9C3, 0xC0A0B8, 0x81B1AC, 0xDBDAD4,
+ 0x898988, 0xFFC6C5, 0xA5C099, 0xE0B963, 0xA8BBD0, 0xCDB3C6, 0x9BC1BD, 0xF3F1EA,
+ ]
+ ),
+ TerminalTheme(
+ name: "Persistence",
+ background: 0x141613, text: 0xDCE0D8, cursor: 0x7FE33F, selection: 0x2E471E,
+ ansi: [
+ 0x2C2D2A, 0xFF6B4A, 0x82E642, 0xFFB742, 0x8FB8FF, 0xE88BD8, 0x4ECFC0, 0xCDD0CA,
+ 0x797C77, 0xFF896E, 0xA1E772, 0xFFC76C, 0xA9CAFF, 0xEDA2E0, 0x77DFD3, 0xE2E6DE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Provenance",
+ background: 0x22252B, text: 0xDFE1E5, cursor: 0xF0B75A, selection: 0x534836,
+ ansi: [
+ 0x35373C, 0xF17D72, 0xA1B398, 0xF0B75A, 0x6FC7E0, 0xB9A6E8, 0x5FC4B0, 0xD1D4D9,
+ 0x808185, 0xF5988F, 0xB6C4AF, 0xF6C87E, 0x96D4E6, 0xC7B8ED, 0x82D3C3, 0xE8EAEE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Quantum",
+ background: 0x141827, text: 0xCDD8FA, cursor: 0xCD8FF9, selection: 0x403459,
+ ansi: [
+ 0x292E3D, 0xFF6E7F, 0x71EA9C, 0xFFC24A, 0x8AB4FF, 0xC77DFF, 0x4DE0D0, 0xC6D0EE,
+ 0x767C8F, 0xFF8B99, 0x8EE8B2, 0xFFD074, 0xA4C6FF, 0xD297FF, 0x83EAE0, 0xDBE6FF,
+ ]
+ ),
+ TerminalTheme(
+ name: "Riso",
+ background: 0x211D16, text: 0xE8E2D4, cursor: 0xFF63B4, selection: 0x562E3C,
+ ansi: [
+ 0x38342E, 0xFF6A5C, 0x4CC886, 0xFFEE3C, 0x8399EF, 0xFF63B4, 0x5FBCC4, 0xD6D1C3,
+ 0x827E75, 0xFF887D, 0x6CDA9D, 0xFFF275, 0x9DAEF3, 0xFF82C3, 0x82CCD3, 0xEDE7D8,
+ ]
+ ),
+ TerminalTheme(
+ name: "Safelight",
+ background: 0x352316, text: 0xFAE6C8, cursor: 0xFFF6E4, selection: 0x655547,
+ ansi: [
+ 0x4A3929, 0xFF7643, 0xC6C080, 0xFFC24A, 0x9DAFC0, 0xD69A8C, 0x8FBAB0, 0xE5D2B7,
+ 0x8B8170, 0xFF916B, 0xD4D09D, 0xFFD074, 0xB3BFCC, 0xDEAEA3, 0xA8CBC3, 0xFDE9CB,
+ ]
+ ),
+ TerminalTheme(
+ name: "Selenium",
+ background: 0x1E1D26, text: 0xD6DEEC, cursor: 0xAE9FCD, selection: 0x403C4E,
+ ansi: [
+ 0x33333C, 0xDB87A4, 0x80BA8F, 0xDDAC7B, 0x60B4E4, 0xAF95CB, 0x5DC0C3, 0xCCD2DF,
+ 0x7B7E87, 0xE2A1B7, 0x99C8A5, 0xE4BD95, 0x82C3EA, 0xBFAAD5, 0x80D0D2, 0xE0E8F7,
+ ]
+ ),
+ TerminalTheme(
+ name: "Silverpoint",
+ background: 0x242427, text: 0xDDDDE1, cursor: 0x93A6BC, selection: 0x3F434B,
+ ansi: [
+ 0x37373A, 0xE4C0C7, 0x9AB09A, 0xC8BA96, 0x9AA8C0, 0xB29AC0, 0x9DCAD2, 0xD3D3D7,
+ 0x818184, 0xF2D5DB, 0xAEC0AE, 0xCDC6AE, 0xAEB9CD, 0xC1AECD, 0xADC9C8, 0xEAEAEE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Stratum",
+ background: 0x1F1B16, text: 0xE4DACA, cursor: 0xC39B54, selection: 0x463A25,
+ ansi: [
+ 0x35312B, 0xDD8758, 0xA9C699, 0xBDA153, 0x999CAE, 0xBA8CB9, 0x7CA8A1, 0xD8D0C1,
+ 0x827C73, 0xE59F7B, 0xC1D9B4, 0xCAB476, 0xAEB1C0, 0xC9A3C7, 0x97B9B4, 0xF0E6D5,
+ ]
+ ),
+ TerminalTheme(
+ name: "Valence",
+ background: 0x15121B, text: 0xD8D5E0, cursor: 0xF976E7, selection: 0x4C2A4C,
+ ansi: [
+ 0x2B292F, 0xFA6770, 0x20D12E, 0xF2B486, 0x12B6E8, 0xF263DF, 0x0CB2B2, 0xD0CDD7,
+ 0x7C7A81, 0xFC868D, 0x45DE5F, 0xF2C4A3, 0x3EC5EB, 0xF582E5, 0x41C4C4, 0xE6E3EE,
+ ]
+ ),
+ TerminalTheme(
+ name: "Verdigris",
+ background: 0x171410, text: 0xE0D6D2, cursor: 0x0FCBB7, selection: 0x154038,
+ ansi: [
+ 0x2E2B28, 0xE67597, 0x41BF76, 0xE4BF39, 0x2FA5CB, 0xA38EF2, 0x00CCB8, 0xD6CDC9,
+ 0x7F7A75, 0xE990AC, 0x67CC91, 0xECCF64, 0x58B8D6, 0xB5A5F5, 0x5BDACB, 0xEDE3DF,
+ ]
+ ),
+ ]
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeAudit.swift b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeAudit.swift
new file mode 100644
index 00000000..1f4760e3
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeAudit.swift
@@ -0,0 +1,165 @@
+import Foundation
+@testable import PeelCore
+
+enum TerminalThemeAudit {
+ static let chromatic = [1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13, 14]
+ static let huePairs = [(1, 5), (1, 3), (1, 2), (2, 3), (2, 6), (4, 6), (4, 5)]
+
+ static func issues(of theme: TerminalTheme) -> [String] {
+ let ansi = theme.ansi
+ let worst = worstBackground(theme.background)
+ func lc(_ color: UInt32) -> Double {
+ min(apcaLc(color, on: theme.background), apcaLc(color, on: worst))
+ }
+ var issues: [String] = []
+ func expect(_ passes: Bool, _ issue: @autoclosure () -> String) {
+ if !passes { issues.append(issue()) }
+ }
+
+ expect(lc(theme.text) >= 75, "text Lc \(lc(theme.text))")
+ for slot in chromatic {
+ expect(lc(ansi[slot]) >= 45, "ANSI \(slot) Lc \(lc(ansi[slot]))")
+ }
+ expect(lc(ansi[7]) >= 75, "white Lc \(lc(ansi[7]))")
+ expect(lc(ansi[15]) >= lc(ansi[7]), "bright white is dimmer than white")
+ expect((30...60).contains(lc(ansi[8])), "bright black Lc \(lc(ansi[8]))")
+ expect(deltaE(ansi[0], theme.background) >= 3, "black is lost on the background")
+ for slot in 1...7 {
+ expect(lc(ansi[slot + 8]) >= lc(ansi[slot]) - 0.5, "ANSI \(slot + 8) is dimmer than ANSI \(slot)")
+ expect(deltaE(ansi[slot], ansi[slot + 8]) >= 5, "ANSI \(slot) and \(slot + 8) look alike")
+ }
+ for (first, second) in huePairs {
+ expect(deltaE(ansi[first], ansi[second]) >= 15, "ANSI \(first) and \(second) look alike")
+ }
+ expect(colorBlindDifference(ansi[1], ansi[2]) >= 10, "red and green look alike to someone color blind")
+ expect(apcaLc(theme.text, on: theme.selection) >= 75, "text on the selection Lc \(apcaLc(theme.text, on: theme.selection))")
+ expect(deltaE(theme.selection, theme.background) >= 6, "the selection is lost on the background")
+ expect(lc(theme.cursor) >= 45, "cursor Lc \(lc(theme.cursor))")
+ expect(apcaLc(theme.background, on: theme.cursor) >= 45, "the letter under the cursor is hard to read")
+ for slot in 1...6 {
+ let best = max(apcaLc(theme.text, on: ansi[slot]), apcaLc(ansi[0], on: ansi[slot]), apcaLc(theme.background, on: ansi[slot]))
+ expect(best >= 45, "no text reads on ANSI \(slot)")
+ }
+ return issues
+ }
+
+ static func meanDifference(_ first: TerminalTheme, _ second: TerminalTheme) -> Double {
+ let firstColors = [first.background] + first.ansi
+ let secondColors = [second.background] + second.ansi
+ return zip(firstColors, secondColors).map { deltaE($0, $1) }.reduce(0, +) / Double(firstColors.count)
+ }
+
+ static func channels(_ color: UInt32) -> [Double] {
+ [Double(color >> 16 & 0xFF), Double(color >> 8 & 0xFF), Double(color & 0xFF)]
+ }
+
+ static func packed(_ channels: [Double]) -> UInt32 {
+ channels.map { UInt32($0.rounded()) }.reduce(0) { $0 << 8 | $1 }
+ }
+
+ static func worstBackground(_ background: UInt32) -> UInt32 {
+ packed(channels(background).map { $0 + (255 - $0) * 0.05 })
+ }
+
+ static func apcaLuminance(_ color: UInt32) -> Double {
+ let weights = [0.2126729, 0.7151522, 0.0721750]
+ let luminance = zip(channels(color), weights).map { pow($0 / 255, 2.4) * $1 }.reduce(0, +)
+ return luminance < 0.022 ? luminance + pow(0.022 - luminance, 1.414) : luminance
+ }
+
+ static func apcaLc(_ text: UInt32, on background: UInt32) -> Double {
+ let textLuminance = apcaLuminance(text)
+ let backgroundLuminance = apcaLuminance(background)
+ guard abs(backgroundLuminance - textLuminance) >= 0.0005 else { return 0 }
+ if backgroundLuminance > textLuminance {
+ let contrast = (pow(backgroundLuminance, 0.56) - pow(textLuminance, 0.57)) * 1.14
+ return contrast < 0.1 ? 0 : (contrast - 0.027) * 100
+ }
+ let contrast = (pow(backgroundLuminance, 0.65) - pow(textLuminance, 0.62)) * 1.14
+ return contrast > -0.1 ? 0 : -(contrast + 0.027) * 100
+ }
+
+ static func linear(_ value: Double) -> Double {
+ value <= 0.03928 ? value / 12.92 : pow((value + 0.055) / 1.055, 2.4)
+ }
+
+ static func lab(_ color: UInt32) -> (lightness: Double, a: Double, b: Double) {
+ let rgb = channels(color).map { linear($0 / 255) }
+ let x = (rgb[0] * 0.4124 + rgb[1] * 0.3576 + rgb[2] * 0.1805) / 0.95047
+ let y = rgb[0] * 0.2126 + rgb[1] * 0.7152 + rgb[2] * 0.0722
+ let z = (rgb[0] * 0.0193 + rgb[1] * 0.1192 + rgb[2] * 0.9505) / 1.08883
+ func f(_ t: Double) -> Double { t > 0.008856 ? cbrt(t) : 7.787 * t + 16 / 116 }
+ return (116 * f(y) - 16, 500 * (f(x) - f(y)), 200 * (f(y) - f(z)))
+ }
+
+ static func deltaE(_ first: UInt32, _ second: UInt32) -> Double {
+ let (l1, a1, b1) = lab(first)
+ let (l2, a2, b2) = lab(second)
+ let radians = Double.pi / 180
+ let chromaMean = (hypot(a1, b1) + hypot(a2, b2)) / 2
+ let g = 0.5 * (1 - (pow(chromaMean, 7) / (pow(chromaMean, 7) + pow(25, 7))).squareRoot())
+ let a1Prime = (1 + g) * a1
+ let a2Prime = (1 + g) * a2
+ let c1 = hypot(a1Prime, b1)
+ let c2 = hypot(a2Prime, b2)
+ func hue(_ b: Double, _ a: Double) -> Double {
+ guard b != 0 || a != 0 else { return 0 }
+ let degrees = atan2(b, a) / radians
+ return degrees >= 0 ? degrees : degrees + 360
+ }
+ let h1 = hue(b1, a1Prime)
+ let h2 = hue(b2, a2Prime)
+ var hueDifference = 0.0
+ if c1 * c2 != 0 {
+ hueDifference = h2 - h1
+ if hueDifference > 180 { hueDifference -= 360 } else if hueDifference < -180 { hueDifference += 360 }
+ }
+ let deltaL = l2 - l1
+ let deltaC = c2 - c1
+ let deltaH = 2 * (c1 * c2).squareRoot() * sin(hueDifference * radians / 2)
+ let lightnessMean = (l1 + l2) / 2
+ let chromaPrimeMean = (c1 + c2) / 2
+ var hueMean = h1 + h2
+ if c1 * c2 != 0 {
+ if abs(h1 - h2) <= 180 {
+ hueMean /= 2
+ } else {
+ hueMean = hueMean < 360 ? (hueMean + 360) / 2 : (hueMean - 360) / 2
+ }
+ }
+ let t = 1 - 0.17 * cos((hueMean - 30) * radians) + 0.24 * cos(2 * hueMean * radians)
+ + 0.32 * cos((3 * hueMean + 6) * radians) - 0.2 * cos((4 * hueMean - 63) * radians)
+ let sl = 1 + 0.015 * pow(lightnessMean - 50, 2) / (20 + pow(lightnessMean - 50, 2)).squareRoot()
+ let sc = 1 + 0.045 * chromaPrimeMean
+ let sh = 1 + 0.015 * chromaPrimeMean * t
+ let rt = -2 * (pow(chromaPrimeMean, 7) / (pow(chromaPrimeMean, 7) + pow(25, 7))).squareRoot()
+ * sin(60 * exp(-pow((hueMean - 275) / 25, 2)) * radians)
+ return (pow(deltaL / sl, 2) + pow(deltaC / sc, 2) + pow(deltaH / sh, 2) + rt * (deltaC / sc) * (deltaH / sh)).squareRoot()
+ }
+
+ static func colorBlind(_ color: UInt32, protanopia: Bool) -> UInt32 {
+ let rgb = channels(color).map { linear($0 / 255) }
+ let long = 17.8824 * rgb[0] + 43.5161 * rgb[1] + 4.11935 * rgb[2]
+ let medium = 3.45565 * rgb[0] + 27.1554 * rgb[1] + 3.86714 * rgb[2]
+ let short = 0.0299566 * rgb[0] + 0.184309 * rgb[1] + 1.46709 * rgb[2]
+ let seenLong = protanopia ? 2.02344 * medium - 2.52581 * short : long
+ let seenMedium = protanopia ? medium : 0.494207 * long + 1.24827 * short
+ let seen = [
+ 0.080944 * seenLong - 0.130504 * seenMedium + 0.116721 * short,
+ -0.0102485 * seenLong + 0.0540194 * seenMedium - 0.113615 * short,
+ -0.000365294 * seenLong - 0.00412163 * seenMedium + 0.693513 * short,
+ ]
+ return packed(seen.map { value in
+ let clamped = min(1, max(0, value))
+ let encoded = clamped <= 0.0031308 ? 12.92 * clamped : 1.055 * pow(clamped, 1 / 2.4) - 0.055
+ return min(1, max(0, encoded)) * 255
+ })
+ }
+
+ static func colorBlindDifference(_ first: UInt32, _ second: UInt32) -> Double {
+ min(
+ deltaE(colorBlind(first, protanopia: false), colorBlind(second, protanopia: false)),
+ deltaE(colorBlind(first, protanopia: true), colorBlind(second, protanopia: true))
+ )
+ }
+}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeTests.swift
new file mode 100644
index 00000000..4b8e012f
--- /dev/null
+++ b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeTests.swift
@@ -0,0 +1,64 @@
+import AppKit
+@testable import PeelCore
+import Testing
+
+struct TerminalThemeTests {
+ static let clearDark = URL(filePath: "/System/Applications/Utilities/Terminal.app/Contents/Resources/Initial Settings/Clear Dark.terminal")
+ static let ansiKeys = ["", "Bright"].flatMap { brightness in
+ ["Black", "Red", "Green", "Yellow", "Blue", "Magenta", "Cyan", "White"].map { "ANSI\(brightness)\($0)Color" }
+ }
+
+ @Test func everyThemeIsTerminalsClearDarkWithItsOwnColors() throws {
+ let apple = try #require(PropertyListSerialization.propertyList(from: Data(contentsOf: Self.clearDark), format: nil) as? [String: Any])
+ for theme in TerminalThemeCatalog.all {
+ let profile = try TerminalProfile.settings(for: theme)
+ #expect(Set(profile.keys) == Set(apple.keys).union(["CursorColor"]), "\(theme.name)")
+ for (key, value) in apple where !key.hasSuffix("Color") && key != "name" {
+ #expect((profile[key] as? NSObject)?.isEqual(value) == true, "\(theme.name): \(key)")
+ }
+ #expect(profile["name"] as? String == theme.profileName)
+ }
+ }
+
+ @Test func everyColorShowsInTerminalAsTheThemeDefinesIt() throws {
+ for theme in TerminalThemeCatalog.all {
+ let profile = try TerminalProfile.settings(for: theme)
+ let colors = [
+ ("BackgroundColor", theme.background), ("TextColor", theme.text), ("TextBoldColor", theme.text),
+ ("CursorColor", theme.cursor), ("SelectionColor", theme.selection),
+ ] + zip(Self.ansiKeys, theme.ansi)
+ for (key, expected) in colors {
+ let data = try #require(profile[key] as? Data, "\(theme.name): \(key)")
+ let color = try #require(try NSKeyedUnarchiver.unarchivedObject(ofClass: NSColor.self, from: data)?.usingColorSpace(.sRGB))
+ let channels = [color.redComponent, color.greenComponent, color.blueComponent].map { Int(($0 * 255).rounded()) }
+ #expect(channels == [Int(expected >> 16 & 0xFF), Int(expected >> 8 & 0xFF), Int(expected & 0xFF)], "\(theme.name): \(key)")
+ #expect(color.alphaComponent == (key == "BackgroundColor" ? 0.95 : 1), "\(theme.name): \(key)")
+ }
+ }
+ }
+
+ @Test func everyThemePassesTheContrastAudit() {
+ for theme in TerminalThemeCatalog.all {
+ let issues = TerminalThemeAudit.issues(of: theme)
+ #expect(issues.isEmpty, "\(theme.name): \(issues.joined(separator: "; "))")
+ }
+ }
+
+ @Test func everyThemeLooksUnlikeTheOthers() {
+ let themes = TerminalThemeCatalog.all
+ for (index, theme) in themes.enumerated() {
+ for other in themes[(index + 1)...] {
+ #expect(TerminalThemeAudit.meanDifference(theme, other) >= 5, "\(theme.name) and \(other.name)")
+ }
+ }
+ }
+
+ @Test func everyThemeHasItsOwnNameAndSixteenColors() {
+ let names = TerminalThemeCatalog.all.map(\.name)
+ #expect(Set(names).count == names.count)
+ for theme in TerminalThemeCatalog.all {
+ #expect(!theme.name.isEmpty)
+ #expect(theme.ansi.count == 16, "\(theme.name)")
+ }
+ }
+}