theme/brand.rs
1//! Brand: one hue for the greys, one for the accent, one radius.
2//!
3//! The two palettes in `palettes.rs` are designed — every lightness in them was
4//! tuned against a measured contrast ratio, and light is not dark inverted. A
5//! brand does not replace that work; it rotates it. Lightness is never a knob
6//! here, so a branded palette keeps the contrast the shipped one was verified
7//! at, and the only thing that moves is hue.
8
9use gpui::{App, Global, Hsla};
10
11use crate::{Appearance, color, theme::Theme};
12
13/// A hue and how much of it, in oklch terms. `chroma: 0.0` is the shipped
14/// neutral, so [`Brand::default`] reproduces the built-in palette exactly.
15#[derive(Debug, Clone, Copy, PartialEq, Default)]
16pub struct Tint {
17 /// oklch hue, in degrees.
18 pub hue: f32,
19 /// oklch chroma. Neutral ramps live near 0.01–0.05; an accent carries more.
20 pub chroma: f32,
21}
22
23impl Tint {
24 pub const NONE: Self = Self {
25 hue: 0.0,
26 chroma: 0.0,
27 };
28
29 pub const fn new(hue: f32, chroma: f32) -> Self {
30 Self { hue, chroma }
31 }
32}
33
34/// The greys a UI is built on, as oklch hue and chroma.
35///
36/// Tailwind's five neutral families at their 500 step (tailwindcss.com/docs/colors,
37/// read 2026-08-24) — the same list shadcn offers as its base colour, and the
38/// reason these are quoted rather than invented: a neutral that carries hue is
39/// a judgement someone else has already made five times.
40pub const BASE_COLORS: [(&str, Tint); 5] = [
41 ("Neutral", Tint::NONE),
42 ("Stone", Tint::new(58.071, 0.013)),
43 ("Zinc", Tint::new(285.938, 0.016)),
44 ("Gray", Tint::new(264.364, 0.027)),
45 ("Slate", Tint::new(257.417, 0.046)),
46];
47
48/// What an app changes about the shipped palette without redesigning it.
49///
50/// Installed as a gpui [`Global`]; [`Theme::install`] applies it to whatever
51/// palette is registered, so it survives a light/dark switch and composes with
52/// [`set_palette`](crate::set_palette) rather than competing with it.
53#[derive(Debug, Clone, Copy, PartialEq)]
54pub struct Brand {
55 /// The hue every grey in the palette carries.
56 pub tint: Tint,
57 /// The emphasis hue. Left neutral, the accent follows [`Self::tint`] like
58 /// any other grey — which is what the shipped palette already is.
59 pub accent: Tint,
60 /// The base corner radius; every other corner is a ratio of it. See
61 /// [`Theme::BASE_RADIUS`].
62 pub radius: f32,
63 /// How opaque the tint over the blurred window is. See
64 /// [`Theme::VIBRANCY_ALPHA`], where it starts, and [`Theme::vibrancy_tint`].
65 pub vibrancy_alpha: f32,
66 /// Whether the window composites translucent — AppKit's vibrancy.
67 pub vibrancy: bool,
68 /// Whether components paint glass. Separate from [`Self::vibrancy`]:
69 /// SwiftUI's material blends within the window, so an opaque window can
70 /// still carry glass — which is what a Reduce-transparency setting asks
71 /// for and the system's own does not do.
72 pub glass: bool,
73}
74
75impl Global for Brand {}
76
77impl Default for Brand {
78 fn default() -> Self {
79 Self {
80 tint: Tint::NONE,
81 accent: Tint::NONE,
82 radius: Theme::BASE_RADIUS,
83 vibrancy_alpha: Theme::VIBRANCY_ALPHA,
84 vibrancy: Theme::VIBRANCY_ALPHA < 1.0,
85 glass: Theme::VIBRANCY_ALPHA < 1.0,
86 }
87 }
88}
89
90/// The accent's lightness in each appearance — indigo-400's and indigo-600's,
91/// the two steps `palettes.rs` picked so an accent clears WCAG AA on its own
92/// background rather than glowing on one and vanishing on the other.
93const ACCENT_L: (f32, f32) = (0.673, 0.511);
94
95/// The lightness of a *plate* carrying [`Theme::on_accent`], taken from
96/// `danger_strong` — the palette's existing chromatic plate, already tuned to
97/// hold a label in both appearances.
98const PLATE_L: (f32, f32) = (0.58, 0.51);
99
100impl Theme {
101 /// The shipped palette for an appearance, rotated onto a brand. What
102 /// [`Theme::install`] builds, without installing it — for previewing the
103 /// appearance you are not currently painting.
104 pub fn branded(brand: &Brand, appearance: Appearance) -> Self {
105 let mut theme = Self::for_appearance(appearance);
106 brand.apply(&mut theme);
107 theme
108 }
109}
110
111impl Brand {
112 /// Rotate a palette onto this brand's hues.
113 pub fn apply(&self, theme: &mut Theme) {
114 theme.vibrancy_alpha = self.vibrancy_alpha;
115 theme.vibrancy = self.vibrancy;
116 theme.glass = self.glass;
117 // Every colour token, with the rule doing the choosing: a token that is
118 // already grey takes the tint, and one that already carries a hue —
119 // danger, warning, success — is semantic and keeps it. Translucent ink
120 // is skipped because it paints over whatever is beneath it, which is
121 // tinted already.
122 let tokens: [&mut Hsla; 39] = [
123 &mut theme.bg,
124 &mut theme.surface,
125 &mut theme.surface_raised,
126 &mut theme.surface_card,
127 &mut theme.surface_dialog,
128 &mut theme.surface_overlay,
129 &mut theme.element_hover,
130 &mut theme.element_active,
131 &mut theme.border,
132 &mut theme.border_strong,
133 &mut theme.text,
134 &mut theme.text_muted,
135 &mut theme.text_faint,
136 &mut theme.text_dim,
137 &mut theme.solid,
138 &mut theme.on_solid,
139 &mut theme.accent,
140 &mut theme.accent_strong,
141 &mut theme.on_accent,
142 &mut theme.danger,
143 &mut theme.danger_muted,
144 &mut theme.warning,
145 &mut theme.warning_muted,
146 &mut theme.success,
147 &mut theme.busy,
148 &mut theme.success_muted,
149 &mut theme.surface_raised_hover,
150 &mut theme.band,
151 &mut theme.input_bg,
152 &mut theme.selection,
153 &mut theme.cursor,
154 &mut theme.caret,
155 &mut theme.ring,
156 &mut theme.danger_strong,
157 &mut theme.code_text,
158 &mut theme.code_wash,
159 &mut theme.diff_add,
160 &mut theme.diff_del,
161 &mut theme.diff_hunk_bg,
162 ];
163 let syntax: [&mut Hsla; 24] = [
164 &mut theme.syntax.comment,
165 &mut theme.syntax.keyword,
166 &mut theme.syntax.string,
167 &mut theme.syntax.string_special,
168 &mut theme.syntax.escape,
169 &mut theme.syntax.number,
170 &mut theme.syntax.boolean,
171 &mut theme.syntax.type_name,
172 &mut theme.syntax.type_builtin,
173 &mut theme.syntax.constructor,
174 &mut theme.syntax.function,
175 &mut theme.syntax.function_builtin,
176 &mut theme.syntax.macro_name,
177 &mut theme.syntax.property,
178 &mut theme.syntax.constant,
179 &mut theme.syntax.variable,
180 &mut theme.syntax.variable_special,
181 &mut theme.syntax.parameter,
182 &mut theme.syntax.operator,
183 &mut theme.syntax.punctuation,
184 &mut theme.syntax.tag,
185 &mut theme.syntax.attribute,
186 &mut theme.syntax.label,
187 &mut theme.syntax.invalid,
188 ];
189 for slot in tokens.into_iter().chain(syntax) {
190 if slot.a == 1.0 && slot.s <= f32::EPSILON {
191 *slot = color::tint(*slot, self.tint.hue, self.tint.chroma);
192 }
193 }
194
195 if self.accent.chroma > 0.0 {
196 let light = theme.appearance == Appearance::Light;
197 let (accent_l, plate_l) = if light {
198 (ACCENT_L.1, PLATE_L.1)
199 } else {
200 (ACCENT_L.0, PLATE_L.0)
201 };
202 theme.accent = color::oklch(accent_l, self.accent.chroma, self.accent.hue);
203 theme.accent_strong = color::oklch(plate_l, self.accent.chroma, self.accent.hue);
204 // Whichever label the plate can actually hold. The shipped accent is
205 // the maximum-contrast neutral, where the answer is always the
206 // inverse; a chromatic plate at a yellow hue is bright enough that
207 // the inverse would be the unreadable one.
208 theme.on_accent = label_on(theme.accent_strong, theme);
209 }
210 }
211}
212
213/// Whichever of the palette's two extremes the plate can actually hold.
214fn label_on(plate: Hsla, theme: &Theme) -> Hsla {
215 let (a, b) = (theme.solid, theme.on_solid);
216 if color::contrast_ratio(plate, a) >= color::contrast_ratio(plate, b) {
217 a
218 } else {
219 b
220 }
221}
222
223/// Read the installed brand (the default before one is set).
224pub fn brand(cx: &App) -> Brand {
225 cx.try_global::<Brand>().copied().unwrap_or_default()
226}
227
228/// Install a brand and repaint every window with it.
229///
230/// Colours are read imperatively at paint time, so nothing observes the theme
231/// global — the same reason [`appearance::apply`](crate::appearance::apply)
232/// refreshes rather than notifies.
233pub fn set_brand(brand: Brand, cx: &mut App) {
234 cx.set_global(brand);
235 Theme::install(crate::paint::current_appearance(), cx);
236 // Crossing 1.0 is what puts the `NSVisualEffectView` in or takes it out,
237 // and nothing else does it — a repaint alone leaves a window that was
238 // opaque at boot opaque forever.
239 crate::appearance::reapply_window_background(cx);
240 cx.refresh_windows();
241}