Commit bb1f098d50
Verified · cmc
Changed files (5)
Sources/OrgPresentation/Palettes.swift +1 · view
| @@ -150,6 +150,7 @@ extension ThemeSpec { | |||
| 150 | "agenda-background": \.bg, "agenda-date": \.fg, "agenda-today": \.accent, "agenda-time": \.secondary, "agenda-category": \.secondary, | 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, | 151 | "agenda-overdue": \.red, "agenda-due-soon": \.orange, "agenda-event": \.purple, |
| 152 | "habit-clear": \.blue, "habit-ready": \.green, "habit-alert": \.yellow, "habit-overdue": \.red, | 152 | "habit-clear": \.blue, "habit-ready": \.green, "habit-alert": \.yellow, "habit-overdue": \.red, |
| 153 | "state-replace": \.red, | ||
| 153 | ] | 154 | ] |
| 154 | 155 | ||
| 155 | /// Alphas are whole bytes (`#…33`, `#…59`), so a palette written out as a theme file | 156 | /// 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 { | |||
| 119 | ("state-normal", "the NORMAL tag"), | 119 | ("state-normal", "the NORMAL tag"), |
| 120 | ("state-insert", "the INSERT tag"), | 120 | ("state-insert", "the INSERT tag"), |
| 121 | ("state-visual", "the VISUAL and V-LINE tags"), | 121 | ("state-visual", "the VISUAL and V-LINE tags"), |
| 122 | ("state-replace", "the REPLACE tag"), | ||
| 122 | ("agenda-background", "the agenda and board; unset: the system's"), | 123 | ("agenda-background", "the agenda and board; unset: the system's"), |
| 123 | ("agenda-date", "agenda day headers"), | 124 | ("agenda-date", "agenda day headers"), |
| 124 | ("agenda-today", "today's header and the current time"), | 125 | ("agenda-today", "today's header and the current time"), |
| @@ -137,6 +138,7 @@ public struct ThemeSpec: Equatable, Sendable { | |||
| 137 | public static let systemDefaults: Set<String> = [ | 138 | public static let systemDefaults: Set<String> = [ |
| 138 | "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background", | 139 | "sidebar-background", "sidebar-foreground", "sidebar-header", "modeline-background", "modeline-foreground", "agenda-background", |
| 139 | "accent", | 140 | "accent", |
| 141 | "state-replace", | ||
| 140 | ] | 142 | ] |
| 141 | 143 | ||
| 142 | // MARK: - The default theme | 144 | // MARK: - The default theme |
Sources/Orgstar/Keys.swift +31 −6 · view
| @@ -127,7 +127,7 @@ struct StateTag: View { | |||
| 127 | var body: some View { | 127 | var body: some View { |
| 128 | Text(label) | 128 | Text(label) |
| 129 | .font(.system(.caption, design: .monospaced).bold()) | 129 | .font(.system(.caption, design: .monospaced).bold()) |
| 130 | .foregroundStyle(.white) | 130 | .foregroundStyle(labelColor) |
| 131 | .padding(.horizontal, 6) | 131 | .padding(.horizontal, 6) |
| 132 | .padding(.vertical, 1) | 132 | .padding(.vertical, 1) |
| 133 | .background(color, in: RoundedRectangle(cornerRadius: 4)) | 133 | .background(color, in: RoundedRectangle(cornerRadius: 4)) |
| @@ -142,13 +142,38 @@ struct StateTag: View { | |||
| 142 | } | 142 | } |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | private var color: Color { | 145 | private var slot: String { |
| 146 | switch state { | 146 | switch state { |
| 147 | case "insert": theme.color("state-insert") ?? .green | 147 | case "insert": "state-insert" |
| 148 | case "replace": theme.color("state-replace") ?? .red | 148 | case "replace": "state-replace" |
| 149 | case "visual", "visual-line", "visual-block": theme.color("state-visual") ?? .orange | 149 | case "visual", "visual-line", "visual-block": "state-visual" |
| 150 | default: theme.color("state-normal") ?? .blue | 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] | ||
| 151 | } | 174 | } |
| 175 | let (x, y) = (luminance(a), luminance(b)) | ||
| 176 | return (max(x, y) + 0.05) / (min(x, y) + 0.05) | ||
| 152 | } | 177 | } |
| 153 | } | 178 | } |
| 154 | 179 | ||
Tests/OrgPresentationTests/PresentationTests.swift +6 · view
| @@ -153,6 +153,12 @@ struct ThemeSpecTests { | |||
| 153 | #expect(problems.count == 2) | 153 | #expect(problems.count == 2) |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 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 | |||
| 156 | @Test func defaultThemeFileReadsBackAsTheDefault() throws { | 162 | @Test func defaultThemeFileReadsBackAsTheDefault() throws { |
| 157 | let (spec, problems) = ThemeSpec.reading(try TOML.parse(ThemeSpec.defaultThemeFile()), over: ThemeSpec(light: [:], dark: [:])) | 163 | let (spec, problems) = ThemeSpec.reading(try TOML.parse(ThemeSpec.defaultThemeFile()), over: ThemeSpec(light: [:], dark: [:])) |
| 158 | #expect(problems.isEmpty) | 164 | #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 | |||
| 801 | | =state-normal= | the NORMAL tag (Doom keys) | | 801 | | =state-normal= | the NORMAL tag (Doom keys) | |
| 802 | | =state-insert= | the INSERT tag | | 802 | | =state-insert= | the INSERT tag | |
| 803 | | =state-visual= | the VISUAL and V-LINE tags | | 803 | | =state-visual= | the VISUAL and V-LINE tags | |
| 804 | | =state-replace= | the REPLACE tag | | ||
| 804 | | =agenda-background= | the agenda and board; the iOS widgets | | 805 | | =agenda-background= | the agenda and board; the iOS widgets | |
| 805 | | =agenda-date= | agenda day headers after today | | 806 | | =agenda-date= | agenda day headers after today | |
| 806 | | =agenda-today= | today's header and the now line | | 807 | | =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 | |||
| 814 | | =habit-alert= | habit graph: due today, last chance | | 815 | | =habit-alert= | habit graph: due today, last chance | |
| 815 | | =habit-overdue= | habit graph: overdue | | 816 | | =habit-overdue= | habit graph: overdue | |
| 816 | 817 | ||
| 817 | The 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. | 818 | The 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. |
| 818 | 819 | ||
| 819 | An 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. | 820 | An 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. |
| 820 | 821 | ||