Skip to main content

euv_ui/theme/token/
impl.rs

1use super::*;
2
3/// Compact 2-byte theme key helpers resolving directly into `THEME_TOKENS_LUT`.
4impl ThemeKey {
5    /// Constructs a 2-byte `ThemeKey` from `scheme` and `mode`.
6    ///
7    /// # Arguments
8    ///
9    /// - `DesignScheme` - Target design language.
10    /// - `ThemeMode` - Light or Dark appearance.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - Compact 2-byte theme key.
15    #[inline(always)]
16    #[must_use]
17    pub const fn new(scheme: DesignScheme, mode: ThemeMode) -> Self {
18        Self { scheme, mode }
19    }
20
21    /// Extracts a 2-byte `ThemeKey` from an existing `ThemeTokens` bundle.
22    ///
23    /// # Arguments
24    ///
25    /// - `&ThemeTokens` - Source theme tokens.
26    ///
27    /// # Returns
28    ///
29    /// - `Self` - Matching 2-byte theme key.
30    #[inline(always)]
31    #[must_use]
32    pub const fn from_tokens(tokens: &ThemeTokens) -> Self {
33        Self {
34            scheme: tokens.scheme,
35            mode: tokens.mode,
36        }
37    }
38
39    /// Returns the canonical `0..34` index into `THEME_TOKENS_LUT`.
40    ///
41    /// # Arguments
42    ///
43    /// - `Self` - Compact 2-byte theme key.
44    ///
45    /// # Returns
46    ///
47    /// - `usize` - Lookup table slot index in `0..34`.
48    #[inline(always)]
49    #[must_use]
50    pub const fn lut_index(self) -> usize {
51        let Self { scheme, mode } = self;
52        (ThemeTokens::scheme_index(scheme) << 1) | ThemeTokens::mode_index(mode)
53    }
54
55    /// Reconstructs a `ThemeKey` from its canonical `0..34` lookup index.
56    ///
57    /// # Arguments
58    ///
59    /// - `usize` - Lookup table slot index produced by `lut_index`.
60    ///
61    /// # Returns
62    ///
63    /// - `Option<Self>` - Matching key, or `None` when `index` is out of range.
64    #[must_use]
65    pub const fn from_lut_index(index: usize) -> Option<Self> {
66        let schemes: [DesignScheme; 17] = DesignScheme::all();
67        let scheme_idx: usize = index >> 1;
68        if scheme_idx >= schemes.len() {
69            return None;
70        }
71        let mode: ThemeMode = if (index & 1) == 0 {
72            ThemeMode::Light
73        } else {
74            ThemeMode::Dark
75        };
76        Some(Self {
77            scheme: schemes[scheme_idx],
78            mode,
79        })
80    }
81
82    /// Resolves a zero-copy `'static` reference into `THEME_TOKENS_LUT` in O(1).
83    ///
84    /// # Arguments
85    ///
86    /// - `Self` - Compact 2-byte theme key.
87    ///
88    /// # Returns
89    ///
90    /// - `&'static ThemeTokens` - Borrowed static token bundle.
91    #[inline(always)]
92    #[must_use]
93    pub fn resolve_static(self) -> &'static ThemeTokens {
94        ThemeTokens::resolve_static(self)
95    }
96
97    /// Copies the matching `ThemeTokens` bundle from `THEME_TOKENS_LUT` in O(1).
98    ///
99    /// # Arguments
100    ///
101    /// - `Self` - Compact 2-byte theme key.
102    ///
103    /// # Returns
104    ///
105    /// - `ThemeTokens` - Compiled token bundle.
106    #[inline(always)]
107    #[must_use]
108    pub fn resolve(self) -> ThemeTokens {
109        *ThemeTokens::resolve_static(self)
110    }
111}
112
113/// Default theme tokens resolving from the active compile-time feature scheme in Light mode.
114impl Default for ThemeTokens {
115    /// Creates default `ThemeTokens`.
116    ///
117    /// # Returns
118    ///
119    /// - `Self` - Resolved design tokens.
120    #[inline(always)]
121    fn default() -> Self {
122        Self::resolve(active_feature_scheme(), ThemeMode::Light)
123    }
124}
125
126/// Complete 17-scheme x 2-mode deterministic token resolver backed by compile-time LUTs.
127impl ThemeTokens {
128    /// Maps a `DesignScheme` variant to its canonical `0..17` LUT index.
129    ///
130    /// # Arguments
131    ///
132    /// - `DesignScheme` - Target design language.
133    ///
134    /// # Returns
135    ///
136    /// - `usize` - Canonical array index in `0..17`.
137    #[inline(always)]
138    const fn scheme_index(scheme: DesignScheme) -> usize {
139        match scheme {
140            DesignScheme::Shadcn => 0,
141            DesignScheme::Cupertino => 1,
142            DesignScheme::Material => 2,
143            DesignScheme::Fluent => 3,
144            DesignScheme::AntDesign => 4,
145            DesignScheme::Bootstrap => 5,
146            DesignScheme::DaisyUi => 6,
147            DesignScheme::Geist => 7,
148            DesignScheme::HeroUi => 8,
149            DesignScheme::Linear => 9,
150            DesignScheme::Mantine => 10,
151            DesignScheme::Monochrome => 11,
152            DesignScheme::Notion => 12,
153            DesignScheme::Park => 13,
154            DesignScheme::Radix => 14,
155            DesignScheme::Stripe => 15,
156            DesignScheme::Supabase => 16,
157        }
158    }
159
160    /// Maps a `ThemeMode` variant to its binary LUT offset (`0` = Light, `1` = Dark).
161    ///
162    /// # Arguments
163    ///
164    /// - `ThemeMode` - Light or Dark appearance.
165    ///
166    /// # Returns
167    ///
168    /// - `usize` - Mode bit (`0` or `1`).
169    #[inline(always)]
170    const fn mode_index(mode: ThemeMode) -> usize {
171        match mode {
172            ThemeMode::Light => 0,
173            ThemeMode::Dark => 1,
174        }
175    }
176
177    /// Computes a `ThemePalette` at compile time for one `(DesignScheme, ThemeMode)` pair.
178    ///
179    /// # Arguments
180    ///
181    /// - `DesignScheme` - Target design language.
182    /// - `ThemeMode` - Light or Dark appearance.
183    ///
184    /// # Returns
185    ///
186    /// - `ThemePalette` - Resolved semantic color palette.
187    const fn build_palette(scheme: DesignScheme, mode: ThemeMode) -> ThemePalette {
188        let (accent_light_hex, accent_dark_hex): (u32, u32) = match scheme {
189            DesignScheme::Shadcn => (0x18181B, 0xFAFAFA),
190            DesignScheme::Cupertino => (0x0066D6, 0x0A84FF),
191            DesignScheme::Material => (0x6750A4, 0xD0BCFF),
192            DesignScheme::Fluent => (0x0067C0, 0x60CDFF),
193            DesignScheme::AntDesign => (0x1266E3, 0x3C89E8),
194            DesignScheme::Bootstrap => (0x0B5ED7, 0x3D8BFD),
195            DesignScheme::DaisyUi => (0x570DF8, 0xA78BFA),
196            DesignScheme::Geist => (0x000000, 0xFFFFFF),
197            DesignScheme::HeroUi => (0x0063D8, 0x338EF7),
198            DesignScheme::Linear => (0x4F5BC4, 0x828FFF),
199            DesignScheme::Mantine => (0x1971C2, 0x339AF0),
200            DesignScheme::Monochrome => (0x111111, 0xF5F5F5),
201            DesignScheme::Notion => (0x2F3437, 0xE8E8E8),
202            DesignScheme::Park => (0x334155, 0xE2E8F0),
203            DesignScheme::Radix => (0x3E63DD, 0x6E8BFF),
204            DesignScheme::Stripe => (0x554CE8, 0x8A84FF),
205            DesignScheme::Supabase => (0x047857, 0x3ECF8E),
206        };
207        let is_dark: bool = matches!(mode, ThemeMode::Dark);
208        let primary_hex: u32 = if is_dark {
209            accent_dark_hex
210        } else {
211            accent_light_hex
212        };
213        let primary: Color = Color::hex(primary_hex);
214        let primary_foreground: Color = if contrast_ratio_hex(0xFFFFFF, primary_hex)
215            >= contrast_ratio_hex(0x09090B, primary_hex)
216        {
217            Color::hex(0xFFFFFF)
218        } else {
219            Color::hex(0x09090B)
220        };
221        let (
222            light_bg,
223            light_card,
224            light_muted,
225            light_border,
226            dark_bg,
227            dark_card,
228            dark_muted,
229            dark_border,
230        ): (Color, Color, Color, Color, Color, Color, Color, Color) = match scheme {
231            DesignScheme::Shadcn => (
232                Color::hex(0xFAFAFA),
233                Color::hex(0xFFFFFF),
234                Color::hex(0xF4F4F5),
235                Color::hex(0xE4E4E7),
236                Color::hex(0x09090B),
237                Color::hex(0x111113),
238                Color::hex(0x18181B),
239                Color::hex(0x27272A),
240            ),
241            DesignScheme::Cupertino => (
242                Color::hex(0xF2F2F7),
243                Color::hex(0xFFFFFF),
244                Color::hex(0xE5E5EA),
245                Color::hex(0xD1D1D6),
246                Color::hex(0x000000),
247                Color::hex(0x1C1C1E),
248                Color::hex(0x2C2C2E),
249                Color::hex(0x38383A),
250            ),
251            DesignScheme::Material => (
252                Color::hex(0xFEF7FF),
253                Color::hex(0xFFFFFF),
254                Color::hex(0xF3EDF7),
255                Color::hex(0xCAC4D0),
256                Color::hex(0x141218),
257                Color::hex(0x1D1B20),
258                Color::hex(0x2B2930),
259                Color::hex(0x49454F),
260            ),
261            DesignScheme::Fluent => (
262                Color::hex(0xF3F3F3),
263                Color::hex(0xFFFFFF),
264                Color::hex(0xEAEAEA),
265                Color::hex(0xD9D9D9),
266                Color::hex(0x202020),
267                Color::hex(0x2B2B2B),
268                Color::hex(0x323232),
269                Color::hex(0x404040),
270            ),
271            DesignScheme::AntDesign => (
272                Color::hex(0xF5F5F5),
273                Color::hex(0xFFFFFF),
274                Color::hex(0xF0F0F0),
275                Color::hex(0xD9D9D9),
276                Color::hex(0x0A0A0A),
277                Color::hex(0x141414),
278                Color::hex(0x1F1F1F),
279                Color::hex(0x303030),
280            ),
281            DesignScheme::Bootstrap => (
282                Color::hex(0xF8F9FA),
283                Color::hex(0xFFFFFF),
284                Color::hex(0xE9ECEF),
285                Color::hex(0xDEE2E6),
286                Color::hex(0x121417),
287                Color::hex(0x1B1F23),
288                Color::hex(0x252A30),
289                Color::hex(0x343A40),
290            ),
291            DesignScheme::DaisyUi => (
292                Color::hex(0xF7F7FA),
293                Color::hex(0xFFFFFF),
294                Color::hex(0xEEEDF5),
295                Color::hex(0xDCD9E8),
296                Color::hex(0x11131A),
297                Color::hex(0x191D26),
298                Color::hex(0x222736),
299                Color::hex(0x32394D),
300            ),
301            DesignScheme::Geist => (
302                Color::hex(0xFAFAFA),
303                Color::hex(0xFFFFFF),
304                Color::hex(0xF2F2F2),
305                Color::hex(0xEAEAEA),
306                Color::hex(0x000000),
307                Color::hex(0x0A0A0A),
308                Color::hex(0x171717),
309                Color::hex(0x262626),
310            ),
311            DesignScheme::HeroUi => (
312                Color::hex(0xF8FAFC),
313                Color::hex(0xFFFFFF),
314                Color::hex(0xF1F5F9),
315                Color::hex(0xE2E8F0),
316                Color::hex(0x070B14),
317                Color::hex(0x0F172A),
318                Color::hex(0x1E293B),
319                Color::hex(0x334155),
320            ),
321            DesignScheme::Linear => (
322                Color::hex(0xF7F8FA),
323                Color::hex(0xFFFFFF),
324                Color::hex(0xEEF0F4),
325                Color::hex(0xDFE3EA),
326                Color::hex(0x0B0C10),
327                Color::hex(0x13151C),
328                Color::hex(0x1C1F29),
329                Color::hex(0x2A2E3D),
330            ),
331            DesignScheme::Mantine => (
332                Color::hex(0xF8F9FA),
333                Color::hex(0xFFFFFF),
334                Color::hex(0xF1F3F5),
335                Color::hex(0xDEE2E6),
336                Color::hex(0x141517),
337                Color::hex(0x1A1B1E),
338                Color::hex(0x25262B),
339                Color::hex(0x373A40),
340            ),
341            DesignScheme::Monochrome => (
342                Color::hex(0xF7F7F7),
343                Color::hex(0xFFFFFF),
344                Color::hex(0xEDEDED),
345                Color::hex(0xD4D4D4),
346                Color::hex(0x0A0A0A),
347                Color::hex(0x141414),
348                Color::hex(0x1F1F1F),
349                Color::hex(0x333333),
350            ),
351            DesignScheme::Notion => (
352                Color::hex(0xFBFBF9),
353                Color::hex(0xFFFFFF),
354                Color::hex(0xF1F1EE),
355                Color::hex(0xE3E2DF),
356                Color::hex(0x191919),
357                Color::hex(0x202020),
358                Color::hex(0x2A2A2A),
359                Color::hex(0x373737),
360            ),
361            DesignScheme::Park => (
362                Color::hex(0xF8FAFC),
363                Color::hex(0xFFFFFF),
364                Color::hex(0xF1F5F9),
365                Color::hex(0xCBD5E1),
366                Color::hex(0x0B1120),
367                Color::hex(0x111827),
368                Color::hex(0x1E293B),
369                Color::hex(0x334155),
370            ),
371            DesignScheme::Radix => (
372                Color::hex(0xF9FAFB),
373                Color::hex(0xFFFFFF),
374                Color::hex(0xF0F2F7),
375                Color::hex(0xDCE1EB),
376                Color::hex(0x111113),
377                Color::hex(0x18191B),
378                Color::hex(0x212225),
379                Color::hex(0x303238),
380            ),
381            DesignScheme::Stripe => (
382                Color::hex(0xF6F9FC),
383                Color::hex(0xFFFFFF),
384                Color::hex(0xEBF1F6),
385                Color::hex(0xD8E2EC),
386                Color::hex(0x0A1628),
387                Color::hex(0x10213A),
388                Color::hex(0x172D4D),
389                Color::hex(0x254066),
390            ),
391            DesignScheme::Supabase => (
392                Color::hex(0xF8FAFC),
393                Color::hex(0xFFFFFF),
394                Color::hex(0xF1F5F4),
395                Color::hex(0xDDE5E2),
396                Color::hex(0x121212),
397                Color::hex(0x1C1C1C),
398                Color::hex(0x262626),
399                Color::hex(0x333333),
400            ),
401        };
402        if is_dark {
403            ThemePalette {
404                background: dark_bg,
405                foreground: Color::hex(0xFAFAFA),
406                card: dark_card,
407                card_foreground: Color::hex(0xFAFAFA),
408                primary,
409                primary_foreground,
410                secondary: dark_muted,
411                secondary_foreground: Color::hex(0xFAFAFA),
412                muted: dark_muted,
413                muted_foreground: Color::hex(0xA1A1AA),
414                accent: mix_rgb_const(dark_muted, dark_border, 176),
415                destructive: Color::hex(0xDC2626),
416                destructive_foreground: Color::hex(0xFAFAFA),
417                success: Color::hex(0x22C55E),
418                success_foreground: Color::hex(0x052E16),
419                warning: Color::hex(0xF59E0B),
420                warning_foreground: Color::hex(0x09090B),
421                switch_track_off: Color::hex(0x3F3F46),
422                switch_thumb: Color::hex(0xFAFAFA),
423                media_surface: Color::hex(0x0B1120),
424                border: dark_border,
425                ring: primary,
426                overlay: Color::rgba(0, 0, 0, 184),
427            }
428        } else {
429            ThemePalette {
430                background: light_bg,
431                foreground: Color::hex(0x09090B),
432                card: light_card,
433                card_foreground: Color::hex(0x09090B),
434                primary,
435                primary_foreground,
436                secondary: light_muted,
437                secondary_foreground: Color::hex(0x18181B),
438                muted: light_muted,
439                muted_foreground: Color::hex(0x71717A),
440                accent: mix_rgb_const(light_muted, light_border, 152),
441                destructive: Color::hex(0xDC2626),
442                destructive_foreground: Color::hex(0xFFFFFF),
443                success: Color::hex(0x15803D),
444                success_foreground: Color::hex(0xFFFFFF),
445                warning: Color::hex(0xD97706),
446                warning_foreground: Color::hex(0x09090B),
447                switch_track_off: Color::hex(0x8E8E93),
448                switch_thumb: Color::hex(0xFFFFFF),
449                media_surface: Color::hex(0x0F172A),
450                border: light_border,
451                ring: primary,
452                overlay: Color::rgba(0, 0, 0, 128),
453            }
454        }
455    }
456
457    /// Precomputes all 34 `(DesignScheme, ThemeMode)` semantic color palettes into a compile-time lookup table.
458    ///
459    /// # Returns
460    ///
461    /// - `[ThemePalette; 34]` - Compile-time palette LUT indexed by `(scheme_idx << 1) | dark_idx`.
462    pub(crate) const fn build_palette_lut() -> [ThemePalette; 34] {
463        let schemes: [DesignScheme; 17] = DesignScheme::all();
464        let placeholder: ThemePalette = Self::build_palette(DesignScheme::Shadcn, ThemeMode::Light);
465        let mut lut: [ThemePalette; 34] = [placeholder; 34];
466        let mut idx: usize = 0;
467        while idx < schemes.len() {
468            let scheme: DesignScheme = schemes[idx];
469            lut[idx << 1] = Self::build_palette(scheme, ThemeMode::Light);
470            lut[(idx << 1) | 1] = Self::build_palette(scheme, ThemeMode::Dark);
471            idx += 1;
472        }
473        lut
474    }
475
476    /// Builds one complete `ThemeTokens` entry from compile-time palette and geometry LUTs.
477    ///
478    /// # Arguments
479    ///
480    /// - `DesignScheme` - Target design language.
481    /// - `ThemeMode` - Light or Dark appearance.
482    ///
483    /// # Returns
484    ///
485    /// - `Self` - Resolved token bundle.
486    fn build_tokens(scheme: DesignScheme, mode: ThemeMode) -> Self {
487        let scheme_idx: usize = Self::scheme_index(scheme);
488        let dark_idx: usize = Self::mode_index(mode);
489        let palette: ThemePalette = THEME_PALETTE_LUT[(scheme_idx << 1) | dark_idx];
490        let [
491            radius_sm,
492            radius_md,
493            radius_lg,
494            radius_xl,
495            control_sm,
496            control_md,
497            control_lg,
498        ]: [f32; 7] = THEME_CANONICAL_GEOMETRY_METRICS;
499        let (shadow_xs, shadow_sm, shadow_md, shadow_lg): (Shadow, Shadow, Shadow, Shadow) =
500            if dark_idx != 0 {
501                (
502                    Shadow::new(0.0, 1.0, 2.0, 0.0, Color::rgba(0, 0, 0, 48)),
503                    Shadow::new(0.0, 2.0, 6.0, 0.0, Color::rgba(0, 0, 0, 64)),
504                    Shadow::new(0.0, 6.0, 16.0, 0.0, Color::rgba(0, 0, 0, 84)),
505                    Shadow::new(0.0, 12.0, 26.0, 0.0, Color::rgba(0, 0, 0, 108)),
506                )
507            } else {
508                (
509                    Shadow::new(0.0, 1.0, 2.0, 0.0, Color::rgba(0, 0, 0, 12)),
510                    Shadow::new(0.0, 1.0, 3.0, 0.0, Color::rgba(0, 0, 0, 18)),
511                    Shadow::new(0.0, 4.0, 12.0, 0.0, Color::rgba(0, 0, 0, 24)),
512                    Shadow::new(0.0, 10.0, 20.0, 0.0, Color::rgba(0, 0, 0, 32)),
513                )
514            };
515        let geometry: ThemeGeometry = ThemeGeometry {
516            radius_sm,
517            radius_md,
518            radius_lg,
519            radius_xl,
520            border_width: 1.0,
521            ring_width: 2.0,
522            control_sm,
523            control_md,
524            control_lg,
525            font_body: 14.0,
526            font_heading: 16.0,
527            font_title: 24.0,
528            shadow_xs,
529            shadow_sm,
530            shadow_md,
531            shadow_lg,
532        };
533        Self {
534            scheme,
535            mode,
536            palette,
537            geometry,
538        }
539    }
540
541    /// Populates all 34 `(DesignScheme, ThemeMode)` complete `ThemeTokens` entries into the static lookup table.
542    ///
543    /// # Returns
544    ///
545    /// - `[Self; 34]` - Static tokens LUT indexed by `(scheme_idx << 1) | dark_idx`.
546    pub(crate) fn build_tokens_lut() -> [Self; 34] {
547        let schemes: [DesignScheme; 17] = DesignScheme::all();
548        let placeholder: Self = Self::build_tokens(DesignScheme::Shadcn, ThemeMode::Light);
549        let mut lut: [Self; 34] = [placeholder; 34];
550        for (idx, scheme) in schemes.into_iter().enumerate() {
551            lut[idx << 1] = Self::build_tokens(scheme, ThemeMode::Light);
552            lut[(idx << 1) | 1] = Self::build_tokens(scheme, ThemeMode::Dark);
553        }
554        lut
555    }
556
557    /// Resolves a zero-copy `'static` reference to the `ThemeTokens` entry for `key` in O(1).
558    ///
559    /// # Arguments
560    ///
561    /// - `ThemeKey` - Compact 2-byte `(DesignScheme, ThemeMode)` lookup key.
562    ///
563    /// # Returns
564    ///
565    /// - `&'static Self` - Static reference into `THEME_TOKENS_LUT`.
566    #[inline(always)]
567    #[must_use]
568    pub fn resolve_static(key: ThemeKey) -> &'static Self {
569        &THEME_TOKENS_LUT.get_or_init(Self::build_tokens_lut)[key.lut_index()]
570    }
571
572    /// Returns the compact 2-byte `ThemeKey` identifying this token bundle.
573    ///
574    /// # Arguments
575    ///
576    /// - `&Self` - Active theme tokens.
577    ///
578    /// # Returns
579    ///
580    /// - `ThemeKey` - Compact 2-byte `(scheme, mode)` key.
581    #[inline(always)]
582    #[must_use]
583    pub const fn key(&self) -> ThemeKey {
584        ThemeKey::from_tokens(self)
585    }
586
587    /// Resolves complete `ThemeTokens` for any `(DesignScheme, ThemeMode)` pair via O(1) static LUT lookup.
588    ///
589    /// # Arguments
590    ///
591    /// - `DesignScheme` - Target design language.
592    /// - `ThemeMode` - Light or Dark appearance.
593    ///
594    /// # Returns
595    ///
596    /// - `Self` - Compiled zero-allocation token bundle.
597    #[inline(always)]
598    #[must_use]
599    pub fn resolve(scheme: DesignScheme, mode: ThemeMode) -> Self {
600        *Self::resolve_static(ThemeKey::new(scheme, mode))
601    }
602
603    /// Returns a standard card surface `Style` honoring the active scheme and theme.
604    ///
605    /// # Arguments
606    ///
607    /// - `&Self` - Active theme tokens.
608    ///
609    /// # Returns
610    ///
611    /// - `Style` - Compiled card box style.
612    #[inline(always)]
613    #[must_use]
614    pub fn card_style(&self) -> Style {
615        let theme_palette: ThemePalette = self.get_palette();
616        let theme_geometry: ThemeGeometry = self.get_geometry();
617        let mut style: Style = Style::default();
618        style
619            .get_mut_layout()
620            .set_flex_direction(FlexDirection::Column)
621            .set_padding(Edges::all(SPACE_MD))
622            .set_row_gap(SPACE_MD)
623            .set_column_gap(SPACE_MD)
624            .set_overflow(Overflow::Hidden);
625        style
626            .get_mut_paint()
627            .set_background(Some(theme_palette.get_card()))
628            .set_border(Border::new(
629                theme_geometry.get_border_width(),
630                theme_palette.get_border(),
631            ))
632            .set_radius(CornerRadius::all(theme_geometry.get_radius_lg()))
633            .set_shadow(Some(theme_geometry.get_shadow_sm()));
634        style
635            .get_mut_text()
636            .set_color(theme_palette.get_card_foreground())
637            .set_font_size(theme_geometry.get_font_body());
638        style
639    }
640}
641
642/// Contrast-safe foreground pairing helpers for `ThemePalette`.
643impl ThemePalette {
644    /// Returns a foreground guaranteed to reach `>= CONTRAST_MIN_TEXT` against `bg`, preferring palette text colors.
645    ///
646    /// # Arguments
647    ///
648    /// - `&Self` - Active palette.
649    /// - `Color` - Effective (opaque) background color.
650    ///
651    /// # Returns
652    ///
653    /// - `Color` - Readable foreground color.
654    #[must_use]
655    pub fn readable_foreground_on(&self, bg: Color) -> Color {
656        self.readable_foreground_with(self.get_foreground(), bg)
657    }
658
659    /// Returns `preferred` when it reaches `>= CONTRAST_MIN_TEXT` against `bg`, otherwise the most readable fallback.
660    ///
661    /// Fallback order: palette `foreground`, palette `background`, then pure black / white (one of which always
662    /// exceeds `4.58:1` against any opaque color).
663    ///
664    /// # Arguments
665    ///
666    /// - `&Self` - Active palette.
667    /// - `Color` - Preferred semantic foreground.
668    /// - `Color` - Effective (opaque) background color.
669    ///
670    /// # Returns
671    ///
672    /// - `Color` - Readable foreground color.
673    #[must_use]
674    pub fn readable_foreground_with(&self, preferred: Color, bg: Color) -> Color {
675        if contrast_ratio(preferred, bg) >= CONTRAST_MIN_TEXT {
676            return preferred;
677        }
678        let candidates: [Color; 2] = [self.get_foreground(), self.get_background()];
679        if let Some(found) = candidates
680            .into_iter()
681            .find(|&candidate: &Color| contrast_ratio(candidate, bg) >= CONTRAST_MIN_TEXT)
682        {
683            return found;
684        }
685        if contrast_ratio(COLOR_WHITE, bg) >= contrast_ratio(COLOR_BLACK, bg) {
686            COLOR_WHITE
687        } else {
688            COLOR_BLACK
689        }
690    }
691
692    /// Resolves a muted secondary foreground color meeting `CONTRAST_MIN_TEXT` against `bg` while staying visually distinct from full `foreground`.
693    ///
694    /// # Arguments
695    ///
696    /// - `&Self` - Active palette.
697    /// - `Color` - Effective opaque background color.
698    ///
699    /// # Returns
700    ///
701    /// - `Color` - Contrast-safe muted foreground color.
702    #[must_use]
703    fn readable_muted_foreground(&self, bg: Color) -> Color {
704        let base_muted: Color = self.get_muted_foreground();
705        let full_fg: Color = self.get_foreground();
706        for step in [0_u8, 40_u8, 84_u8, 128_u8, 176_u8] {
707            let candidate: Color = if step == 0 {
708                base_muted
709            } else {
710                mix_rgb_const(base_muted, full_fg, step)
711            };
712            if contrast_ratio(candidate, bg) >= CONTRAST_MIN_TEXT {
713                return candidate;
714            }
715        }
716        self.readable_foreground_with(base_muted, bg)
717    }
718
719    /// Resolves paired `(bg, fg, border)` colors for `role` with a guaranteed-readable foreground.
720    ///
721    /// # Arguments
722    ///
723    /// - `&Self` - Active palette.
724    /// - `SurfaceRole` - Semantic surface role.
725    ///
726    /// # Returns
727    ///
728    /// - `SurfaceColors` - Contrast-safe surface color triple.
729    #[must_use]
730    pub fn surface(&self, role: SurfaceRole) -> SurfaceColors {
731        let (bg, preferred, border): (Color, Color, Color) = match role {
732            SurfaceRole::Background => (
733                self.get_background(),
734                self.get_foreground(),
735                self.get_border(),
736            ),
737            SurfaceRole::Card => (
738                self.get_card(),
739                self.get_card_foreground(),
740                self.get_border(),
741            ),
742            SurfaceRole::Primary => (
743                self.get_primary(),
744                self.get_primary_foreground(),
745                self.get_primary(),
746            ),
747            SurfaceRole::Secondary => (
748                self.get_secondary(),
749                self.get_secondary_foreground(),
750                self.get_border(),
751            ),
752            SurfaceRole::Muted => (self.get_muted(), self.get_foreground(), self.get_border()),
753            SurfaceRole::Accent => (self.get_accent(), self.get_foreground(), self.get_border()),
754            SurfaceRole::Destructive => (
755                self.get_destructive(),
756                self.get_destructive_foreground(),
757                self.get_destructive(),
758            ),
759            SurfaceRole::Success => (
760                self.get_success(),
761                self.get_success_foreground(),
762                self.get_success(),
763            ),
764            SurfaceRole::Warning => (
765                self.get_warning(),
766                self.get_warning_foreground(),
767                self.get_warning(),
768            ),
769        };
770        let opaque_bg: Color = blend_over(bg, self.get_background());
771        SurfaceColors::new(
772            bg,
773            self.readable_foreground_with(preferred, opaque_bg),
774            border,
775        )
776    }
777
778    /// Resolves unified contrast-safe `(bg, fg, border)` colors for any interactive component state across all 34 themes.
779    ///
780    /// Guarantees identical state-transition semantics in Light and Dark modes:
781    /// - Hovering never mutates structural border colors.
782    /// - Every role transitions background and foreground together deterministically.
783    ///
784    /// # Arguments
785    ///
786    /// - `&Self` - Active theme palette.
787    /// - `InteractionRole` - Semantic control interaction family.
788    /// - `bool` - Whether the item is currently active / selected / toggled on.
789    /// - `bool` - Whether the pointer is hovering the control.
790    /// - `bool` - Whether the pointer is actively pressing the control.
791    ///
792    /// # Returns
793    ///
794    /// - `SurfaceColors` - Paired `(bg, fg, border)` colors with `>= 4.5:1` contrast.
795    #[must_use]
796    pub fn interaction_colors(
797        &self,
798        role: InteractionRole,
799        active: bool,
800        hovered: bool,
801        pressed: bool,
802    ) -> SurfaceColors {
803        match role {
804            InteractionRole::SolidPrimary => {
805                let base_surface: SurfaceColors = self.surface(SurfaceRole::Primary);
806                let bg_color: Color = if pressed {
807                    blend_over(base_surface.get_fg().with_alpha(52), base_surface.get_bg())
808                } else if hovered {
809                    blend_over(base_surface.get_fg().with_alpha(26), base_surface.get_bg())
810                } else {
811                    base_surface.get_bg()
812                };
813                let fg_color: Color =
814                    self.readable_foreground_with(base_surface.get_fg(), bg_color);
815                SurfaceColors::new(bg_color, fg_color, bg_color)
816            }
817            InteractionRole::SolidDestructive => {
818                let base_surface: SurfaceColors = self.surface(SurfaceRole::Destructive);
819                let bg_color: Color = if pressed {
820                    blend_over(base_surface.get_fg().with_alpha(52), base_surface.get_bg())
821                } else if hovered {
822                    blend_over(base_surface.get_fg().with_alpha(26), base_surface.get_bg())
823                } else {
824                    base_surface.get_bg()
825                };
826                let fg_color: Color =
827                    self.readable_foreground_with(base_surface.get_fg(), bg_color);
828                SurfaceColors::new(bg_color, fg_color, bg_color)
829            }
830            InteractionRole::SecondarySurface => {
831                let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
832                if pressed {
833                    let bg_color: Color = blend_over(
834                        accent_surface.get_fg().with_alpha(28),
835                        accent_surface.get_bg(),
836                    );
837                    let fg_color: Color =
838                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
839                    SurfaceColors::new(bg_color, fg_color, self.get_border())
840                } else if hovered || active {
841                    SurfaceColors::new(
842                        accent_surface.get_bg(),
843                        accent_surface.get_fg(),
844                        self.get_border(),
845                    )
846                } else {
847                    self.surface(SurfaceRole::Secondary)
848                }
849            }
850            InteractionRole::OutlineSurface => {
851                let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
852                let card_surface: SurfaceColors = self.surface(SurfaceRole::Card);
853                if pressed {
854                    let bg_color: Color = blend_over(
855                        accent_surface.get_fg().with_alpha(28),
856                        accent_surface.get_bg(),
857                    );
858                    let fg_color: Color =
859                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
860                    SurfaceColors::new(bg_color, fg_color, card_surface.get_border())
861                } else if hovered || active {
862                    SurfaceColors::new(
863                        accent_surface.get_bg(),
864                        accent_surface.get_fg(),
865                        card_surface.get_border(),
866                    )
867                } else {
868                    card_surface
869                }
870            }
871            InteractionRole::GhostSurface => {
872                let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
873                if pressed {
874                    let bg_color: Color = blend_over(
875                        accent_surface.get_fg().with_alpha(28),
876                        accent_surface.get_bg(),
877                    );
878                    let fg_color: Color =
879                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
880                    SurfaceColors::new(bg_color, fg_color, COLOR_TRANSPARENT)
881                } else if hovered || active {
882                    SurfaceColors::new(
883                        accent_surface.get_bg(),
884                        accent_surface.get_fg(),
885                        COLOR_TRANSPARENT,
886                    )
887                } else {
888                    let base_surface: SurfaceColors = self.surface(SurfaceRole::Background);
889                    SurfaceColors::new(COLOR_TRANSPARENT, base_surface.get_fg(), COLOR_TRANSPARENT)
890                }
891            }
892            InteractionRole::LinkSurface => {
893                let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
894                if pressed {
895                    let bg_color: Color = blend_over(
896                        accent_surface.get_fg().with_alpha(28),
897                        accent_surface.get_bg(),
898                    );
899                    let fg_color: Color =
900                        self.readable_foreground_with(self.get_primary(), bg_color);
901                    SurfaceColors::new(bg_color, fg_color, COLOR_TRANSPARENT)
902                } else if hovered || active {
903                    let fg_color: Color =
904                        self.readable_foreground_with(self.get_primary(), accent_surface.get_bg());
905                    SurfaceColors::new(accent_surface.get_bg(), fg_color, COLOR_TRANSPARENT)
906                } else {
907                    let fg_color: Color =
908                        self.readable_foreground_with(self.get_primary(), self.get_background());
909                    SurfaceColors::new(COLOR_TRANSPARENT, fg_color, COLOR_TRANSPARENT)
910                }
911            }
912            InteractionRole::SegmentedPill => {
913                if active {
914                    let card_surface: SurfaceColors = self.surface(SurfaceRole::Card);
915                    let bg_color: Color = if pressed {
916                        blend_over(card_surface.get_fg().with_alpha(18), card_surface.get_bg())
917                    } else {
918                        card_surface.get_bg()
919                    };
920                    let fg_color: Color =
921                        self.readable_foreground_with(card_surface.get_fg(), bg_color);
922                    SurfaceColors::new(bg_color, fg_color, card_surface.get_border())
923                } else if pressed {
924                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
925                    let bg_color: Color = blend_over(
926                        accent_surface.get_fg().with_alpha(24),
927                        accent_surface.get_bg(),
928                    );
929                    let fg_color: Color =
930                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
931                    SurfaceColors::new(bg_color, fg_color, COLOR_TRANSPARENT)
932                } else if hovered {
933                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
934                    SurfaceColors::new(
935                        accent_surface.get_bg(),
936                        accent_surface.get_fg(),
937                        COLOR_TRANSPARENT,
938                    )
939                } else {
940                    let muted_bg: Color = self.surface(SurfaceRole::Muted).get_bg();
941                    let idle_fg: Color = self.readable_muted_foreground(muted_bg);
942                    SurfaceColors::new(COLOR_TRANSPARENT, idle_fg, COLOR_TRANSPARENT)
943                }
944            }
945            InteractionRole::NavItem => {
946                if active {
947                    let tint_alpha: u8 = if pressed {
948                        44
949                    } else if hovered {
950                        34
951                    } else {
952                        24
953                    };
954                    let row_bg: Color = self.get_primary().with_alpha(tint_alpha);
955                    let opaque_bg: Color = blend_over(row_bg, self.get_card());
956                    let fg_color: Color =
957                        self.readable_foreground_with(self.get_foreground(), opaque_bg);
958                    SurfaceColors::new(row_bg, fg_color, self.get_primary())
959                } else if pressed {
960                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
961                    let bg_color: Color = blend_over(
962                        accent_surface.get_fg().with_alpha(28),
963                        accent_surface.get_bg(),
964                    );
965                    let fg_color: Color =
966                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
967                    SurfaceColors::new(bg_color, fg_color, COLOR_TRANSPARENT)
968                } else if hovered {
969                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
970                    SurfaceColors::new(
971                        accent_surface.get_bg(),
972                        accent_surface.get_fg(),
973                        COLOR_TRANSPARENT,
974                    )
975                } else {
976                    let card_bg: Color = self.surface(SurfaceRole::Card).get_bg();
977                    let idle_fg: Color = self.readable_muted_foreground(card_bg);
978                    SurfaceColors::new(COLOR_TRANSPARENT, idle_fg, COLOR_TRANSPARENT)
979                }
980            }
981            InteractionRole::OptionItem => {
982                if active {
983                    let primary_surface: SurfaceColors = self.surface(SurfaceRole::Primary);
984                    let bg_color: Color = if pressed {
985                        blend_over(
986                            primary_surface.get_fg().with_alpha(44),
987                            primary_surface.get_bg(),
988                        )
989                    } else if hovered {
990                        blend_over(
991                            primary_surface.get_fg().with_alpha(22),
992                            primary_surface.get_bg(),
993                        )
994                    } else {
995                        primary_surface.get_bg()
996                    };
997                    let fg_color: Color =
998                        self.readable_foreground_with(primary_surface.get_fg(), bg_color);
999                    SurfaceColors::new(bg_color, fg_color, primary_surface.get_border())
1000                } else if pressed {
1001                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
1002                    let bg_color: Color = blend_over(
1003                        accent_surface.get_fg().with_alpha(28),
1004                        accent_surface.get_bg(),
1005                    );
1006                    let fg_color: Color =
1007                        self.readable_foreground_with(accent_surface.get_fg(), bg_color);
1008                    SurfaceColors::new(bg_color, fg_color, COLOR_TRANSPARENT)
1009                } else if hovered {
1010                    let accent_surface: SurfaceColors = self.surface(SurfaceRole::Accent);
1011                    SurfaceColors::new(
1012                        accent_surface.get_bg(),
1013                        accent_surface.get_fg(),
1014                        COLOR_TRANSPARENT,
1015                    )
1016                } else {
1017                    let card_surface: SurfaceColors = self.surface(SurfaceRole::Card);
1018                    SurfaceColors::new(COLOR_TRANSPARENT, card_surface.get_fg(), COLOR_TRANSPARENT)
1019                }
1020            }
1021        }
1022    }
1023}