Commit c78d052e44

c78d052e444202adc95e241ac9e83788eea4cf75

parent: 3faae816a4

Verified · cmc

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

Palettes, with the default theme as Default; accent theme color

Layout: unified · split

Sources/OrgPresentation/Palettes.swift added +39
@@ -0,0 +1,39 @@
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 static let lightPalettes: [Palette] = [
33 Palette(id: "default-light", name: "Default", appearance: .light, colors: ThemeSpec.default.light),
34 ]
35
36 static let darkPalettes: [Palette] = [
37 Palette(id: "default-dark", name: "Default", appearance: .dark, colors: ThemeSpec.default.dark),
38 ]
39}
Sources/OrgPresentation/ThemeSpec.swift +3 −1
@@ -73,6 +73,7 @@ public struct ThemeSpec: Equatable, Sendable {
7373 ("foreground", "body text"),
7474 ("cursor", "the caret"),
7575 ("selection", "selected text's background"),
76 ("accent", "buttons, toggles and selection in lists; unset: the system's"),
7677 ("heading-1", "level 1 headings"), ("heading-2", "level 2 headings"), ("heading-3", "level 3 headings"),
7778 ("heading-4", "level 4 headings"), ("heading-5", "level 5 headings"), ("heading-6", "level 6 headings"),
7879 ("heading-7", "level 7 headings"), ("heading-8", "level 8 and deeper headings"),
@@ -135,6 +136,7 @@ public struct ThemeSpec: Equatable, Sendable {
135136 /// Keys the default theme leaves to the system, so windows keep the macOS look.
136137 public static let systemDefaults: Set<String> = [
137138 "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background",
139 "accent",
138140 ]
139141
140142 // MARK: - The default theme
@@ -188,7 +190,7 @@ public struct ThemeSpec: Equatable, Sendable {
188190 // MARK: - Reading
189191
190192 /// Keys of `[theme]` that aren't colors; config.toml's settings handle them.
191 public static let typeKeys: Set<String> = ["font", "body-font", "font-size", "line-spacing", "text-width", "heading-size-step", "theme-file"]
193 public static let typeKeys: Set<String> = ["font", "body-font", "font-size", "line-spacing", "text-width", "heading-size-step", "theme-file", "light-theme", "dark-theme"]
192194
193195 /// Whether a table holds theme colors.
194196 public static func isThemeTable(_ name: String) -> Bool {
Sources/Orgstar/ThemeColors.swift +1
@@ -37,6 +37,7 @@ extension ThemeSpec {
3737 case "sidebar-background", "modeline-background": .windowBackgroundColor
3838 case "agenda-background", "background": .textBackgroundColor
3939 case "sidebar-header", "modeline-foreground", "agenda-time", "agenda-category", "tags": .secondaryLabelColor
40 case "accent": .controlAccentColor
4041 default: .labelColor
4142 }
4243 }
Tests/OrgPresentationTests/PaletteTests.swift added +31
@@ -0,0 +1,31 @@
1import OrgPresentation
2import Testing
3
4struct PaletteTests {
5 @Test func defaultPalettesAreTheDefaultTheme() throws {
6 let light = try #require(ThemeSpec.palette("default-light", .light))
7 let dark = try #require(ThemeSpec.palette("default-dark", .dark))
8 #expect(light.colors == ThemeSpec.default.light && dark.colors == ThemeSpec.default.dark)
9 #expect(light.name == "Default" && ThemeSpec.palettes(.light).first == light && ThemeSpec.palettes(.dark).first == dark)
10 #expect(ThemeSpec.defaultPaletteID(.light) == "default-light" && ThemeSpec.defaultPaletteID(.dark) == "default-dark")
11 }
12
13 @Test func aPaletteIsFoundOnlyInItsAppearance() {
14 #expect(ThemeSpec.palette("default-light", .dark) == nil)
15 #expect(ThemeSpec.palette("nope", .light) == nil)
16 }
17
18 @Test func accentIsASlotTheDefaultLeavesToTheSystem() {
19 #expect(ThemeSpec.slots.contains { $0.key == "accent" })
20 #expect(ThemeSpec.systemDefaults.contains("accent"))
21 #expect(ThemeSpec.default.light["accent"] == nil && ThemeSpec.default.dark["accent"] == nil)
22 }
23
24 @Test func everyPaletteHasExactlyTheSlotKeys() {
25 let all = Set(ThemeSpec.slots.map(\.key))
26 for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
27 let expected = palette.id.hasPrefix("default-") ? all.subtracting(ThemeSpec.systemDefaults) : all
28 #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))")
29 }
30 }
31}