SPB Git

Repository Public

Theme.swift 9.1 KB 165 lines swift

1import Foundation23/// sRGB colour stored as 8-bit components; serialized as `#RRGGBB`.4public struct RGBColor: Hashable, Sendable, Codable, CustomStringConvertible {5    public var red: UInt86    public var green: UInt87    public var blue: UInt889    public init(_ red: UInt8, _ green: UInt8, _ blue: UInt8) {10        self.red = red; self.green = green; self.blue = blue11    }1213    /// Parses `#RGB`, `#RRGGBB` or the same without `#`.14    public init?(hex: String) {15        var text = hex.trimmingCharacters(in: .whitespaces)16        if text.hasPrefix("#") { text.removeFirst() }17        if text.count == 3 { text = text.map { "\($0)\($0)" }.joined() }18        guard text.count == 6, let value = UInt32(text, radix: 16) else { return nil }19        self.init(UInt8(value >> 16 & 0xff), UInt8(value >> 8 & 0xff), UInt8(value & 0xff))20    }2122    public var hex: String { String(format: "#%02X%02X%02X", red, green, blue) }23    public var description: String { hex }2425    public init(from decoder: Decoder) throws {26        let text = try decoder.singleValueContainer().decode(String.self)27        guard let color = RGBColor(hex: text) else {28            throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "invalid colour \(text)"))29        }30        self = color31    }32    public func encode(to encoder: Encoder) throws { var c = encoder.singleValueContainer(); try c.encode(hex) }3334    /// WCAG 2 relative luminance.35    public var relativeLuminance: Double {36        func channel(_ v: UInt8) -> Double {37            let s = Double(v) / 25538            return s <= 0.03928 ? s / 12.92 : pow((s + 0.055) / 1.055, 2.4)39        }40        return 0.2126 * channel(red) + 0.7152 * channel(green) + 0.0722 * channel(blue)41    }4243    /// WCAG 2 contrast ratio between two colours (1…21).44    public func contrastRatio(with other: RGBColor) -> Double {45        let a = relativeLuminance, b = other.relativeLuminance46        return (max(a, b) + 0.05) / (min(a, b) + 0.05)47    }4849    /// Linear blend toward `other` (`amount` 0…1).50    public func mixed(with other: RGBColor, amount: Double) -> RGBColor {51        let t = min(max(amount, 0), 1)52        func mix(_ a: UInt8, _ b: UInt8) -> UInt8 { UInt8((Double(a) + (Double(b) - Double(a)) * t).rounded()) }53        return RGBColor(mix(red, other.red), mix(green, other.green), mix(blue, other.blue))54    }55}5657/// A terminal colour profile plus the chrome colours derived from it.58public struct TerminalTheme: Codable, Sendable, Hashable, Identifiable {59    public var id: String60    public var name: String61    public var isDark: Bool62    public var isHighContrast: Bool63    public var background: RGBColor64    public var foreground: RGBColor65    public var cursor: RGBColor66    public var selection: RGBColor67    /// Accent used by the application chrome (focus ring, active tab); the user can override it.68    public var accent: RGBColor69    /// The 16 ANSI colours: 0–7 normal, 8–15 bright.70    public var ansi: [RGBColor]7172    public init(id: String, name: String, isDark: Bool, isHighContrast: Bool = false, background: RGBColor,73                foreground: RGBColor, cursor: RGBColor, selection: RGBColor, accent: RGBColor, ansi: [RGBColor]) {74        precondition(ansi.count == 16, "a theme needs exactly 16 ANSI colours")75        self.id = id; self.name = name; self.isDark = isDark; self.isHighContrast = isHighContrast76        self.background = background; self.foreground = foreground; self.cursor = cursor77        self.selection = selection; self.accent = accent; self.ansi = ansi78    }7980    /// Chrome surfaces derived from the terminal background so the window reads as one object.81    public var chromeBackground: RGBColor { background.mixed(with: isDark ? RGBColor(255, 255, 255) : RGBColor(0, 0, 0), amount: isDark ? 0.045 : 0.04) }82    public var separator: RGBColor { background.mixed(with: foreground, amount: isHighContrast ? 0.6 : 0.16) }83    public var secondaryText: RGBColor { foreground.mixed(with: background, amount: isHighContrast ? 0.12 : (isDark ? 0.38 : 0.28)) }84}8586private func c(_ hex: String) -> RGBColor { RGBColor(hex: hex)! }8788/// Built-in colour profiles. All palettes are original to SP Term.89public enum ThemeCatalog {90    public static let graphite = TerminalTheme(91        id: "sp-graphite", name: "SP Graphite", isDark: true,92        background: c("#14161B"), foreground: c("#D7DCE5"), cursor: c("#5CC8FF"), selection: c("#2B4466"), accent: c("#4C8DFF"),93        ansi: ["#1D2027", "#F0707A", "#7FD88F", "#E8C46A", "#6FA8FF", "#B48CFF", "#56D4DD", "#C5CBD6",94               "#5B6370", "#FF8F97", "#9CEBAA", "#F5D88A", "#93C0FF", "#CBAEFF", "#7FE6ED", "#F4F6FA"].map(c))9596    public static let midnight = TerminalTheme(97        id: "sp-midnight", name: "SP Midnight", isDark: true,98        background: c("#0B0D14"), foreground: c("#CDD3E4"), cursor: c("#9B7BFF"), selection: c("#2A2752"), accent: c("#8B6CFF"),99        ansi: ["#161927", "#F2687E", "#6FD3A0", "#E6B866", "#6C9BFF", "#A587FF", "#4FCBE0", "#B9C0D4",100               "#525A72", "#FF8A9C", "#8FE6BA", "#F3CF8C", "#94B6FF", "#C2ABFF", "#7CE0EF", "#EEF1F9"].map(c))101102    public static let daylight = TerminalTheme(103        id: "sp-daylight", name: "SP Daylight", isDark: false,104        background: c("#FBFBFD"), foreground: c("#20242C"), cursor: c("#1F6FEB"), selection: c("#CFE0FF"), accent: c("#1F6FEB"),105        ansi: ["#20242C", "#C2283A", "#17803D", "#946200", "#1B5FCC", "#7A3FD1", "#0B7A86", "#6E7683",106               "#4B525E", "#A61B2D", "#0F6A31", "#7A5000", "#144FB0", "#6630B8", "#086670", "#20242C"].map(c))107108    public static let paper = TerminalTheme(109        id: "sp-paper", name: "SP Paper", isDark: false,110        background: c("#F4EFE6"), foreground: c("#2B2722"), cursor: c("#B4541A"), selection: c("#E3D5BD"), accent: c("#B4541A"),111        ansi: ["#2B2722", "#B3261E", "#3C6E1F", "#8A5A00", "#2456A6", "#7B3C9A", "#1A6F73", "#6F675C",112               "#544D43", "#981B14", "#2F5A17", "#704800", "#1B468C", "#662F82", "#125B5F", "#2B2722"].map(c))113114    /// SP Term Next signature dark theme: deep blue-black, teal cursor.115    public static let aurora = TerminalTheme(116        id: "sp-aurora", name: "SP Aurora", isDark: true,117        background: c("#0E1117"), foreground: c("#DDE3EE"), cursor: c("#7CE0C3"), selection: c("#23405C"), accent: c("#4FD1C5"),118        ansi: ["#1A1E27", "#F2707C", "#7BDCA0", "#E9C46A", "#74A9FF", "#B892FF", "#5AD6E0", "#C8CEDA",119               "#5D6675", "#FF929B", "#9BEDB6", "#F6DA8E", "#98C2FF", "#CFB3FF", "#84E8EF", "#F5F7FB"].map(c))120121    /// SP Term Next signature light theme: cool white, deep teal accent.122    public static let porcelain = TerminalTheme(123        id: "sp-porcelain", name: "SP Porcelain", isDark: false,124        background: c("#F7F8FA"), foreground: c("#1C2129"), cursor: c("#0E7C86"), selection: c("#CDE7EA"), accent: c("#0E7C86"),125        ansi: ["#1C2129", "#BF2638", "#157A3A", "#8C5D00", "#1A5BC4", "#763CCB", "#0A7480", "#6A7280",126               "#48505C", "#A31A2C", "#0E652F", "#744C00", "#134CA9", "#622EB2", "#07616B", "#1C2129"].map(c))127128    public static let highContrastDark = TerminalTheme(129        id: "sp-contrast-dark", name: "High Contrast Dark", isDark: true, isHighContrast: true,130        background: c("#000000"), foreground: c("#FFFFFF"), cursor: c("#FFFF00"), selection: c("#0050C8"), accent: c("#66B2FF"),131        ansi: ["#000000", "#FF6B6B", "#5CFF7A", "#FFE95C", "#7AB8FF", "#E08CFF", "#5CF2FF", "#E6E6E6",132               "#9A9A9A", "#FF9C9C", "#9CFFB0", "#FFF39C", "#B0D6FF", "#EDBBFF", "#A6F8FF", "#FFFFFF"].map(c))133134    public static let highContrastLight = TerminalTheme(135        id: "sp-contrast-light", name: "High Contrast Light", isDark: false, isHighContrast: true,136        background: c("#FFFFFF"), foreground: c("#000000"), cursor: c("#0000C8"), selection: c("#A8CCFF"), accent: c("#0040C0"),137        ansi: ["#000000", "#A00000", "#005A00", "#5C4300", "#0030B0", "#6A0096", "#004F57", "#4A4A4A",138               "#303030", "#7A0000", "#004300", "#463300", "#002488", "#500072", "#003A40", "#000000"].map(c))139140    public static let all: [TerminalTheme] = [graphite, midnight, aurora, daylight, paper, porcelain, highContrastDark, highContrastLight]141    public static let defaultDarkID = graphite.id142    public static let defaultLightID = daylight.id143144    public static func theme(id: String) -> TerminalTheme? { all.first { $0.id == id } }145}146147/// Spacing, radii and type scale shared by the application chrome.148public enum Metrics {149    public static let tabBarHeight: Double = 34150    public static let statusBarHeight: Double = 24151    public static let paneHeaderHeight: Double = 22152    public static let sidebarWidth: Double = 232153    public static let cornerRadius: Double = 8154    public static let paneGap: Double = 1155    /// Gap between pane cards and inset of the canvas (SP Term Next card layout).156    public static let cardGap: Double = 6157    public static let inspectorWidth: Double = 360158    public static let spacingXS: Double = 4159    public static let spacingS: Double = 8160    public static let spacingM: Double = 12161    public static let spacingL: Double = 16162    public static let minimumFontSize: Double = 6163    public static let maximumFontSize: Double = 72164}165
Files main
Menu

Theme