Commit 6005b32424

6005b324240fed2a2b67b9f42a53949d9700715c

parent: c78d052e44

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-14 13:55 UTC

Fourteen built-in palettes, with a contrast test

Layout: unified · split

Sources/OrgPresentation/Palettes.swift +93
@@ -29,11 +29,104 @@ extension ThemeSpec {
29 "default-" + appearance.rawValue 29 "default-" + appearance.rawValue
30 } 30 }
31 31
32 // Upstream palettes; see THIRD_PARTY.org. Colors darkened from upstream to read at 3:1 on
33 // the background: Solarized Light yellow, green and cyan; Catppuccin Latte orange, yellow
34 // and green; Tomorrow orange and yellow.
32 static let lightPalettes: [Palette] = [ 35 static let lightPalettes: [Palette] = [
33 Palette(id: "default-light", name: "Default", appearance: .light, colors: ThemeSpec.default.light), 36 Palette(id: "default-light", name: "Default", appearance: .light, colors: ThemeSpec.default.light),
37 ported("solarized-light", "Solarized Light", .light, Roles(
38 bg: "#fdf6e3", alt: "#eee8d5", fg: "#586e75", dim: "#93a1a1", red: "#dc322f", orange: "#cb4b16", yellow: "#a57c00",
39 green: "#738a05", cyan: "#258f86", blue: "#268bd2", purple: "#6c71c4", accent: "#268bd2")),
40 ported("gruvbox-light", "Gruvbox Light", .light, Roles(
41 bg: "#fbf1c7", alt: "#ebdbb2", fg: "#3c3836", dim: "#928374", red: "#9d0006", orange: "#af3a03", yellow: "#b57614",
42 green: "#79740e", cyan: "#427b58", blue: "#076678", purple: "#8f3f71", accent: "#076678")),
43 ported("modus-operandi", "Modus Operandi", .light, Roles(
44 bg: "#ffffff", alt: "#f2f2f2", fg: "#000000", dim: "#595959", red: "#a60000", orange: "#972500", yellow: "#6f5500",
45 green: "#006800", cyan: "#005e8b", blue: "#0031a9", purple: "#721045", accent: "#0031a9")),
46 ported("catppuccin-latte", "Catppuccin Latte", .light, Roles(
47 bg: "#eff1f5", alt: "#e6e9ef", fg: "#4c4f69", dim: "#8c8fa1", red: "#d20f39", orange: "#d0590a", yellow: "#b06f13",
48 green: "#388c24", cyan: "#179299", blue: "#1e66f5", purple: "#8839ef", accent: "#8839ef")),
49 ported("tomorrow", "Tomorrow", .light, Roles(
50 bg: "#ffffff", alt: "#efefef", fg: "#4d4d4c", dim: "#8e908c", red: "#c82829", orange: "#c76b17", yellow: "#a87e00",
51 green: "#718c00", cyan: "#3e999f", blue: "#4271ae", purple: "#8959a8", accent: "#4271ae")),
52 // orgstar's own: warm paper and ink, for long reading.
53 ported("paper", "Paper", .light, Roles(
54 bg: "#f8f3e6", alt: "#efe8d6", fg: "#3b3428", dim: "#7d7262", red: "#a8322d", orange: "#a85a1f", yellow: "#8a6d1a",
55 green: "#4d6b2f", cyan: "#2f6b6b", blue: "#2f5486", purple: "#6b4a85", accent: "#a85a1f")),
34 ] 56 ]
35 57
36 static let darkPalettes: [Palette] = [ 58 static let darkPalettes: [Palette] = [
37 Palette(id: "default-dark", name: "Default", appearance: .dark, colors: ThemeSpec.default.dark), 59 Palette(id: "default-dark", name: "Default", appearance: .dark, colors: ThemeSpec.default.dark),
60 ported("solarized-dark", "Solarized Dark", .dark, Roles(
61 bg: "#002b36", alt: "#073642", fg: "#93a1a1", dim: "#586e75", red: "#dc322f", orange: "#cb4b16", yellow: "#b58900",
62 green: "#859900", cyan: "#2aa198", blue: "#268bd2", purple: "#6c71c4", accent: "#268bd2")),
63 ported("gruvbox-dark", "Gruvbox Dark", .dark, Roles(
64 bg: "#282828", alt: "#3c3836", fg: "#ebdbb2", dim: "#928374", red: "#fb4934", orange: "#fe8019", yellow: "#fabd2f",
65 green: "#b8bb26", cyan: "#8ec07c", blue: "#83a598", purple: "#d3869b", accent: "#83a598")),
66 ported("modus-vivendi", "Modus Vivendi", .dark, Roles(
67 bg: "#000000", alt: "#1e1e1e", fg: "#ffffff", dim: "#989898", red: "#ff5f59", orange: "#fec43f", yellow: "#d0bc00",
68 green: "#44bc44", cyan: "#00d3d0", blue: "#2fafff", purple: "#b6a0ff", accent: "#2fafff")),
69 ported("catppuccin-mocha", "Catppuccin Mocha", .dark, Roles(
70 bg: "#1e1e2e", alt: "#181825", fg: "#cdd6f4", dim: "#7f849c", red: "#f38ba8", orange: "#fab387", yellow: "#f9e2af",
71 green: "#a6e3a1", cyan: "#94e2d5", blue: "#89b4fa", purple: "#cba6f7", accent: "#cba6f7")),
72 ported("nord", "Nord", .dark, Roles(
73 bg: "#2e3440", alt: "#3b4252", fg: "#d8dee9", dim: "#616e88", red: "#bf616a", orange: "#d08770", yellow: "#ebcb8b",
74 green: "#a3be8c", cyan: "#8fbcbb", blue: "#88c0d0", purple: "#b48ead", accent: "#88c0d0")),
75 ported("dracula", "Dracula", .dark, Roles(
76 bg: "#282a36", alt: "#44475a", fg: "#f8f8f2", dim: "#6272a4", red: "#ff5555", orange: "#ffb86c", yellow: "#f1fa8c",
77 green: "#50fa7b", cyan: "#8be9fd", blue: "#8be9fd", purple: "#bd93f9", accent: "#bd93f9")),
78 ported("tokyo-night", "Tokyo Night", .dark, Roles(
79 bg: "#1a1b26", alt: "#16161e", fg: "#c0caf5", dim: "#565f89", red: "#f7768e", orange: "#ff9e64", yellow: "#e0af68",
80 green: "#9ece6a", cyan: "#7dcfff", blue: "#7aa2f7", purple: "#bb9af7", accent: "#7aa2f7")),
81 ported("doom-one", "doom-one", .dark, Roles(
82 bg: "#282c34", alt: "#21242b", fg: "#bbc2cf", dim: "#5b6268", red: "#ff6c6b", orange: "#da8548", yellow: "#ecbe7b",
83 green: "#98be65", cyan: "#46d9ff", blue: "#51afef", purple: "#c678dd", accent: "#51afef")),
38 ] 84 ]
39} 85}
86
87extension ThemeSpec {
88 /// A palette's named colors, from which every slot is set.
89 public struct Roles: Sendable {
90 public var bg, alt, fg, dim, red, orange, yellow, green, cyan, blue, purple, accent: String
91
92 /// Slot → role. `selection` and `inline-background` are translucent roles.
93 static let slots: [String: KeyPath<Roles, String> & Sendable] = [
94 "background": \.bg, "foreground": \.fg, "cursor": \.accent, "accent": \.accent,
95 "heading-1": \.blue, "heading-2": \.purple, "heading-3": \.green, "heading-4": \.orange,
96 "heading-5": \.blue, "heading-6": \.purple, "heading-7": \.green, "heading-8": \.orange,
97 "todo": \.red, "todo-next": \.blue, "todo-waiting": \.yellow, "done": \.green, "todo-cancelled": \.dim,
98 "priority": \.orange, "priority-a": \.red, "priority-b": \.orange, "priority-c": \.dim,
99 "tags": \.dim, "link": \.blue, "timestamp": \.purple, "code": \.orange, "verbatim": \.cyan,
100 "markup": \.dim, "comment": \.dim, "keyword": \.dim, "metadata": \.dim, "special": \.cyan,
101 "block-background": \.alt, "block-delimiter": \.dim, "table": \.fg,
102 "line-number": \.dim, "line-number-current": \.fg,
103 "syntax-keyword": \.purple, "syntax-string": \.green, "syntax-comment": \.dim, "syntax-function": \.blue,
104 "syntax-type": \.yellow, "syntax-number": \.orange, "syntax-property": \.cyan, "syntax-label": \.purple,
105 "sidebar-background": \.alt, "sidebar-foreground": \.fg, "sidebar-header": \.dim,
106 "modeline-background": \.alt, "modeline-foreground": \.fg, "modeline-highlight": \.accent,
107 "state-normal": \.blue, "state-insert": \.green, "state-visual": \.orange,
108 "agenda-background": \.bg, "agenda-date": \.fg, "agenda-today": \.accent, "agenda-time": \.dim, "agenda-category": \.dim,
109 "agenda-overdue": \.red, "agenda-due-soon": \.orange, "agenda-event": \.purple,
110 "habit-clear": \.blue, "habit-ready": \.green, "habit-alert": \.yellow, "habit-overdue": \.red,
111 ]
112
113 /// Alphas are whole bytes (`#…33`, `#…59`), so a palette written out as a theme file
114 /// reads back equal.
115 public func colors(dark: Bool) -> [String: RGBA] {
116 var out = Self.slots.compactMapValues { RGBA(hex: self[keyPath: $0]) }
117 if var selection = RGBA(hex: accent) {
118 selection.alpha = (dark ? 0x59 : 0x33) / 255
119 out["selection"] = selection
120 }
121 if var inline = RGBA(hex: dim) {
122 inline.alpha = 0x33 / 255
123 out["inline-background"] = inline
124 }
125 return out
126 }
127 }
128
129 private static func ported(_ id: String, _ name: String, _ appearance: Palette.Appearance, _ roles: Roles) -> Palette {
130 Palette(id: id, name: name, appearance: appearance, colors: roles.colors(dark: appearance == .dark))
131 }
132}
THIRD_PARTY.org +10 −1
@@ -11,5 +11,14 @@ Orgstar is 0BSD. It builds on these projects, under their own licenses.
11| [[https://github.com/6cdh/tree-sitter-scheme][tree-sitter-scheme]] | Highlighting | MIT | 11| [[https://github.com/6cdh/tree-sitter-scheme][tree-sitter-scheme]] | Highlighting | MIT |
12| [[https://github.com/Wilfred/tree-sitter-elisp][tree-sitter-elisp]] | Highlighting (vendored in =Sources/TreeSitterElisp=) | MIT | 12| [[https://github.com/Wilfred/tree-sitter-elisp][tree-sitter-elisp]] | Highlighting (vendored in =Sources/TreeSitterElisp=) | MIT |
13| [[https://github.com/sogaiu/tree-sitter-clojure][tree-sitter-clojure]] | Highlighting (vendored in =Sources/TreeSitterClojure=) | CC0-1.0 | 13| [[https://github.com/sogaiu/tree-sitter-clojure][tree-sitter-clojure]] | Highlighting (vendored in =Sources/TreeSitterClojure=) | CC0-1.0 |
14| [[https://ethanschoonover.com/solarized/][Solarized]] | Colors of the Solarized themes | MIT |
15| [[https://github.com/morhetz/gruvbox][Gruvbox]] | Colors of the Gruvbox themes | MIT |
16| [[https://protesilaos.com/emacs/modus-themes][Modus themes]] | Colors of Modus Operandi and Modus Vivendi (color values only, no code) | GPL-3.0-or-later |
17| [[https://github.com/catppuccin/catppuccin][Catppuccin]] | Colors of Catppuccin Latte and Mocha | MIT |
18| [[https://github.com/chriskempson/tomorrow-theme][Tomorrow]] | Colors of the Tomorrow theme | MIT |
19| [[https://www.nordtheme.com][Nord]] | Colors of the Nord theme | MIT |
20| [[https://draculatheme.com][Dracula]] | Colors of the Dracula theme | MIT |
21| [[https://github.com/folke/tokyonight.nvim][Tokyo Night]] | Colors of the Tokyo Night theme | Apache-2.0 |
22| [[https://github.com/doomemacs/themes][doom-themes]] | Colors of doom-one | MIT |
14 23
15The JavaScript, Lua, Python and YAML scanners are copied into =Sources/TreeSitterScanners= with their licenses. Highlight queries are embedded in =Sources/OrgHighlight/Queries.swift= by =scripts/update-highlight-queries.sh=. 24The JavaScript, Lua, Python and YAML scanners are copied into =Sources/TreeSitterScanners= with their licenses. Highlight queries are embedded in =Sources/OrgHighlight/Queries.swift= by =scripts/update-highlight-queries.sh=. The theme colors are in =Sources/OrgPresentation/Palettes.swift=; a few light-theme colors are darkened from upstream for contrast, as noted there.
Tests/OrgPresentationTests/PaletteTests.swift +32
@@ -1,3 +1,4 @@
1import Foundation
1import OrgPresentation 2import OrgPresentation
2import Testing 3import Testing
3 4
@@ -28,4 +29,35 @@ struct PaletteTests {
28 #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))") 29 #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))")
29 } 30 }
30 } 31 }
32 @Test func theCatalogIsInOrder() {
33 #expect(ThemeSpec.palettes(.light).map(\.id) == ["default-light", "solarized-light", "gruvbox-light", "modus-operandi", "catppuccin-latte", "tomorrow", "paper"])
34 #expect(ThemeSpec.palettes(.dark).map(\.id) == ["default-dark", "solarized-dark", "gruvbox-dark", "modus-vivendi", "catppuccin-mocha", "nord", "dracula", "tokyo-night", "doom-one"])
35 }
36
37 /// WCAG 2 contrast ratio.
38 static func contrast(_ a: RGBA, _ b: RGBA) -> Double {
39 func luminance(_ c: RGBA) -> Double {
40 func channel(_ v: Double) -> Double { v <= 0.03928 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4) }
41 return 0.2126 * channel(c.red) + 0.7152 * channel(c.green) + 0.0722 * channel(c.blue)
42 }
43 let (x, y) = (luminance(a), luminance(b))
44 return (max(x, y) + 0.05) / (min(x, y) + 0.05)
45 }
46
47 @Test func textReadsOnTheBackground() throws {
48 let body = ["foreground", "table"]
49 let marks = ["heading-1", "heading-2", "heading-3", "heading-4", "todo", "todo-next", "todo-waiting", "done", "link", "timestamp", "code",
50 "syntax-keyword", "syntax-string", "syntax-function", "syntax-type", "syntax-number", "syntax-property", "syntax-label"]
51 for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
52 let background = try #require(palette.colors["background"])
53 for key in body {
54 let ratio = Self.contrast(try #require(palette.colors[key]), background)
55 #expect(ratio >= 4.5, "\(palette.id) \(key): \(ratio)")
56 }
57 for key in marks {
58 let ratio = Self.contrast(try #require(palette.colors[key]), background)
59 #expect(ratio >= 3, "\(palette.id) \(key): \(ratio)")
60 }
61 }
62 }
31} 63}