Sources/OrgPresentation/Palettes.swift

065a5cf4c58176da6374247e13bd1b1580a10a86
orgstar/Sources/OrgPresentation/Palettes.swift

163 lines · 10296 bytes

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

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form