Commit bb1f098d50

bb1f098d50d6146c4be3bb7146900f2f82dd4460

parent: 8ab8c91ce9

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 20:35 UTC

Mac: state tag text colour from the theme background

The label is the theme's background or white, whichever contrasts more
with the state colour. state-replace is a theme slot; the default theme
leaves it to the system's red.

Ref #221

Layout: unified · split

Changed files (5)
Sources/OrgPresentation/Palettes.swift +1 · view
@@ -150,6 +150,7 @@ extension ThemeSpec {
150150 "agenda-background": \.bg, "agenda-date": \.fg, "agenda-today": \.accent, "agenda-time": \.secondary, "agenda-category": \.secondary,
151151 "agenda-overdue": \.red, "agenda-due-soon": \.orange, "agenda-event": \.purple,
152152 "habit-clear": \.blue, "habit-ready": \.green, "habit-alert": \.yellow, "habit-overdue": \.red,
153 "state-replace": \.red,
153154 ]
154155
155156 /// Alphas are whole bytes (`#…33`, `#…59`), so a palette written out as a theme file
Sources/OrgPresentation/ThemeSpec.swift +2 · view
@@ -119,6 +119,7 @@ public struct ThemeSpec: Equatable, Sendable {
119119 ("state-normal", "the NORMAL tag"),
120120 ("state-insert", "the INSERT tag"),
121121 ("state-visual", "the VISUAL and V-LINE tags"),
122 ("state-replace", "the REPLACE tag"),
122123 ("agenda-background", "the agenda and board; unset: the system's"),
123124 ("agenda-date", "agenda day headers"),
124125 ("agenda-today", "today's header and the current time"),
@@ -137,6 +138,7 @@ public struct ThemeSpec: Equatable, Sendable {
137138 public static let systemDefaults: Set<String> = [
138139 "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background",
139140 "accent",
141 "state-replace",
140142 ]
141143
142144 // MARK: - The default theme
Sources/Orgstar/Keys.swift +31 −6 · view
@@ -127,7 +127,7 @@ struct StateTag: View {
127127 var body: some View {
128128 Text(label)
129129 .font(.system(.caption, design: .monospaced).bold())
130 .foregroundStyle(.white)
130 .foregroundStyle(labelColor)
131131 .padding(.horizontal, 6)
132132 .padding(.vertical, 1)
133133 .background(color, in: RoundedRectangle(cornerRadius: 4))
@@ -142,13 +142,38 @@ struct StateTag: View {
142142 }
143143 }
144144
145 private var color: Color {
145 private var slot: String {
146146 switch state {
147 case "insert": theme.color("state-insert") ?? .green
148 case "replace": theme.color("state-replace") ?? .red
149 case "visual", "visual-line", "visual-block": theme.color("state-visual") ?? .orange
150 default: theme.color("state-normal") ?? .blue
147 case "insert": "state-insert"
148 case "replace": "state-replace"
149 case "visual", "visual-line", "visual-block": "state-visual"
150 default: "state-normal"
151 }
152 }
153
154 private var color: Color {
155 theme.color(slot) ?? ["state-insert": Color.green, "state-replace": .red, "state-visual": .orange][slot] ?? .blue
156 }
157
158 /// The theme's background on light fills, white on dark ones: whichever contrasts more.
159 private var labelColor: Color {
160 let (theme, slot) = (theme, slot)
161 return theme.dynamic(.white) { dark in
162 let colors = dark ? theme.dark : theme.light
163 guard let fill = colors[slot], let background = colors["background"] else { return nil }
164 let white = RGBA(red: 1, green: 1, blue: 1)
165 return Self.contrast(background, fill) > Self.contrast(white, fill) ? background : white
166 }
167 }
168
169 /// The WCAG contrast ratio of two opaque colors.
170 nonisolated static func contrast(_ a: RGBA, _ b: RGBA) -> Double {
171 func luminance(_ c: RGBA) -> Double {
172 let linear = [c.red, c.green, c.blue].map { $0 <= 0.03928 ? $0 / 12.92 : pow(($0 + 0.055) / 1.055, 2.4) }
173 return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2]
151174 }
175 let (x, y) = (luminance(a), luminance(b))
176 return (max(x, y) + 0.05) / (min(x, y) + 0.05)
152177 }
153178}
154179
Tests/OrgPresentationTests/PresentationTests.swift +6 · view
@@ -153,6 +153,12 @@ struct ThemeSpecTests {
153153 #expect(problems.count == 2)
154154 }
155155
156 @Test func readsTheReplaceStateColor() throws {
157 let (spec, problems) = ThemeSpec.reading(try TOML.parse("[theme]\nstate-replace = \"#cf222e\""))
158 #expect(problems.isEmpty)
159 #expect(spec.light["state-replace"]?.hex == "#cf222e")
160 }
161
156162 @Test func defaultThemeFileReadsBackAsTheDefault() throws {
157163 let (spec, problems) = ThemeSpec.reading(try TOML.parse(ThemeSpec.defaultThemeFile()), over: ThemeSpec(light: [:], dark: [:]))
158164 #expect(problems.isEmpty)
docs/manual/guide/13-configuration.org +2 −1 · view
@@ -801,6 +801,7 @@ The colors stack in this order, each over the one before: the themes chosen with
801801| =state-normal= | the NORMAL tag (Doom keys) |
802802| =state-insert= | the INSERT tag |
803803| =state-visual= | the VISUAL and V-LINE tags |
804| =state-replace= | the REPLACE tag |
804805| =agenda-background= | the agenda and board; the iOS widgets |
805806| =agenda-date= | agenda day headers after today |
806807| =agenda-today= | today's header and the now line |
@@ -814,7 +815,7 @@ The colors stack in this order, each over the one before: the themes chosen with
814815| =habit-alert= | habit graph: due today, last chance |
815816| =habit-overdue= | habit graph: overdue |
816817
817The default theme leaves =sidebar-background=, =sidebar-foreground=, =sidebar-header=, =modeline-background=, =modeline-foreground= and =agenda-background= unset, so those parts keep the standard macOS look. Set them to color those parts too. In the views around the editor (sidebar, modeline, agenda), a color you set for one appearance only leaves the other appearance to the system. In the editor, a color set for one appearance only takes the default theme's color in the other.
818The default theme leaves =sidebar-background=, =sidebar-foreground=, =sidebar-header=, =modeline-background=, =modeline-foreground=, =agenda-background= and =state-replace= unset, so those parts keep the standard macOS look. Set them to color those parts too. In the views around the editor (sidebar, modeline, agenda), a color you set for one appearance only leaves the other appearance to the system. In the editor, a color set for one appearance only takes the default theme's color in the other.
818819
819820An unknown color key, a value that isn't a color, or an unknown table such as =[theme.solarized]= is reported as a problem and skipped.
820821
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