Sources/OrgPresentation/Palettes.swift

189 lines · 12117 bytes

  1import Foundation
  2
  3/// A built-in theme for one appearance, chosen with `light-theme` or `dark-theme`.
  4public struct Palette: Sendable, Equatable {
  5    public enum Appearance: String, Sendable { case light, dark }
  6
  7    public let id: String
  8    public let name: String
  9    public let appearance: Appearance
 10    public let colors: [String: RGBA]
 11    /// Body text or coloured roles under 4.5:1 on the background, kept as upstream draws them.
 12    public let lowContrast: Bool
 13
 14    /// `name` in the user's language: Default and Paper are translated, the others are names.
 15    public var displayName: String {
 16        switch id {
 17        case "default-light", "default-dark": String(localized: "Default")
 18        case "paper": String(localized: "Paper")
 19        default: name
 20        }
 21    }
 22
 23    public init(id: String, name: String, appearance: Appearance, colors: [String: RGBA], lowContrast: Bool = false) {
 24        self.id = id
 25        self.name = name
 26        self.appearance = appearance
 27        self.colors = colors
 28        self.lowContrast = lowContrast
 29    }
 30}
 31
 32extension ThemeSpec {
 33    /// The built-in palettes of `appearance`, Default first.
 34    public static func palettes(_ appearance: Palette.Appearance) -> [Palette] {
 35        appearance == .light ? lightPalettes : darkPalettes
 36    }
 37
 38    public static func palette(_ id: String, _ appearance: Palette.Appearance) -> Palette? {
 39        palettes(appearance).first { $0.id == id }
 40    }
 41
 42    public static func defaultPaletteID(_ appearance: Palette.Appearance) -> String {
 43        "default-" + appearance.rawValue
 44    }
 45
 46    /// `key`'s color in one appearance for the editor: the spec's own, else the default
 47    /// theme's, else the spec's for the other appearance. Nil for a `systemDefaults` key the
 48    /// spec leaves unset in this appearance, which the system colors there.
 49    public func editorColor(_ key: String, dark isDark: Bool) -> RGBA? {
 50        if let own = color(key, dark: isDark) { return own }
 51        guard !Self.systemDefaults.contains(key) else { return nil }
 52        return Self.default.color(key, dark: isDark) ?? color(key, dark: !isDark)
 53    }
 54
 55    /// The default theme with each appearance's colors from the palette `light-theme` and
 56    /// `dark-theme` name; Default where one is unset or unknown, with a problem for unknown.
 57    public static func base(light: String?, dark: String?) -> (ThemeSpec, [String]) {
 58        var spec = ThemeSpec.default
 59        var problems: [String] = []
 60        for (id, appearance) in [(light, Palette.Appearance.light), (dark, .dark)] {
 61            guard let id, !id.isEmpty, id != defaultPaletteID(appearance) else { continue }
 62            guard let palette = palette(id, appearance) else {
 63                problems.append("config.toml: unknown \(appearance.rawValue)-theme \(id)")
 64                continue
 65            }
 66            if appearance == .light { spec.light = palette.colors } else { spec.dark = palette.colors }
 67        }
 68        return (spec, problems)
 69    }
 70
 71    // Upstream palettes; see THIRD_PARTY.org. Colors darkened from upstream to read at 3:1 on
 72    // the background: Solarized Light yellow, green and cyan; Catppuccin Latte orange, yellow
 73    // and green; Tomorrow orange and yellow. `secondary` is `dim` mixed toward `fg` until it
 74    // reads at 4.5:1 on the background.
 75    static let lightPalettes: [Palette] = [
 76        Palette(id: "default-light", name: "Default", appearance: .light, colors: ThemeSpec.default.light),
 77        ported("solarized-light", "Solarized Light", .light, lowContrast: true, Roles(
 78            bg: "#fdf6e3", alt: "#eee8d5", fg: "#586e75", dim: "#93a1a1", secondary: "#5f747a", red: "#dc322f", orange: "#cb4b16",
 79            yellow: "#a57c00", green: "#738a05", cyan: "#258f86", blue: "#268bd2", purple: "#6c71c4", accent: "#268bd2")),
 80        ported("gruvbox-light", "Gruvbox Light", .light, Roles(
 81            bg: "#fbf1c7", alt: "#ebdbb2", fg: "#3c3836", dim: "#928374", secondary: "#776c61", red: "#9d0006", orange: "#af3a03",
 82            yellow: "#b57614", green: "#79740e", cyan: "#427b58", blue: "#076678", purple: "#8f3f71", accent: "#076678")),
 83        ported("modus-operandi", "Modus Operandi", .light, Roles(
 84            bg: "#ffffff", alt: "#f2f2f2", fg: "#000000", dim: "#595959", secondary: "#595959", red: "#a60000", orange: "#972500",
 85            yellow: "#6f5500", green: "#006800", cyan: "#005e8b", blue: "#0031a9", purple: "#721045", accent: "#0031a9")),
 86        ported("catppuccin-latte", "Catppuccin Latte", .light, Roles(
 87            bg: "#eff1f5", alt: "#e6e9ef", fg: "#4c4f69", dim: "#8c8fa1", secondary: "#696c83", red: "#d20f39", orange: "#d0590a",
 88            yellow: "#b06f13", green: "#388c24", cyan: "#179299", blue: "#1e66f5", purple: "#8839ef", accent: "#8839ef")),
 89        ported("tomorrow", "Tomorrow", .light, Roles(
 90            bg: "#ffffff", alt: "#efefef", fg: "#4d4d4c", dim: "#8e908c", secondary: "#767774", red: "#c82829", orange: "#c76b17",
 91            yellow: "#a87e00", green: "#718c00", cyan: "#3e999f", blue: "#4271ae", purple: "#8959a8", accent: "#4271ae")),
 92        // orgstar's own: warm paper and ink, for long reading.
 93        ported("paper", "Paper", .light, Roles(
 94            bg: "#f8f3e6", alt: "#efe8d6", fg: "#3b3428", dim: "#7d7262", secondary: "#796e5f", red: "#a8322d", orange: "#a85a1f",
 95            yellow: "#8a6d1a", green: "#4d6b2f", cyan: "#2f6b6b", blue: "#2f5486", purple: "#6b4a85", accent: "#a85a1f")),
 96    ]
 97
 98    static let darkPalettes: [Palette] = [
 99        Palette(id: "default-dark", name: "Default", appearance: .dark, colors: ThemeSpec.default.dark),
100        ported("solarized-dark", "Solarized Dark", .dark, lowContrast: true, Roles(
101            bg: "#002b36", alt: "#073642", fg: "#93a1a1", dim: "#586e75", secondary: "#7f9092", red: "#dc322f", orange: "#cb4b16",
102            yellow: "#b58900", green: "#859900", cyan: "#2aa198", blue: "#268bd2", purple: "#6c71c4", accent: "#268bd2")),
103        ported("gruvbox-dark", "Gruvbox Dark", .dark, Roles(
104            bg: "#282828", alt: "#3c3836", fg: "#ebdbb2", dim: "#928374", secondary: "#9b8c7a", red: "#fb4934", orange: "#fe8019",
105            yellow: "#fabd2f", green: "#b8bb26", cyan: "#8ec07c", blue: "#83a598", purple: "#d3869b", accent: "#83a598")),
106        ported("modus-vivendi", "Modus Vivendi", .dark, Roles(
107            bg: "#000000", alt: "#1e1e1e", fg: "#ffffff", dim: "#989898", secondary: "#989898", red: "#ff5f59", orange: "#fec43f",
108            yellow: "#d0bc00", green: "#44bc44", cyan: "#00d3d0", blue: "#2fafff", purple: "#b6a0ff", accent: "#2fafff")),
109        ported("catppuccin-mocha", "Catppuccin Mocha", .dark, Roles(
110            bg: "#1e1e2e", alt: "#181825", fg: "#cdd6f4", dim: "#7f849c", secondary: "#81869e", red: "#f38ba8", orange: "#fab387",
111            yellow: "#f9e2af", green: "#a6e3a1", cyan: "#94e2d5", blue: "#89b4fa", purple: "#cba6f7", accent: "#cba6f7")),
112        ported("nord", "Nord", .dark, Roles(
113            bg: "#2e3440", alt: "#3b4252", fg: "#d8dee9", dim: "#616e88", secondary: "#929cb0", red: "#bf616a", orange: "#d08770",
114            yellow: "#ebcb8b", green: "#a3be8c", cyan: "#8fbcbb", blue: "#88c0d0", purple: "#b48ead", accent: "#88c0d0")),
115        ported("dracula", "Dracula", .dark, Roles(
116            bg: "#282a36", alt: "#44475a", fg: "#f8f8f2", dim: "#6272a4", secondary: "#8491b6", red: "#ff5555", orange: "#ffb86c",
117            yellow: "#f1fa8c", green: "#50fa7b", cyan: "#8be9fd", blue: "#8be9fd", purple: "#bd93f9", accent: "#bd93f9")),
118        ported("tokyo-night", "Tokyo Night", .dark, Roles(
119            bg: "#1a1b26", alt: "#16161e", fg: "#c0caf5", dim: "#565f89", secondary: "#7881ac", red: "#f7768e", orange: "#ff9e64",
120            yellow: "#e0af68", green: "#9ece6a", cyan: "#7dcfff", blue: "#7aa2f7", purple: "#bb9af7", accent: "#7aa2f7")),
121        ported("doom-one", "doom-one", .dark, Roles(
122            bg: "#282c34", alt: "#21242b", fg: "#bbc2cf", dim: "#5b6268", secondary: "#8c939d", red: "#ff6c6b", orange: "#da8548",
123            yellow: "#ecbe7b", green: "#98be65", cyan: "#46d9ff", blue: "#51afef", purple: "#c678dd", accent: "#51afef")),
124    ]
125}
126
127extension ThemeSpec {
128    /// A palette's named colors, from which every slot is set.
129    public struct Roles: Sendable {
130        /// `dim` is for marks that aren't read as text (markup, delimiters, line numbers);
131        /// `secondary` for quieter text (tags, comments, planning lines, agenda times).
132        public var bg, alt, fg, dim, secondary, red, orange, yellow, green, cyan, blue, purple, accent: String
133
134        /// Slot → role. `selection` and `inline-background` are translucent roles.
135        static let slots: [String: KeyPath<Roles, String> & Sendable] = [
136            "background": \.bg, "foreground": \.fg, "cursor": \.accent, "accent": \.accent,
137            "heading-1": \.blue, "heading-2": \.purple, "heading-3": \.green, "heading-4": \.orange,
138            "heading-5": \.blue, "heading-6": \.purple, "heading-7": \.green, "heading-8": \.orange,
139            "todo": \.red, "todo-next": \.blue, "todo-waiting": \.yellow, "done": \.green, "todo-cancelled": \.secondary,
140            "priority": \.orange, "priority-a": \.red, "priority-b": \.orange, "priority-c": \.secondary,
141            "tags": \.secondary, "link": \.blue, "timestamp": \.purple, "code": \.orange, "verbatim": \.cyan,
142            "markup": \.dim, "comment": \.secondary, "keyword": \.secondary, "metadata": \.secondary, "special": \.cyan,
143            "block-background": \.alt, "block-delimiter": \.dim, "table": \.fg,
144            "line-number": \.dim, "line-number-current": \.fg,
145            "syntax-keyword": \.purple, "syntax-string": \.green, "syntax-comment": \.secondary, "syntax-function": \.blue,
146            "syntax-type": \.yellow, "syntax-number": \.orange, "syntax-property": \.cyan, "syntax-label": \.purple,
147            "sidebar-background": \.alt, "sidebar-foreground": \.fg, "sidebar-header": \.secondary,
148            "modeline-background": \.alt, "modeline-foreground": \.fg, "modeline-highlight": \.accent,
149            "state-normal": \.blue, "state-insert": \.green, "state-visual": \.orange,
150            "agenda-background": \.bg, "agenda-date": \.fg, "agenda-today": \.accent, "agenda-time": \.secondary, "agenda-category": \.secondary,
151            "agenda-overdue": \.red, "agenda-due-soon": \.orange, "agenda-event": \.purple,
152            "habit-clear": \.blue, "habit-ready": \.green, "habit-alert": \.yellow, "habit-overdue": \.red,
153        ]
154
155        /// Alphas are whole bytes (`#…33`, `#…59`), so a palette written out as a theme file
156        /// reads back equal.
157        public func colors(dark: Bool) -> [String: RGBA] {
158            var out = Self.slots.compactMapValues { RGBA(hex: self[keyPath: $0]) }
159            if var selection = RGBA(hex: accent) {
160                selection.alpha = (dark ? 0x59 : 0x33) / 255
161                out["selection"] = selection
162            }
163            if var inline = RGBA(hex: dim) {
164                inline.alpha = 0x33 / 255
165                out["inline-background"] = inline
166            }
167            return out
168        }
169    }
170
171    private static func ported(_ id: String, _ name: String, _ appearance: Palette.Appearance, lowContrast: Bool = false, _ roles: Roles) -> Palette {
172        Palette(id: id, name: name, appearance: appearance, colors: roles.colors(dark: appearance == .dark), lowContrast: lowContrast)
173    }
174}
175
176extension ThemeSpec {
177    /// Two palettes as a theme file, every color written out, in slot order.
178    public static func themeFile(light: Palette, dark: Palette) -> String {
179        var out = "# \(light.name) and \(dark.name), copied from orgstar's built-in themes. Used with\n"
180            + "# theme-file = \"my-theme.toml\" under [theme] in config.toml; colors there apply on top.\n"
181        for (table, palette) in [("theme.light", light), ("theme.dark", dark)] {
182            out += "\n[\(table)]\n"
183            for slot in slots {
184                if let color = palette.colors[slot.key] { out += "\(slot.key) = \"\(color.hex)\"\n" }
185            }
186        }
187        return out
188    }
189}