Skip to main content

theme/
brand.rs

1//! Brand: one hue for the greys, one for the accent, one radius, and the
2//! text ink.
3//!
4//! Apart from the text ink, a brand moves hue alone: every other lightness in
5//! `palettes.rs` stays as the palette set it.
6
7use gpui::{App, Global, Hsla};
8
9use crate::{Appearance, color, theme::Theme};
10
11/// A hue and how much of it, in oklch terms. `chroma: 0.0` is the shipped
12/// neutral, so [`Brand::default`] reproduces the built-in palette exactly.
13#[derive(Debug, Clone, Copy, PartialEq, Default)]
14pub struct Tint {
15    /// oklch hue, in degrees.
16    pub hue: f32,
17    /// oklch chroma. Neutral ramps live near 0.01–0.05; an accent carries more.
18    pub chroma: f32,
19}
20
21impl Tint {
22    pub const NONE: Self = Self {
23        hue: 0.0,
24        chroma: 0.0,
25    };
26
27    pub const fn new(hue: f32, chroma: f32) -> Self {
28        Self { hue, chroma }
29    }
30}
31
32/// The greys a UI is built on, as oklch hue and chroma.
33///
34/// Tailwind's five neutral families at their 500 step (tailwindcss.com/docs/colors,
35/// read 2026-08-24) — the same list shadcn offers as its base colour, and the
36/// reason these are quoted rather than invented: a neutral that carries hue is
37/// a judgement someone else has already made five times.
38pub const BASE_COLORS: [(&str, Tint); 5] = [
39    ("Neutral", Tint::NONE),
40    ("Stone", Tint::new(58.071, 0.013)),
41    ("Zinc", Tint::new(285.938, 0.016)),
42    ("Gray", Tint::new(264.364, 0.027)),
43    ("Slate", Tint::new(257.417, 0.046)),
44];
45
46/// The alpha of each text role over the palette's ink — the colour of
47/// [`Theme::text`] at full opacity. The shipped palettes' ink is white in dark
48/// and black in light.
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct TextInk {
51    /// [`Theme::text`].
52    pub text: f32,
53    /// [`Theme::text_muted`].
54    pub muted: f32,
55    /// [`Theme::text_faint`].
56    pub faint: f32,
57}
58
59impl TextInk {
60    /// The role at `alpha` over `appearance`'s ink.
61    pub fn color(appearance: Appearance, alpha: f32) -> Hsla {
62        match appearance {
63            Appearance::Dark => gpui::hsla(0.0, 0.0, 1.0, alpha),
64            Appearance::Light => gpui::hsla(0.0, 0.0, 0.0, alpha),
65        }
66    }
67
68    /// Writes [`Theme::text`], [`Theme::text_muted`] and [`Theme::text_faint`]
69    /// at these alphas over the colour `theme.text` already carries.
70    pub fn paint(&self, theme: &mut Theme) {
71        let ink = theme.text;
72        theme.text = ink.alpha(self.text);
73        theme.text_muted = ink.alpha(self.muted);
74        theme.text_faint = ink.alpha(self.faint);
75    }
76}
77
78/// Text ink for both appearances.
79#[derive(Debug, Clone, Copy, PartialEq)]
80pub struct Ink {
81    pub dark: TextInk,
82    pub light: TextInk,
83}
84
85impl Ink {
86    /// AppKit's label, secondary and tertiary label colours. Muted is under
87    /// 4.5:1 on white, and faint is under 3:1 in both appearances.
88    pub const APPKIT: Self = Self {
89        dark: TextInk {
90            text: 0.85,
91            muted: 0.55,
92            faint: 0.25,
93        },
94        light: TextInk {
95            text: 0.85,
96            muted: 0.50,
97            faint: 0.26,
98        },
99    };
100
101    /// Text and muted at 4.5:1 or more and faint at 3:1 or more, on
102    /// [`Theme::bg`] and [`Theme::surface`] in both appearances.
103    pub const ACCESSIBLE: Self = Self {
104        dark: TextInk {
105            text: 0.85,
106            muted: 0.52,
107            faint: 0.40,
108        },
109        light: TextInk {
110            text: 0.85,
111            muted: 0.56,
112            faint: 0.43,
113        },
114    };
115
116    pub fn for_appearance(&self, appearance: Appearance) -> TextInk {
117        match appearance {
118            Appearance::Dark => self.dark,
119            Appearance::Light => self.light,
120        }
121    }
122}
123
124/// Whether the window composites translucent — AppKit's vibrancy, and Mica.
125///
126/// Three-valued rather than a bool because the honest answer depends on the
127/// appearance, and the appearance moves under the app: the OS switches at
128/// sunset. A bool would have to be rewritten by whoever noticed, and nothing
129/// outside this crate is told when a palette is installed.
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
131pub enum Vibrancy {
132    /// Frost where the palette is built for it, which is dark alone.
133    ///
134    /// Light's glass tokens sit near 85% opacity — see [`Theme::glass_overlay`],
135    /// [`Theme::card_glass_bg`] and [`Theme::input_glass_bg`], each tuned there
136    /// because a translucent white tint left text ghosting over whatever sat
137    /// behind it. Light pays for the blur and shows almost none of it, so it is
138    /// not asked for unless an app says so.
139    #[default]
140    Auto,
141    /// Frost in both appearances. What an app asks for when it has tuned its
142    /// own light palette to carry one.
143    On,
144    /// Opaque in both. What a Reduce-transparency switch sets.
145    Off,
146}
147
148impl Vibrancy {
149    /// Whether to composite translucent for `appearance`.
150    ///
151    /// The platform gate rides on [`Vibrancy::Auto`] rather than on the caller:
152    /// it asks for a frost only where the compositor paints one behind the
153    /// window ([`frosted_window`](crate::frosted_window)), since a window that is merely transparent
154    /// shows raw desktop through the sidebar. An app that knows its compositor
155    /// says [`Vibrancy::On`] and gets it anywhere.
156    pub fn on(self, appearance: Appearance) -> bool {
157        match self {
158            Self::Auto => crate::frosted_window() && matches!(appearance, Appearance::Dark),
159            Self::On => true,
160            Self::Off => false,
161        }
162    }
163}
164
165/// What an app changes about the shipped palette without redesigning it.
166///
167/// Installed as a gpui [`Global`]; [`Theme::install`] applies it to whatever
168/// palette is registered, so it survives a light/dark switch and composes with
169/// [`crate::AppExt::set_palette`](crate::AppExt::set_palette) rather than competing with it.
170#[derive(Debug, Clone, Copy, PartialEq)]
171pub struct Brand {
172    /// The hue every grey in the palette carries.
173    pub tint: Tint,
174    /// The emphasis hue. Left neutral, the accent follows [`Self::tint`] like
175    /// any other grey — which is what the shipped palette already is.
176    pub accent: Tint,
177    /// The base corner radius; every other corner is a ratio of it. See
178    /// [`Theme::BASE_RADIUS`].
179    pub radius: f32,
180    /// How opaque the tint over the blurred window is. See
181    /// [`Theme::VIBRANCY_ALPHA`], where it starts, and [`Theme::vibrancy_tint`].
182    pub vibrancy_alpha: f32,
183    /// The blur behind the translucent window. See [`Theme::window_blur`].
184    pub window_blur: f32,
185    /// Whether the window composites translucent, which is a question about
186    /// the appearance as much as about the app — see [`Vibrancy`].
187    pub vibrancy: Vibrancy,
188    /// Whether components paint glass. Separate from [`Self::vibrancy`]:
189    /// SwiftUI's material blends within the window, so an opaque window can
190    /// still carry glass — which is what a Reduce-transparency setting asks
191    /// for and the system's own does not do. The converse is a platform
192    /// answer: a Mica window carries no glass, since the blur a card lays over
193    /// the content it covers is [`LENSED`](crate::LENSED) and Mica is not.
194    pub glass: bool,
195    /// The text roles' alphas. [`Ink::APPKIT`] is what the palettes ship.
196    pub ink: Ink,
197}
198
199impl Global for Brand {}
200
201impl Default for Brand {
202    fn default() -> Self {
203        Self {
204            tint: Tint::NONE,
205            accent: Tint::NONE,
206            radius: Theme::BASE_RADIUS,
207            vibrancy_alpha: Theme::VIBRANCY_ALPHA,
208            window_blur: Theme::WINDOW_BLUR,
209            vibrancy: Vibrancy::Auto,
210            glass: crate::LENSED,
211            ink: Ink::APPKIT,
212        }
213    }
214}
215
216/// The accent's lightness in each appearance — indigo-400's and indigo-600's,
217/// the two steps `palettes.rs` picked so an accent clears WCAG AA on its own
218/// background rather than glowing on one and vanishing on the other.
219const ACCENT_L: (f32, f32) = (0.673, 0.511);
220
221/// The lightness of a *plate* carrying [`Theme::on_accent`], taken from
222/// `danger_strong` — the palette's existing chromatic plate, already tuned to
223/// hold a label in both appearances.
224const PLATE_L: (f32, f32) = (0.58, 0.51);
225
226impl Theme {
227    /// The shipped palette for an appearance, rotated onto a brand. What
228    /// [`Theme::install`] builds, without installing it — for previewing the
229    /// appearance you are not currently painting.
230    pub fn branded(brand: &Brand, appearance: Appearance) -> Self {
231        let mut theme = Self::for_appearance(appearance);
232        brand.apply(&mut theme);
233        theme
234    }
235}
236
237impl Brand {
238    /// Rotate a palette onto this brand's hues.
239    pub fn apply(&self, theme: &mut Theme) {
240        // The one place the appearance and the brand are both in hand, and it
241        // already runs on every install — which is every light/dark switch,
242        // from the OS or from a person. Resolving here is what keeps vibrancy
243        // right across a sunset without anyone outside watching for one.
244        theme.vibrancy = self.vibrancy.on(theme.appearance);
245        // Left where the brand put it, even when the window is opaque: every
246        // reader of the tint is behind a `vibrancy` gate already
247        // ([`Theme::window_bg`]), and an app that turns the frost back on
248        // wants the coverage it tuned, not a value that was overwritten while
249        // nothing was looking at it.
250        theme.vibrancy_alpha = self.vibrancy_alpha;
251        theme.window_blur = self.window_blur;
252        theme.glass = self.glass;
253        self.ink.for_appearance(theme.appearance).paint(theme);
254        // Every colour token, with the rule doing the choosing: a token that is
255        // already grey takes the tint, and one that already carries a hue —
256        // danger, warning, success — is semantic and keeps it. Translucent ink
257        // is skipped because it paints over whatever is beneath it, which is
258        // tinted already.
259        let tint = match theme.family {
260            None => self.tint,
261            Some(_) => Tint::NONE,
262        };
263        for (_, slot) in theme.tokens_mut() {
264            if slot.a == 1.0 && slot.s <= f32::EPSILON {
265                *slot = color::tint(*slot, tint.hue, tint.chroma);
266            }
267        }
268
269        if self.accent.chroma > 0.0 {
270            let light = theme.appearance == Appearance::Light;
271            let (accent_l, plate_l) = if light {
272                (ACCENT_L.1, PLATE_L.1)
273            } else {
274                (ACCENT_L.0, PLATE_L.0)
275            };
276            theme.accent = color::oklch(accent_l, self.accent.chroma, self.accent.hue);
277            theme.accent_strong = color::oklch(plate_l, self.accent.chroma, self.accent.hue);
278            // Whichever label the plate can actually hold. The shipped accent is
279            // the maximum-contrast neutral, where the answer is always the
280            // inverse; a chromatic plate at a yellow hue is bright enough that
281            // the inverse would be the unreadable one.
282            theme.on_accent = label_on(theme.accent_strong, theme);
283        }
284    }
285}
286
287/// Whichever of the palette's two extremes the plate can actually hold.
288fn label_on(plate: Hsla, theme: &Theme) -> Hsla {
289    let (a, b) = (theme.solid, theme.on_solid);
290    if color::contrast_ratio(plate, a) >= color::contrast_ratio(plate, b) {
291        a
292    } else {
293        b
294    }
295}
296
297/// Read the installed brand (the default before one is set).
298pub(crate) fn brand(cx: &App) -> Brand {
299    cx.try_global::<Brand>().copied().unwrap_or_default()
300}
301
302/// Install a brand and repaint every window with it.
303///
304/// Colours are read imperatively at paint time, so nothing observes the theme
305/// global — the same reason [`appearance::apply`](crate::appearance::apply)
306/// refreshes rather than notifies.
307pub(crate) fn set_brand(brand: Brand, cx: &mut App) {
308    cx.set_global(brand);
309    Theme::install(crate::paint::current_appearance(), cx);
310    // Crossing 1.0 is what puts the `NSVisualEffectView` in or takes it out,
311    // and nothing else does it — a repaint alone leaves a window that was
312    // opaque at boot opaque forever.
313    crate::appearance::reapply_window_background(cx);
314    cx.refresh_windows();
315}