Skip to main content

herogpui_theme/
semantic.rs

1//! Semantic color tokens — a faithful port of HeroUI v3's
2//! `packages/styles/themes/default/variables.css`.
3//!
4//! Every base value below is transcribed verbatim from that file in `oklch()`.
5//! Derived tokens (`*-hover`, `*-soft`, `background-secondary`,
6//! `separator-secondary`, …) are computed with the same
7//! `color-mix(in oklab, …)` weights the stylesheet uses, so a HeroGPUI theme
8//! and a HeroUI theme resolve to identical pixels.
9
10use gpui::Hsla;
11use herogpui_core::{mix_oklab, oklch, soft_mix, with_alpha};
12
13// ---------------------------------------------------------------------------
14// Base colors — identical in light and dark ("do not change between modes")
15// ---------------------------------------------------------------------------
16
17/// `--white: oklch(100% 0 0)`
18pub fn white() -> Hsla {
19    oklch(1.0, 0.0, 0.0)
20}
21/// `--black: oklch(0% 0 0)`
22pub fn black() -> Hsla {
23    oklch(0.0, 0.0, 0.0)
24}
25/// `--snow: oklch(0.9911 0 0)`
26pub fn snow() -> Hsla {
27    oklch(0.9911, 0.0, 0.0)
28}
29/// `--eclipse: oklch(0.2103 0.0059 285.89)`
30pub fn eclipse() -> Hsla {
31    oklch(0.2103, 0.0059, 285.89)
32}
33
34/// How a role resolves `--role-soft-foreground`.
35#[derive(Clone, Copy, Debug)]
36pub enum SoftForeground {
37    /// `--default-soft-foreground: var(--default-foreground)`
38    RoleForeground,
39    /// `color-mix(in oklab, var(--role) C%, var(--foreground) F%)`. CSS
40    /// normalises the weights, so the role contributes `C / (C + F)`.
41    Mix { color: f32, foreground: f32 },
42}
43
44/// A semantic color role (`accent`, `default`, `success`, `warning`, `danger`).
45///
46/// v3 removed numbered scales; a role carries only its base value and readable
47/// foreground, and every other shade is derived.
48#[derive(Clone, Copy, Debug)]
49pub struct RoleColor {
50    /// e.g. `--accent`.
51    pub color: Hsla,
52    /// e.g. `--accent-foreground`.
53    pub foreground: Hsla,
54    /// Weight of `foreground` in the `*-hover` mix. `0.10` for the status and
55    /// accent roles, `0.04` for `default`.
56    pub hover_mix: f32,
57    /// An explicit `*-hover`, in place of the `hover_mix` derivation.
58    ///
59    /// Upstream derives every hover shade by mixing the role toward its own
60    /// foreground, which moves a dark accent lighter and a light one darker.
61    /// A design system whose hover moves the other way — or to an unrelated
62    /// value — has no weight that expresses it, so it can name the colour
63    /// instead. Set through [`RoleColor::with_hover`].
64    hover_override: Option<Hsla>,
65    /// Share of the role color in `*-soft`, over transparent.
66    soft_mix: f32,
67    /// Share of the role color in `*-soft-hover`.
68    soft_hover_mix: f32,
69    /// How `*-soft-foreground` resolves.
70    soft_foreground: SoftForeground,
71    /// `[data-vibrant-palette="true"]` (variables.css:317-330): the opt-in
72    /// palette reweights every mixing role's `*-soft-foreground` to
73    /// `92%` role over `8%` page foreground, in both appearances. Roles that
74    /// resolve to [`SoftForeground::RoleForeground`] (`default`) are untouched,
75    /// exactly as upstream leaves `--default-soft-foreground` alone.
76    vibrant_palette: bool,
77}
78
79impl RoleColor {
80    pub fn new(color: Hsla, foreground: Hsla) -> Self {
81        Self {
82            color,
83            foreground,
84            hover_mix: 0.10,
85            hover_override: None,
86            soft_mix: 0.15,
87            soft_hover_mix: 0.20,
88            soft_foreground: SoftForeground::Mix {
89                color: 70.0,
90                foreground: 30.0,
91            },
92            vibrant_palette: false,
93        }
94    }
95
96    /// `default` mixes only 4% of its foreground on hover.
97    ///
98    /// Ignored once [`RoleColor::with_hover`] has named the shade outright.
99    pub fn with_hover_mix(mut self, hover_mix: f32) -> Self {
100        self.hover_mix = hover_mix;
101        self
102    }
103
104    /// Replaces the derived `*-hover` shade with an explicit colour.
105    ///
106    /// This is the role-level hook: every component that hovers this role —
107    /// a `Variant::Primary` button, a select trigger, a menu row — reads
108    /// [`RoleColor::hover`], so naming it here makes the whole surface correct
109    /// by construction rather than one call site at a time. It is the
110    /// alternative to registering a named component recipe carrying a
111    /// [`crate::ComponentColor::Literal`] and converting each site to it,
112    /// which leaves any newly written site on the derived shade.
113    ///
114    /// Leaves `*-soft-hover` alone: that shade is an alpha of the role over
115    /// transparent and does not mix the foreground in.
116    pub fn with_hover(mut self, hover: Hsla) -> Self {
117        self.hover_override = Some(hover);
118        self
119    }
120
121    /// The explicit `*-hover`, when one was named.
122    pub fn hover_override(&self) -> Option<Hsla> {
123        self.hover_override
124    }
125
126    /// Sets the shares of the role color in `--role-soft` and
127    /// `--role-soft-hover` (`default` sits at 50/60, the rest at 15/20 in
128    /// light and 12/16 in dark for the cooler roles).
129    pub fn with_soft_mix(mut self, soft: f32, soft_hover: f32) -> Self {
130        self.soft_mix = soft;
131        self.soft_hover_mix = soft_hover;
132        self
133    }
134
135    /// `--role-soft-foreground: color-mix(in oklab, var(--role) C%, var(--foreground) F%)`
136    pub fn with_soft_foreground_mix(mut self, color: f32, foreground: f32) -> Self {
137        self.soft_foreground = SoftForeground::Mix { color, foreground };
138        self
139    }
140
141    /// Opts this role into `[data-vibrant-palette="true"]`. Set through
142    /// [`crate::ThemeBuilder::vibrant_palette`], which fans the flag out over
143    /// every role at once, the way the attribute selector does.
144    pub fn with_vibrant_palette(mut self, vibrant: bool) -> Self {
145        self.vibrant_palette = vibrant;
146        self
147    }
148
149    /// Whether this role resolves its soft foreground with the vibrant weights.
150    pub fn is_vibrant_palette(&self) -> bool {
151        self.vibrant_palette
152    }
153
154    /// `--default-soft-foreground: var(--default-foreground)`
155    pub fn with_soft_foreground_role(mut self) -> Self {
156        self.soft_foreground = SoftForeground::RoleForeground;
157        self
158    }
159
160    /// `--color-accent-hover: color-mix(in oklab, var(--accent) 90%, var(--accent-foreground) 10%)`
161    ///
162    /// [`RoleColor::with_hover`] replaces this derivation with a named colour.
163    pub fn hover(&self) -> Hsla {
164        self.hover_override
165            .unwrap_or_else(|| mix_oklab(self.color, self.foreground, self.hover_mix))
166    }
167
168    /// `--color-accent-soft: color-mix(in oklab, var(--accent) 15%, transparent)`
169    pub fn soft(&self) -> Hsla {
170        soft_mix(self.color, self.soft_mix)
171    }
172
173    /// `--color-accent-soft-hover: color-mix(in oklab, var(--accent) 20%, transparent)`
174    pub fn soft_hover(&self) -> Hsla {
175        soft_mix(self.color, self.soft_hover_mix)
176    }
177
178    /// `--color-accent-soft-foreground: color-mix(in oklab, var(--accent) 70%, var(--foreground) 30%)`
179    ///
180    /// The mixing roles blend against the page foreground, so pass the live
181    /// `ThemeColors::foreground` — a `ThemeBuilder::foreground` override then
182    /// flows through without rebuilding the theme.
183    pub fn soft_foreground(&self, page_foreground: Hsla) -> Hsla {
184        match self.soft_foreground {
185            SoftForeground::RoleForeground => self.foreground,
186            // variables.css:318-321 (light) / :326-329 (dark): the vibrant
187            // palette replaces the per-role weights with one 92/8 mix.
188            SoftForeground::Mix { .. } if self.vibrant_palette => {
189                mix_oklab(self.color, page_foreground, 8.0 / 100.0)
190            }
191            SoftForeground::Mix { color, foreground } => {
192                // `mix_oklab`'s `t` is the weight of its *second* argument, so
193                // the page foreground's normalised share goes here — passing the
194                // role's share paints the text as `F%` role over `C%` ink.
195                mix_oklab(
196                    self.color,
197                    page_foreground,
198                    foreground / (color + foreground),
199                )
200            }
201        }
202    }
203
204    pub fn with_alpha(&self, alpha: f32) -> Hsla {
205        with_alpha(self.color, alpha)
206    }
207}
208
209/// A layered container color: `surface`, `overlay` or `segment`.
210#[derive(Clone, Copy, Debug)]
211pub struct SurfaceColor {
212    pub background: Hsla,
213    pub foreground: Hsla,
214}
215
216impl SurfaceColor {
217    /// `--surface-hover: color-mix(in oklab, var(--surface) 92%, var(--surface-foreground) 8%)`
218    pub fn hover(&self) -> Hsla {
219        mix_oklab(self.background, self.foreground, 0.08)
220    }
221}
222
223/// Form-field tokens. v3 keeps these separate from buttons so inputs can be
224/// styled independently.
225#[derive(Clone, Copy, Debug)]
226pub struct FieldColors {
227    /// `--field-background`
228    pub background: Hsla,
229    /// `--field-foreground`
230    pub foreground: Hsla,
231    /// `--field-placeholder`
232    pub placeholder: Hsla,
233    /// `--field-border` — `transparent` by default.
234    pub border: Hsla,
235}
236
237impl FieldColors {
238    /// `--color-field-hover: color-mix(in oklab, var(--field-background) 90%, var(--field-foreground) 2%)`
239    ///
240    /// CSS normalises the 90/2 weights, so the foreground contributes 2/92.
241    pub fn hover(&self) -> Hsla {
242        mix_oklab(self.background, self.foreground, 2.0 / 92.0)
243    }
244
245    /// `--color-field-focus: var(--field-background)`
246    pub fn focus(&self) -> Hsla {
247        self.background
248    }
249
250    /// `--field-border-hover: color-mix(in oklab, var(--field-border) 88%, var(--field-foreground) 10%)`
251    ///
252    /// The weights sum to 98, which CSS normalises, so the foreground
253    /// contributes 10/98. Invisible while `--field-border-width` is 0, and the
254    /// token exists for a caller who gives their fields a border.
255    pub fn border_hover(&self) -> Hsla {
256        mix_oklab(self.border, self.foreground, 10.0 / 98.0)
257    }
258
259    /// `--field-border-focus: color-mix(in oklab, var(--field-border) 74%, var(--field-foreground) 22%)`
260    pub fn border_focus(&self) -> Hsla {
261        mix_oklab(self.border, self.foreground, 22.0 / 96.0)
262    }
263}
264
265/// All semantic tokens of one appearance.
266#[derive(Clone, Debug)]
267pub struct ThemeColors {
268    // -- base ---------------------------------------------------------------
269    /// `--background`
270    pub background: Hsla,
271    /// `--foreground`
272    pub foreground: Hsla,
273    /// `--muted` — de-emphasised body text and icons.
274    pub muted: Hsla,
275    /// `--scrollbar: var(--scrollbar-thumb)`
276    pub scrollbar: Hsla,
277
278    // -- containers ---------------------------------------------------------
279    /// `--surface` / `--surface-foreground` — non-floating components
280    /// (cards, accordions, disclosure groups).
281    pub surface: SurfaceColor,
282    /// `--surface-secondary`
283    pub surface_secondary: Hsla,
284    /// `--surface-tertiary`
285    pub surface_tertiary: Hsla,
286    /// `--overlay` / `--overlay-foreground` — floating components
287    /// (tooltips, popovers, modals, menus).
288    pub overlay: SurfaceColor,
289    /// `--segment` / `--segment-foreground` — selected segment of a
290    /// segmented control (tabs, toggle groups).
291    pub segment: SurfaceColor,
292
293    // -- roles --------------------------------------------------------------
294    /// `--default` — the neutral backbone of the system.
295    pub default: RoleColor,
296    /// `--accent` — the brand color (v2 `primary`).
297    pub accent: RoleColor,
298    /// `--success`
299    pub success: RoleColor,
300    /// `--warning`
301    pub warning: RoleColor,
302    /// `--danger`
303    pub danger: RoleColor,
304
305    // -- fields -------------------------------------------------------------
306    pub field: FieldColors,
307
308    // -- misc ---------------------------------------------------------------
309    /// `--border`
310    pub border: Hsla,
311    /// `--separator`
312    pub separator: Hsla,
313    /// `--focus`
314    pub focus: Hsla,
315    /// `--link`
316    pub link: Hsla,
317    /// `--backdrop` — the scrim behind modals and drawers.
318    pub backdrop: Hsla,
319}
320
321impl ThemeColors {
322    /// Whether `[data-vibrant-palette="true"]` is on (variables.css:317-330).
323    ///
324    /// The flag lives on each [`RoleColor`]; the mixing roles are set together
325    /// by [`crate::ThemeBuilder::vibrant_palette`], so `accent` answers for all.
326    pub fn vibrant_palette(&self) -> bool {
327        self.accent.is_vibrant_palette()
328    }
329
330    /// Fans `[data-vibrant-palette="true"]` out over the roles the selector
331    /// lists: accent, success, warning and danger. `default` is absent from
332    /// both blocks upstream and keeps `--default-soft-foreground`.
333    pub fn set_vibrant_palette(&mut self, vibrant: bool) {
334        self.accent = self.accent.with_vibrant_palette(vibrant);
335        self.success = self.success.with_vibrant_palette(vibrant);
336        self.warning = self.warning.with_vibrant_palette(vibrant);
337        self.danger = self.danger.with_vibrant_palette(vibrant);
338    }
339
340    // -- derived backgrounds ------------------------------------------------
341
342    /// `color-mix(in oklab, var(--background) 96%, var(--foreground) 4%)`
343    pub fn background_secondary(&self) -> Hsla {
344        mix_oklab(self.background, self.foreground, 0.04)
345    }
346
347    /// `color-mix(in oklab, var(--background) 92%, var(--foreground) 8%)`
348    pub fn background_tertiary(&self) -> Hsla {
349        mix_oklab(self.background, self.foreground, 0.08)
350    }
351
352    /// `--color-background-inverse: var(--foreground)`
353    pub fn background_inverse(&self) -> Hsla {
354        self.foreground
355    }
356
357    // -- derived separators -------------------------------------------------
358
359    /// `color-mix(in oklab, var(--surface) 85%, var(--surface-foreground) 15%)`
360    pub fn separator_secondary(&self) -> Hsla {
361        mix_oklab(self.surface.background, self.surface.foreground, 0.15)
362    }
363
364    /// `color-mix(in oklab, var(--surface) 81%, var(--surface-foreground) 19%)`
365    pub fn separator_tertiary(&self) -> Hsla {
366        mix_oklab(self.surface.background, self.surface.foreground, 0.19)
367    }
368
369    // -- derived borders ----------------------------------------------------
370
371    /// `--border-secondary: color-mix(in oklab, var(--surface) 78%, var(--surface-foreground) 22%)`
372    pub fn border_secondary(&self) -> Hsla {
373        mix_oklab(self.surface.background, self.surface.foreground, 0.22)
374    }
375
376    /// `--border-tertiary: color-mix(in oklab, var(--surface) 66%, var(--surface-foreground) 34%)`
377    pub fn border_tertiary(&self) -> Hsla {
378        mix_oklab(self.surface.background, self.surface.foreground, 0.34)
379    }
380
381    /// `--surface-secondary-foreground: var(--foreground)`
382    ///
383    /// v3 gives the secondary and tertiary surfaces their own foreground
384    /// variables, both defaulting to the page's, so a caller who repaints one of
385    /// those surfaces has somewhere to put the matching text colour.
386    pub fn surface_secondary_foreground(&self) -> Hsla {
387        self.foreground
388    }
389
390    /// `--surface-tertiary-foreground: var(--foreground)`
391    pub fn surface_tertiary_foreground(&self) -> Hsla {
392        self.foreground
393    }
394
395    /// Resolves a role by its v3 token name, defaulting to `accent`.
396    pub fn role(&self, name: &str) -> &RoleColor {
397        match name {
398            "default" => &self.default,
399            "success" => &self.success,
400            "warning" => &self.warning,
401            "danger" => &self.danger,
402            _ => &self.accent,
403        }
404    }
405
406    // -- light --------------------------------------------------------------
407
408    pub fn light() -> Self {
409        let foreground = eclipse();
410        let muted = oklch(0.5517, 0.0138, 285.94);
411        let accent = RoleColor::new(oklch(0.6204, 0.195, 253.83), snow())
412            .with_soft_mix(0.15, 0.20)
413            .with_soft_foreground_mix(70.0, 30.0);
414        Self {
415            background: oklch(0.9702, 0.0, 0.0),
416            foreground,
417            muted,
418            scrollbar: with_alpha(foreground, 0.15),
419
420            surface: SurfaceColor {
421                background: white(),
422                foreground,
423            },
424            surface_secondary: oklch(0.9524, 0.0013, 286.37),
425            surface_tertiary: oklch(0.9373, 0.0013, 286.37),
426            overlay: SurfaceColor {
427                background: white(),
428                foreground,
429            },
430            segment: SurfaceColor {
431                background: white(),
432                foreground: eclipse(),
433            },
434
435            default: RoleColor::new(oklch(0.94, 0.001, 286.375), eclipse())
436                .with_hover_mix(0.04)
437                .with_soft_mix(0.50, 0.60)
438                .with_soft_foreground_role(),
439            accent,
440            success: RoleColor::new(oklch(0.7329, 0.1935, 150.81), eclipse())
441                .with_soft_foreground_mix(80.0, 60.0),
442            warning: RoleColor::new(oklch(0.7819, 0.1585, 72.33), eclipse())
443                .with_soft_foreground_mix(80.0, 70.0),
444            danger: RoleColor::new(oklch(0.6532, 0.2328, 25.74), snow())
445                .with_soft_foreground_mix(70.0, 40.0),
446
447            field: FieldColors {
448                background: white(),
449                foreground: oklch(0.2103, 0.0059, 285.89),
450                placeholder: muted,
451                border: with_alpha(black(), 0.0),
452            },
453
454            // `--border` is a step darker than `--separator`: 90% against 92%.
455            // Both had been transcribed as the separator's value.
456            border: oklch(0.9, 0.004, 286.32),
457            separator: oklch(0.92, 0.004, 286.32),
458            focus: accent.color,
459            link: foreground,
460            backdrop: with_alpha(black(), 0.5),
461        }
462    }
463
464    // -- dark ---------------------------------------------------------------
465
466    pub fn dark() -> Self {
467        let foreground = snow();
468        let muted = oklch(0.705, 0.015, 286.067);
469        let accent = RoleColor::new(oklch(0.6204, 0.195, 253.83), snow())
470            .with_soft_mix(0.12, 0.16)
471            .with_soft_foreground_mix(80.0, 30.0);
472        let default = RoleColor::new(oklch(0.274, 0.006, 286.033), snow())
473            .with_hover_mix(0.04)
474            .with_soft_mix(0.50, 0.60)
475            .with_soft_foreground_role();
476        Self {
477            background: oklch(0.12, 0.005, 285.823),
478            foreground,
479            muted,
480            scrollbar: with_alpha(foreground, 0.15),
481
482            surface: SurfaceColor {
483                background: oklch(0.2103, 0.0059, 285.89),
484                foreground,
485            },
486            surface_secondary: oklch(0.257, 0.0037, 286.14),
487            surface_tertiary: oklch(0.2721, 0.0024, 247.91),
488            // `--overlay` *is* `--surface` in dark mode. This used to lighten it
489            // "so floating panels read", which is the kind of improvement the
490            // token values are not allowed to make: a v3 dark popover is the
491            // colour of a v3 dark card, and the shadow is what separates them.
492            overlay: SurfaceColor {
493                background: oklch(0.2103, 0.0059, 285.89),
494                foreground,
495            },
496            segment: SurfaceColor {
497                background: oklch(0.3964, 0.01, 285.93),
498                foreground,
499            },
500
501            default,
502            accent,
503            // `--success` is not overridden in dark mode; only its soft shares
504            // are (12/16 over transparent, foreground at 80/30).
505            success: RoleColor::new(oklch(0.7329, 0.1935, 150.81), eclipse())
506                .with_soft_mix(0.12, 0.16)
507                .with_soft_foreground_mix(80.0, 30.0),
508            warning: RoleColor::new(oklch(0.8203, 0.1388, 76.34), eclipse())
509                .with_soft_mix(0.12, 0.16)
510                .with_soft_foreground_mix(80.0, 30.0),
511            danger: RoleColor::new(oklch(0.594, 0.1967, 24.63), snow())
512                .with_soft_foreground_mix(80.0, 30.0),
513
514            field: FieldColors {
515                // `--field-background: oklch(0.2103 0.0059 285.89)` -- the
516                // surface colour, not `--default`, which is two steps lighter.
517                background: oklch(0.2103, 0.0059, 285.89),
518                foreground,
519                placeholder: muted,
520                border: with_alpha(black(), 0.0),
521            },
522
523            // `--border: oklch(28% ..)`, `--separator: oklch(25% ..)`. Both were
524            // one value here, and both too dark.
525            border: oklch(0.28, 0.006, 286.033),
526            separator: oklch(0.25, 0.006, 286.033),
527            focus: accent.color,
528            link: foreground,
529            backdrop: with_alpha(black(), 0.6),
530        }
531    }
532}
533
534#[cfg(test)]
535mod tests {
536    use super::*;
537
538    fn rgb8(c: Hsla) -> (u8, u8, u8) {
539        let r = gpui::Rgba::from(c);
540        (
541            (r.r * 255.0).round() as u8,
542            (r.g * 255.0).round() as u8,
543            (r.b * 255.0).round() as u8,
544        )
545    }
546
547    #[test]
548    fn an_explicit_role_hover_replaces_the_derived_mix() {
549        let stock = ThemeColors::light();
550        let derived = stock.accent.hover();
551        // Upstream mixes the role toward its own foreground, so a hover named
552        // in the other direction has no `hover_mix` that expresses it.
553        let named = gpui::hsla(0.6, 0.9, 0.3, 1.0);
554        let role = stock.accent.with_hover(named);
555        assert_ne!(rgb8(derived), rgb8(named), "the fixture must differ");
556        assert_eq!(rgb8(role.hover()), rgb8(named));
557        assert_eq!(role.hover_override(), Some(named));
558        // Only `*-hover` moves: the soft shades are alphas of the role over
559        // transparent and never mix the foreground in.
560        assert_eq!(rgb8(role.soft()), rgb8(stock.accent.soft()));
561        assert_eq!(rgb8(role.soft_hover()), rgb8(stock.accent.soft_hover()));
562        assert_eq!(rgb8(role.color), rgb8(stock.accent.color));
563    }
564
565    #[test]
566    fn a_named_role_hover_outranks_the_hover_mix_whichever_order_they_are_set() {
567        let base = ThemeColors::light().accent;
568        let named = gpui::hsla(0.1, 0.8, 0.4, 1.0);
569        assert_eq!(
570            rgb8(base.with_hover(named).with_hover_mix(0.5).hover()),
571            rgb8(named)
572        );
573        assert_eq!(
574            rgb8(base.with_hover_mix(0.5).with_hover(named).hover()),
575            rgb8(named)
576        );
577    }
578
579    #[test]
580    fn accent_soft_is_fifteen_percent_accent() {
581        let c = ThemeColors::light();
582        assert!((c.accent.soft().a - 0.15).abs() < 1e-4);
583        assert!((c.accent.soft_hover().a - 0.20).abs() < 1e-4);
584    }
585
586    #[test]
587    fn light_scrollbar_thumb_is_fifteen_percent_foreground() {
588        let c = ThemeColors::light();
589        assert_eq!(c.scrollbar, with_alpha(c.foreground, 0.15));
590    }
591
592    #[test]
593    fn dark_scrollbar_thumb_is_fifteen_percent_foreground() {
594        let c = ThemeColors::dark();
595        assert_eq!(c.scrollbar, with_alpha(c.foreground, 0.15));
596    }
597
598    #[test]
599    fn the_soft_matrix_matches_the_pinned_stylesheet() {
600        // Weights transcribed from `variables.css` at v3.2.4. `None` means
601        // `--role-soft-foreground: var(--role-foreground)`; `Some` is the
602        // `color-mix(in oklab, var(--role) C%, var(--foreground) F%)` pair.
603        let light = ThemeColors::light();
604        let dark = ThemeColors::dark();
605        for (colors, role_name, soft, soft_hover, foreground) in [
606            (&light, "default", 0.50, 0.60, None),
607            (&light, "accent", 0.15, 0.20, Some((70.0, 30.0))),
608            (&light, "success", 0.15, 0.20, Some((80.0, 60.0))),
609            (&light, "warning", 0.15, 0.20, Some((80.0, 70.0))),
610            (&light, "danger", 0.15, 0.20, Some((70.0, 40.0))),
611            (&dark, "default", 0.50, 0.60, None),
612            (&dark, "accent", 0.12, 0.16, Some((80.0, 30.0))),
613            (&dark, "success", 0.12, 0.16, Some((80.0, 30.0))),
614            (&dark, "warning", 0.12, 0.16, Some((80.0, 30.0))),
615            (&dark, "danger", 0.15, 0.20, Some((80.0, 30.0))),
616        ] {
617            let role = colors.role(role_name);
618            assert!(
619                (role.soft().a - soft).abs() < 1e-4,
620                "{role_name} soft alpha"
621            );
622            assert!(
623                (role.soft_hover().a - soft_hover).abs() < 1e-4,
624                "{role_name} soft-hover alpha"
625            );
626            match foreground {
627                Some((color, page)) => assert_eq!(
628                    role.soft_foreground(colors.foreground),
629                    mix_oklab(role.color, colors.foreground, page / (color + page)),
630                    "{role_name} soft-foreground mix"
631                ),
632                None => assert_eq!(
633                    role.soft_foreground(colors.foreground),
634                    role.foreground,
635                    "{role_name} soft-foreground follows the role"
636                ),
637            }
638        }
639    }
640
641    /// Every semantic token, resolved the way a browser resolves it on
642    /// heroui.com at v3.2.4 — light and dark. A weight-for-weight comparison
643    /// against the same formula cannot catch a formula that is wrong, so these
644    /// are the pixels themselves.
645    #[test]
646    fn every_token_resolves_to_the_stylesheets_own_pixels() {
647        type Pick = fn(&ThemeColors) -> Hsla;
648        /// `(name, accessor, light sRGB+alpha, dark sRGB+alpha)`.
649        type Row = (&'static str, Pick, (u8, u8, u8, f32), (u8, u8, u8, f32));
650        #[rustfmt::skip]
651        let table: &[Row] = &[
652            ("accent", |c| c.accent.color, (4, 133, 247, 1.00), (4, 133, 247, 1.00)),
653            ("accent_foreground", |c| c.accent.foreground, (252, 252, 252, 1.00), (252, 252, 252, 1.00)),
654            ("accent_hover", |c| c.accent.hover(), (53, 146, 249, 1.00), (53, 146, 249, 1.00)),
655            ("accent_soft", |c| c.accent.soft(), (4, 133, 247, 0.15), (4, 133, 247, 0.12)),
656            ("accent_soft_foreground", |c| c.accent.soft_foreground(c.foreground), (30, 99, 174, 1.00), (97, 168, 251, 1.00)),
657            ("backdrop", |c| c.backdrop, (0, 0, 0, 0.50), (0, 0, 0, 0.60)),
658            ("background", |c| c.background, (245, 245, 245, 1.00), (6, 6, 7, 1.00)),
659            ("background_inverse", |c| c.background_inverse(), (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
660            ("background_secondary", |c| c.background_secondary(), (235, 235, 235, 1.00), (12, 12, 14, 1.00)),
661            ("background_tertiary", |c| c.background_tertiary(), (225, 225, 225, 1.00), (19, 19, 22, 1.00)),
662            ("border", |c| c.border, (221, 222, 224, 1.00), (40, 40, 44, 1.00)),
663            ("border_secondary", |c| c.border_secondary(), (198, 198, 199, 1.00), (67, 67, 69, 1.00)),
664            ("border_tertiary", |c| c.border_tertiary(), (168, 168, 169, 1.00), (92, 92, 95, 1.00)),
665            ("danger", |c| c.danger.color, (255, 56, 60, 1.00), (219, 59, 62, 1.00)),
666            ("danger_foreground", |c| c.danger.foreground, (252, 252, 252, 1.00), (252, 252, 252, 1.00)),
667            ("danger_hover", |c| c.danger.hover(), (255, 85, 81, 1.00), (225, 84, 81, 1.00)),
668            ("danger_soft", |c| c.danger.soft(), (255, 56, 60, 0.15), (219, 59, 62, 0.15)),
669            ("danger_soft_foreground", |c| c.danger.soft_foreground(c.foreground), (164, 53, 51, 1.00), (235, 120, 114, 1.00)),
670            ("default", |c| c.default.color, (235, 235, 236, 1.00), (39, 39, 42, 1.00)),
671            ("default_foreground", |c| c.default.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
672            ("default_hover", |c| c.default.hover(), (225, 225, 226, 1.00), (46, 46, 49, 1.00)),
673            ("default_soft", |c| c.default.soft(), (235, 235, 235, 0.50), (39, 39, 42, 0.50)),
674            ("default_soft_foreground", |c| c.default.soft_foreground(c.foreground), (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
675            ("field_background", |c| c.field.background, (255, 255, 255, 1.00), (24, 24, 27, 1.00)),
676            ("field_foreground", |c| c.field.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
677            ("field_placeholder", |c| c.field.placeholder, (113, 113, 122, 1.00), (159, 159, 169, 1.00)),
678            ("focus", |c| c.focus, (4, 133, 247, 1.00), (4, 133, 247, 1.00)),
679            ("foreground", |c| c.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
680            ("link", |c| c.link, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
681            ("muted", |c| c.muted, (113, 113, 122, 1.00), (159, 159, 169, 1.00)),
682            ("overlay", |c| c.overlay.background, (255, 255, 255, 1.00), (24, 24, 27, 1.00)),
683            ("overlay_foreground", |c| c.overlay.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
684            ("scrollbar", |c| c.scrollbar, (24, 24, 27, 0.15), (255, 255, 255, 0.15)),
685            ("segment", |c| c.segment.background, (255, 255, 255, 1.00), (70, 70, 76, 1.00)),
686            ("segment_foreground", |c| c.segment.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
687            ("separator", |c| c.separator, (228, 228, 231, 1.00), (33, 33, 36, 1.00)),
688            ("separator_secondary", |c| c.separator_secondary(), (216, 216, 216, 1.00), (52, 52, 55, 1.00)),
689            ("separator_tertiary", |c| c.separator_tertiary(), (205, 205, 206, 1.00), (60, 60, 63, 1.00)),
690            ("success", |c| c.success.color, (23, 201, 100, 1.00), (23, 201, 100, 1.00)),
691            ("success_foreground", |c| c.success.foreground, (24, 24, 27, 1.00), (24, 24, 27, 1.00)),
692            ("success_hover", |c| c.success.hover(), (33, 181, 93, 1.00), (33, 181, 93, 1.00)),
693            ("success_soft", |c| c.success.soft(), (23, 201, 100, 0.15), (23, 201, 100, 0.12)),
694            ("success_soft_foreground", |c| c.success.soft_foreground(c.foreground), (43, 119, 69, 1.00), (116, 216, 143, 1.00)),
695            ("surface", |c| c.surface.background, (255, 255, 255, 1.00), (24, 24, 27, 1.00)),
696            ("surface_foreground", |c| c.surface.foreground, (24, 24, 27, 1.00), (252, 252, 252, 1.00)),
697            ("surface_hover", |c| c.surface.hover(), (234, 234, 234, 1.00), (39, 39, 42, 1.00)),
698            ("surface_secondary", |c| c.surface_secondary, (239, 239, 240, 1.00), (35, 35, 37, 1.00)),
699            ("surface_tertiary", |c| c.surface_tertiary, (234, 234, 235, 1.00), (38, 39, 40, 1.00)),
700            ("warning", |c| c.warning.color, (245, 165, 36, 1.00), (247, 183, 80, 1.00)),
701            ("warning_foreground", |c| c.warning.foreground, (24, 24, 27, 1.00), (24, 24, 27, 1.00)),
702            ("warning_hover", |c| c.warning.hover(), (220, 150, 42, 1.00), (222, 165, 76, 1.00)),
703            ("warning_soft", |c| c.warning.soft(), (245, 165, 36, 0.15), (247, 183, 80, 0.12)),
704            ("warning_soft_foreground", |c| c.warning.soft_foreground(c.foreground), (133, 95, 46, 1.00), (249, 203, 134, 1.00)),
705        ];
706        for (name, pick, light, dark) in table {
707            for (mode, c, want) in [
708                ("light", ThemeColors::light(), light),
709                ("dark", ThemeColors::dark(), dark),
710            ] {
711                let got = pick(&c);
712                let (r, g, b) = rgb8(got);
713                // The reference was read back from a premultiplied surface, so
714                // a translucent token's channels carry `1/alpha` of rounding.
715                let slack = (2.0 / want.3).ceil() as i32;
716                let near = |a: u8, b: u8| (a as i32 - b as i32).abs() <= slack;
717                assert!(
718                    near(r, want.0) && near(g, want.1) && near(b, want.2)
719                        && (got.a - want.3).abs() < 0.011,
720                    "{name} ({mode}) resolved to ({r}, {g}, {b}, {:.3}),                      upstream resolves ({}, {}, {}, {:.3})",
721                    got.a, want.0, want.1, want.2, want.3
722                );
723            }
724        }
725    }
726
727    #[test]
728    fn soft_foreground_resolves_to_the_stylesheets_own_pixels() {
729        // The weights alone cannot catch an inverted mix, so pin what a browser
730        // resolves `--color-<role>-soft-foreground` to at v3.2.4 in light mode.
731        // A soft label reads as a tinted version of its role, not as body ink.
732        let c = ThemeColors::light();
733        for (role_name, expected) in [
734            ("accent", (30, 99, 174)),
735            ("success", (43, 119, 69)),
736            ("warning", (133, 95, 46)),
737            ("danger", (164, 53, 51)),
738        ] {
739            let got = rgb8(c.role(role_name).soft_foreground(c.foreground));
740            let near = |a: u8, b: u8| (a as i32 - b as i32).abs() <= 2;
741            assert!(
742                near(got.0, expected.0) && near(got.1, expected.1) && near(got.2, expected.2),
743                "{role_name} soft-foreground was {got:?}, expected {expected:?}"
744            );
745        }
746    }
747
748    #[test]
749    fn a_custom_foreground_stays_live_in_the_soft_foreground_mix() {
750        let base = ThemeColors::light();
751        let custom = ThemeColors {
752            foreground: oklch(0.30, 0.05, 120.0),
753            ..base
754        };
755        // The mixing roles resolve against the page foreground passed in, so a
756        // `ThemeBuilder::foreground` override flows through at render time.
757        assert_eq!(
758            custom.accent.soft_foreground(custom.foreground),
759            mix_oklab(custom.accent.color, custom.foreground, 0.30)
760        );
761        assert_ne!(
762            custom.accent.soft_foreground(custom.foreground),
763            base.accent.soft_foreground(base.foreground)
764        );
765        // `--default-soft-foreground: var(--default-foreground)` does not mix,
766        // so it follows the role's own foreground instead of the page's.
767        assert_eq!(
768            custom.default.soft_foreground(custom.foreground),
769            custom.default.foreground
770        );
771    }
772
773    #[test]
774    fn the_derived_borders_step_away_from_the_surface() {
775        let c = ThemeColors::light();
776        // `--border-secondary` and `--border-tertiary` mix further from the
777        // surface than `--separator-secondary` does, so a border reads stronger
778        // than a rule at the same step.
779        let steps = [
780            c.separator_secondary(),
781            c.border_secondary(),
782            c.border_tertiary(),
783        ];
784        for pair in steps.windows(2) {
785            assert!(
786                pair[1].l < pair[0].l,
787                "each step is darker than the last on a light surface"
788            );
789        }
790    }
791
792    #[test]
793    fn a_secondary_surface_keeps_the_page_foreground() {
794        let c = ThemeColors::light();
795        assert_eq!(c.surface_secondary_foreground(), c.foreground);
796        assert_eq!(c.surface_tertiary_foreground(), c.foreground);
797    }
798
799    #[test]
800    fn a_field_border_mixes_toward_its_own_foreground() {
801        let c = ThemeColors::light();
802        // Both are mixes of `--field-border` toward `--field-foreground`, and
803        // focus mixes further than hover.
804        let border = c.field.border;
805        assert_ne!(c.field.border_hover(), border);
806        assert_ne!(c.field.border_focus(), c.field.border_hover());
807    }
808
809    #[test]
810    fn field_focus_matches_field_background() {
811        let c = ThemeColors::light();
812        assert_eq!(c.field.focus(), c.field.background);
813    }
814
815    #[test]
816    #[ignore = "v3 gives dark mode one colour for both; the shadow separates them"]
817    fn dark_surface_is_darker_than_overlay() {
818        let c = ThemeColors::dark();
819        assert!(c.surface.background.l < c.overlay.background.l);
820    }
821
822    #[test]
823    fn light_background_is_lighter_than_its_derived_levels() {
824        let c = ThemeColors::light();
825        assert!(c.background.l > c.background_secondary().l);
826        assert!(c.background_secondary().l > c.background_tertiary().l);
827    }
828}