Skip to main content

sui_widgets/
hdr_theme.rs

1use sui_core::Color;
2use sui_runtime::OutputColorRange;
3
4use crate::theme::{DefaultTheme, ThemeColors};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum HdrThemeMode {
8    #[default]
9    Disabled,
10    WideGamutOnly,
11    ConstrainedHdr,
12    FullHdr,
13}
14
15impl HdrThemeMode {
16    /// This mode as far as an output with `range` can show it: HDR modes
17    /// fall back to wide gamut on wide-gamut SDR outputs, and every mode to
18    /// the SDR baseline on sRGB outputs. An unknown range leaves the mode as
19    /// it is.
20    pub fn limited_to(self, range: Option<OutputColorRange>) -> Self {
21        match (self, range) {
22            (_, None | Some(OutputColorRange::HighDynamicRange)) => self,
23            (Self::ConstrainedHdr | Self::FullHdr, Some(OutputColorRange::WideGamut)) => {
24                Self::WideGamutOnly
25            }
26            (mode, Some(OutputColorRange::WideGamut)) => mode,
27            (_, Some(OutputColorRange::Standard)) => Self::Disabled,
28        }
29    }
30}
31
32#[derive(Debug, Clone, Copy, PartialEq)]
33pub struct SemanticColorToken {
34    pub sdr: Color,
35    pub wide_gamut: Option<Color>,
36    pub hdr: Option<Color>,
37}
38
39impl SemanticColorToken {
40    pub const fn new(sdr: Color, wide_gamut: Option<Color>, hdr: Option<Color>) -> Self {
41        Self {
42            sdr,
43            wide_gamut,
44            hdr,
45        }
46    }
47
48    pub const fn from_sdr(sdr: Color) -> Self {
49        Self::new(sdr, None, None)
50    }
51
52    pub const fn with_wide_gamut(self, wide_gamut: Color) -> Self {
53        Self {
54            wide_gamut: Some(wide_gamut),
55            ..self
56        }
57    }
58
59    pub const fn with_hdr(self, hdr: Color) -> Self {
60        Self {
61            hdr: Some(hdr),
62            ..self
63        }
64    }
65
66    pub fn resolve(self, mode: HdrThemeMode) -> Color {
67        match mode {
68            HdrThemeMode::Disabled => self.sdr,
69            HdrThemeMode::WideGamutOnly => self.wide_gamut.unwrap_or(self.sdr),
70            HdrThemeMode::ConstrainedHdr | HdrThemeMode::FullHdr => {
71                self.hdr.or(self.wide_gamut).unwrap_or(self.sdr)
72            }
73        }
74    }
75}
76
77impl Default for SemanticColorToken {
78    fn default() -> Self {
79        Self::from_sdr(Color::TRANSPARENT)
80    }
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum WidgetColorRole {
85    Surface,
86    SurfaceElevated,
87    SurfaceOutline,
88    Text,
89    TextMuted,
90    Accent,
91    AccentText,
92    Secondary,
93    Info,
94    Success,
95    Warning,
96    Danger,
97}
98
99/// Chromatic roles below this OKLCH chroma are treated as achromatic and get
100/// no wide-gamut variant.
101const WIDE_GAMUT_MIN_CHROMA: f32 = 0.04;
102/// How far wide-gamut variants extend a role's chroma beyond its sRGB value;
103/// the result is gamut-mapped into Display P3.
104const WIDE_GAMUT_CHROMA_BOOST: f32 = 1.15;
105
106#[derive(Debug, Clone, Copy, PartialEq)]
107pub struct HdrColorRoles {
108    pub surface: SemanticColorToken,
109    pub surface_elevated: SemanticColorToken,
110    pub surface_outline: SemanticColorToken,
111    pub text: SemanticColorToken,
112    pub text_muted: SemanticColorToken,
113    pub accent: SemanticColorToken,
114    pub accent_text: SemanticColorToken,
115    pub secondary: SemanticColorToken,
116    pub info: SemanticColorToken,
117    pub success: SemanticColorToken,
118    pub warning: SemanticColorToken,
119    pub danger: SemanticColorToken,
120}
121
122impl HdrColorRoles {
123    pub fn from_colors(colors: ThemeColors) -> Self {
124        let neutrals = colors.neutrals;
125        let mut roles = Self {
126            surface: SemanticColorToken::from_sdr(neutrals.window),
127            surface_elevated: SemanticColorToken::from_sdr(neutrals.panel),
128            surface_outline: SemanticColorToken::from_sdr(neutrals.border),
129            text: SemanticColorToken::from_sdr(neutrals.text),
130            text_muted: SemanticColorToken::from_sdr(neutrals.text_secondary),
131            accent: SemanticColorToken::from_sdr(colors.primary),
132            accent_text: SemanticColorToken::from_sdr(colors.on_primary),
133            secondary: SemanticColorToken::from_sdr(colors.secondary),
134            info: SemanticColorToken::from_sdr(colors.info),
135            success: SemanticColorToken::from_sdr(colors.success),
136            warning: SemanticColorToken::from_sdr(colors.warning),
137            danger: SemanticColorToken::from_sdr(colors.danger),
138        };
139        roles.apply_display_variants(colors);
140        roles
141    }
142
143    pub fn from_default_theme(theme: DefaultTheme) -> Self {
144        Self::from_colors(theme.colors)
145    }
146
147    pub fn sync_sdr_from_colors(&mut self, colors: ThemeColors) {
148        *self = Self::from_colors(colors);
149    }
150
151    /// Derive Display P3 variants for every chromatic role by extending its
152    /// OKLCH chroma into the wider gamut, and extended-range HDR variants for
153    /// the live-signal roles (accent and secondary). Achromatic roles — the
154    /// neutral presets' primary, for example — stay SDR-only.
155    fn apply_display_variants(&mut self, colors: ThemeColors) {
156        for token in [
157            &mut self.accent,
158            &mut self.secondary,
159            &mut self.info,
160            &mut self.success,
161            &mut self.warning,
162            &mut self.danger,
163        ] {
164            let oklch = token.sdr.to_oklch();
165            if oklch.chroma < WIDE_GAMUT_MIN_CHROMA {
166                continue;
167            }
168            *token = token.with_wide_gamut(
169                oklch
170                    .with_chroma(oklch.chroma * WIDE_GAMUT_CHROMA_BOOST)
171                    .to_display_p3(),
172            );
173        }
174
175        let lift = match colors.scheme {
176            crate::theme::ThemeColorScheme::Light => 1.15,
177            crate::theme::ThemeColorScheme::Dark => 1.25,
178            crate::theme::ThemeColorScheme::HighContrast => 1.1,
179        };
180        for token in [&mut self.accent, &mut self.secondary] {
181            if token.wide_gamut.is_none() {
182                continue;
183            }
184            let oklch = token.sdr.to_oklch();
185            let linear = oklch
186                .with_chroma(oklch.chroma * WIDE_GAMUT_CHROMA_BOOST)
187                .to_linear_display_p3();
188            *token = token.with_hdr(Color::linear_display_p3(
189                linear.red * lift,
190                linear.green * lift,
191                linear.blue * lift,
192                linear.alpha,
193            ));
194        }
195    }
196
197    pub fn for_widget_role(self, role: WidgetColorRole) -> SemanticColorToken {
198        match role {
199            WidgetColorRole::Surface => self.surface,
200            WidgetColorRole::SurfaceElevated => self.surface_elevated,
201            WidgetColorRole::SurfaceOutline => self.surface_outline,
202            WidgetColorRole::Text => self.text,
203            WidgetColorRole::TextMuted => self.text_muted,
204            WidgetColorRole::Accent => self.accent,
205            WidgetColorRole::AccentText => self.accent_text,
206            WidgetColorRole::Secondary => self.secondary,
207            WidgetColorRole::Info => self.info,
208            WidgetColorRole::Success => self.success,
209            WidgetColorRole::Warning => self.warning,
210            WidgetColorRole::Danger => self.danger,
211        }
212    }
213}
214
215impl Default for HdrColorRoles {
216    fn default() -> Self {
217        Self::from_colors(ThemeColors::default())
218    }
219}
220
221#[derive(Debug, Clone, Copy, PartialEq)]
222pub struct HdrLuminanceTokens {
223    pub reference_white: f32,
224    pub focused: f32,
225    pub semantic_accent: f32,
226    pub emissive_indicator: f32,
227    pub alert_pulse: f32,
228}
229
230impl HdrLuminanceTokens {
231    pub const fn constrained_defaults() -> Self {
232        let reference_white = 1.0;
233        Self {
234            reference_white,
235            focused: 1.05,
236            semantic_accent: 1.1,
237            emissive_indicator: 1.25,
238            alert_pulse: 1.15,
239        }
240    }
241}
242
243impl Default for HdrLuminanceTokens {
244    fn default() -> Self {
245        Self::constrained_defaults()
246    }
247}
248
249#[derive(Debug, Clone, Copy, PartialEq)]
250pub struct MaterialToken {
251    pub opacity: f32,
252    pub blur_radius: f32,
253    pub specular_strength: f32,
254    pub rim_light_strength: f32,
255}
256
257impl MaterialToken {
258    pub const fn new(
259        opacity: f32,
260        blur_radius: f32,
261        specular_strength: f32,
262        rim_light_strength: f32,
263    ) -> Self {
264        Self {
265            opacity,
266            blur_radius,
267            specular_strength,
268            rim_light_strength,
269        }
270    }
271}
272
273impl Default for MaterialToken {
274    fn default() -> Self {
275        Self::new(1.0, 0.0, 0.0, 0.0)
276    }
277}
278
279#[derive(Debug, Clone, Copy, PartialEq)]
280pub struct HdrMaterialTokens {
281    pub flat: MaterialToken,
282    pub raised: MaterialToken,
283    pub glass: MaterialToken,
284    pub glossy: MaterialToken,
285    pub stylized: MaterialToken,
286}
287
288impl Default for HdrMaterialTokens {
289    fn default() -> Self {
290        Self {
291            flat: MaterialToken::new(1.0, 0.0, 0.0, 0.0),
292            raised: MaterialToken::new(0.98, 0.0, 0.08, 0.04),
293            glass: MaterialToken::new(0.84, 6.0, 0.14, 0.10),
294            glossy: MaterialToken::new(0.92, 0.0, 0.24, 0.18),
295            stylized: MaterialToken::new(0.88, 4.0, 0.28, 0.22),
296        }
297    }
298}
299
300#[derive(Debug, Clone, Copy, PartialEq)]
301pub struct EffectToken {
302    pub intensity: f32,
303    pub speed: f32,
304    pub color: Option<Color>,
305}
306
307impl EffectToken {
308    pub const fn new(intensity: f32, speed: f32, color: Option<Color>) -> Self {
309        Self {
310            intensity,
311            speed,
312            color,
313        }
314    }
315}
316
317impl Default for EffectToken {
318    fn default() -> Self {
319        Self::new(0.0, 0.0, None)
320    }
321}
322
323#[derive(Debug, Clone, Copy, PartialEq)]
324pub struct HdrEffectTokens {
325    pub focus_ring: EffectToken,
326    pub glow: EffectToken,
327    pub pulse: EffectToken,
328}
329
330impl Default for HdrEffectTokens {
331    fn default() -> Self {
332        Self {
333            focus_ring: EffectToken::new(0.35, 0.0, None),
334            glow: EffectToken::new(0.24, 0.0, None),
335            pulse: EffectToken::new(0.4, 1.0, None),
336        }
337    }
338}
339
340#[derive(Debug, Clone, Copy, PartialEq)]
341pub struct HdrPolicyTokens {
342    pub max_large_area_lift: f32,
343    pub max_constrained_lift: f32,
344    pub max_emissive_lift: f32,
345}
346
347impl Default for HdrPolicyTokens {
348    fn default() -> Self {
349        Self {
350            max_large_area_lift: 1.2,
351            max_constrained_lift: 1.35,
352            max_emissive_lift: 2.0,
353        }
354    }
355}
356
357#[derive(Debug, Clone, Copy, PartialEq)]
358pub struct HdrThemeTokens {
359    pub mode: HdrThemeMode,
360    pub color_roles: HdrColorRoles,
361    pub luminance: HdrLuminanceTokens,
362    pub materials: HdrMaterialTokens,
363    pub effects: HdrEffectTokens,
364    pub policy: HdrPolicyTokens,
365}
366
367impl HdrThemeTokens {
368    pub fn from_colors(colors: ThemeColors) -> Self {
369        Self {
370            mode: HdrThemeMode::Disabled,
371            color_roles: HdrColorRoles::from_colors(colors),
372            luminance: HdrLuminanceTokens::default(),
373            materials: HdrMaterialTokens::default(),
374            effects: HdrEffectTokens::default(),
375            policy: HdrPolicyTokens::default(),
376        }
377    }
378
379    pub fn from_default_theme(theme: DefaultTheme) -> Self {
380        Self::from_colors(theme.colors)
381    }
382
383    /// These tokens with their mode limited to what an output with `range`
384    /// can show; see [`HdrThemeMode::limited_to`].
385    pub fn limited_to(mut self, range: Option<OutputColorRange>) -> Self {
386        self.mode = self.mode.limited_to(range);
387        self
388    }
389
390    pub fn sync_semantic_defaults(&mut self, colors: ThemeColors) {
391        self.color_roles.sync_sdr_from_colors(colors);
392    }
393}
394
395impl Default for HdrThemeTokens {
396    fn default() -> Self {
397        Self::from_colors(ThemeColors::default())
398    }
399}
400
401#[derive(Debug, Clone, Copy, PartialEq, Eq)]
402pub enum WidgetLuminanceRole {
403    Standard,
404    Focused,
405    SemanticAccent,
406    EmissiveIndicator,
407    AlertPulse,
408}
409
410#[derive(Debug, Clone, Copy, PartialEq, Eq)]
411pub enum WidgetMaterialRole {
412    Flat,
413    Raised,
414    Glass,
415    Glossy,
416    Stylized,
417}
418
419#[derive(Debug, Clone, Copy, PartialEq, Eq)]
420pub enum WidgetEffectRole {
421    FocusRing,
422    Glow,
423    Pulse,
424}
425
426#[derive(Debug, Clone, Copy, PartialEq)]
427pub struct ResolvedMaterialStyle {
428    pub opacity: f32,
429    pub blur_radius: f32,
430    pub specular_strength: f32,
431    pub rim_light_strength: f32,
432}
433
434impl ResolvedMaterialStyle {
435    fn from_token(token: MaterialToken) -> Self {
436        Self {
437            opacity: token.opacity,
438            blur_radius: token.blur_radius,
439            specular_strength: token.specular_strength,
440            rim_light_strength: token.rim_light_strength,
441        }
442    }
443}
444
445impl Default for ResolvedMaterialStyle {
446    fn default() -> Self {
447        Self::from_token(MaterialToken::default())
448    }
449}
450
451#[derive(Debug, Clone, Copy, PartialEq)]
452pub struct ResolvedEffectStyle {
453    pub intensity: f32,
454    pub speed: f32,
455    pub color: Color,
456}
457
458#[derive(Debug, Clone, Copy, PartialEq)]
459pub struct ResolvedHdrStyle {
460    pub color: Color,
461    pub peak_lift: f32,
462    pub material: ResolvedMaterialStyle,
463    pub effect: Option<ResolvedEffectStyle>,
464}
465
466pub fn resolve_semantic_color(token: SemanticColorToken, mode: HdrThemeMode) -> Color {
467    token.resolve(mode)
468}
469
470pub fn resolve_luminance_role(tokens: &HdrThemeTokens, role: WidgetLuminanceRole) -> f32 {
471    let requested = match role {
472        WidgetLuminanceRole::Standard => tokens.luminance.reference_white,
473        WidgetLuminanceRole::Focused => tokens.luminance.focused,
474        WidgetLuminanceRole::SemanticAccent => tokens.luminance.semantic_accent,
475        WidgetLuminanceRole::EmissiveIndicator => tokens.luminance.emissive_indicator,
476        WidgetLuminanceRole::AlertPulse => tokens.luminance.alert_pulse,
477    }
478    .max(tokens.luminance.reference_white);
479
480    match tokens.mode {
481        HdrThemeMode::Disabled => tokens.luminance.reference_white,
482        HdrThemeMode::WideGamutOnly => requested.min(tokens.luminance.reference_white),
483        HdrThemeMode::ConstrainedHdr => match role {
484            WidgetLuminanceRole::EmissiveIndicator | WidgetLuminanceRole::AlertPulse => {
485                requested.min(tokens.policy.max_constrained_lift)
486            }
487            WidgetLuminanceRole::Standard
488            | WidgetLuminanceRole::Focused
489            | WidgetLuminanceRole::SemanticAccent => {
490                requested.min(tokens.policy.max_large_area_lift)
491            }
492        },
493        HdrThemeMode::FullHdr => match role {
494            WidgetLuminanceRole::EmissiveIndicator | WidgetLuminanceRole::AlertPulse => {
495                requested.min(tokens.policy.max_emissive_lift)
496            }
497            WidgetLuminanceRole::Standard
498            | WidgetLuminanceRole::Focused
499            | WidgetLuminanceRole::SemanticAccent => {
500                requested.min(tokens.policy.max_large_area_lift)
501            }
502        },
503    }
504}
505
506pub fn resolve_material_role(
507    tokens: &HdrThemeTokens,
508    role: WidgetMaterialRole,
509) -> ResolvedMaterialStyle {
510    let token = match role {
511        WidgetMaterialRole::Flat => tokens.materials.flat,
512        WidgetMaterialRole::Raised => tokens.materials.raised,
513        WidgetMaterialRole::Glass => tokens.materials.glass,
514        WidgetMaterialRole::Glossy => tokens.materials.glossy,
515        WidgetMaterialRole::Stylized => tokens.materials.stylized,
516    };
517
518    if matches!(tokens.mode, HdrThemeMode::Disabled) {
519        ResolvedMaterialStyle::from_token(tokens.materials.flat)
520    } else {
521        ResolvedMaterialStyle::from_token(token)
522    }
523}
524
525pub fn resolve_effect_role(
526    tokens: &HdrThemeTokens,
527    role: WidgetEffectRole,
528    fallback_color: Color,
529) -> Option<ResolvedEffectStyle> {
530    if matches!(tokens.mode, HdrThemeMode::Disabled) {
531        return None;
532    }
533
534    let token = match role {
535        WidgetEffectRole::FocusRing => tokens.effects.focus_ring,
536        WidgetEffectRole::Glow => tokens.effects.glow,
537        WidgetEffectRole::Pulse => tokens.effects.pulse,
538    };
539
540    (token.intensity > 0.0).then_some(ResolvedEffectStyle {
541        intensity: token.intensity,
542        speed: token.speed,
543        color: token.color.unwrap_or(fallback_color),
544    })
545}
546
547pub fn resolve_widget_hdr_style(
548    tokens: &HdrThemeTokens,
549    color_role: WidgetColorRole,
550    luminance_role: WidgetLuminanceRole,
551    material_role: WidgetMaterialRole,
552    effect_role: Option<WidgetEffectRole>,
553) -> ResolvedHdrStyle {
554    let color = resolve_semantic_color(tokens.color_roles.for_widget_role(color_role), tokens.mode);
555
556    ResolvedHdrStyle {
557        color,
558        peak_lift: resolve_luminance_role(tokens, luminance_role),
559        material: resolve_material_role(tokens, material_role),
560        effect: effect_role.and_then(|role| resolve_effect_role(tokens, role, color)),
561    }
562}
563
564#[cfg(test)]
565mod tests {
566    use std::fmt::Debug;
567
568    use super::{
569        HdrColorRoles, HdrEffectTokens, HdrLuminanceTokens, HdrMaterialTokens, HdrPolicyTokens,
570        HdrThemeMode, HdrThemeTokens, MaterialToken, ResolvedHdrStyle, SemanticColorToken,
571        WidgetColorRole, WidgetEffectRole, WidgetLuminanceRole, WidgetMaterialRole,
572        resolve_luminance_role, resolve_material_role, resolve_semantic_color,
573        resolve_widget_hdr_style,
574    };
575    use crate::theme::DefaultTheme;
576    use sui_core::{Color, ColorSpace};
577
578    fn assert_copy_debug_eq<T: Copy + Debug + PartialEq>() {}
579
580    #[test]
581    fn hdr_theme_modes_fall_back_to_what_the_output_can_show() {
582        use sui_runtime::OutputColorRange::{HighDynamicRange, Standard, WideGamut};
583
584        let modes = [
585            HdrThemeMode::Disabled,
586            HdrThemeMode::WideGamutOnly,
587            HdrThemeMode::ConstrainedHdr,
588            HdrThemeMode::FullHdr,
589        ];
590        for mode in modes {
591            assert_eq!(mode.limited_to(None), mode);
592            assert_eq!(mode.limited_to(Some(HighDynamicRange)), mode);
593            assert_eq!(mode.limited_to(Some(Standard)), HdrThemeMode::Disabled);
594        }
595        assert_eq!(
596            HdrThemeMode::Disabled.limited_to(Some(WideGamut)),
597            HdrThemeMode::Disabled
598        );
599        for mode in [
600            HdrThemeMode::WideGamutOnly,
601            HdrThemeMode::ConstrainedHdr,
602            HdrThemeMode::FullHdr,
603        ] {
604            assert_eq!(
605                mode.limited_to(Some(WideGamut)),
606                HdrThemeMode::WideGamutOnly
607            );
608        }
609    }
610
611    #[test]
612    fn hdr_theme_tokens_default_to_disabled_mode() {
613        let tokens = HdrThemeTokens::default();
614
615        assert_eq!(tokens.mode, HdrThemeMode::Disabled);
616    }
617
618    #[test]
619    fn hdr_luminance_constrained_defaults_stay_at_or_above_reference_white() {
620        let tokens = HdrLuminanceTokens::constrained_defaults();
621
622        assert!(tokens.focused >= tokens.reference_white);
623        assert!(tokens.semantic_accent >= tokens.reference_white);
624        assert!(tokens.emissive_indicator >= tokens.reference_white);
625        assert!(tokens.alert_pulse >= tokens.reference_white);
626    }
627
628    #[test]
629    fn hdr_color_roles_derive_from_default_theme_semantics() {
630        let theme = DefaultTheme::default();
631        let roles = HdrColorRoles::from_default_theme(theme);
632
633        assert_eq!(roles.surface.sdr, theme.colors.neutrals.window);
634        assert_eq!(roles.surface_elevated.sdr, theme.colors.neutrals.panel);
635        assert_eq!(roles.text.sdr, theme.colors.neutrals.text);
636        assert_eq!(roles.accent.sdr, theme.colors.primary);
637        assert_eq!(roles.accent_text.sdr, theme.colors.on_primary);
638        assert_eq!(roles.success.sdr, theme.colors.success);
639        assert_eq!(roles.warning.sdr, theme.colors.warning);
640        assert_eq!(roles.danger.sdr, theme.colors.danger);
641    }
642
643    #[test]
644    fn chromatic_roles_derive_p3_and_hdr_signal_variants() {
645        let dark = DefaultTheme::dark();
646        let roles = HdrColorRoles::from_default_theme(dark);
647
648        let accent_wide = roles.accent.wide_gamut.expect("accent P3 variant");
649        assert_eq!(accent_wide.space, ColorSpace::DisplayP3);
650        assert!(accent_wide.to_oklch().chroma > dark.colors.primary.to_oklch().chroma);
651        let accent_hdr = roles.accent.hdr.expect("accent HDR variant");
652        assert_eq!(accent_hdr.space, ColorSpace::LinearDisplayP3);
653        assert!(roles.secondary.hdr.is_some());
654        assert!(roles.danger.wide_gamut.is_some());
655        assert_eq!(roles.danger.hdr, None);
656        assert_eq!(roles.surface.wide_gamut, None);
657        assert_eq!(roles.surface.hdr, None);
658        assert_eq!(roles.accent_text.wide_gamut, None);
659    }
660
661    #[test]
662    fn hdr_tokens_are_copy_debug_and_partial_eq_where_practical() {
663        assert_copy_debug_eq::<SemanticColorToken>();
664        assert_copy_debug_eq::<HdrColorRoles>();
665        assert_copy_debug_eq::<HdrLuminanceTokens>();
666        assert_copy_debug_eq::<MaterialToken>();
667        assert_copy_debug_eq::<HdrMaterialTokens>();
668        assert_copy_debug_eq::<HdrEffectTokens>();
669        assert_copy_debug_eq::<HdrPolicyTokens>();
670        assert_copy_debug_eq::<HdrThemeTokens>();
671        assert_copy_debug_eq::<ResolvedHdrStyle>();
672    }
673
674    #[test]
675    fn disabled_mode_material_role_resolves_flat_style() {
676        let mut tokens = HdrThemeTokens {
677            mode: HdrThemeMode::Disabled,
678            ..HdrThemeTokens::default()
679        };
680        tokens.materials.flat = MaterialToken::new(1.0, 0.0, 0.0, 0.0);
681        tokens.materials.raised = MaterialToken::new(0.98, 0.0, 0.08, 0.04);
682        tokens.materials.glass = MaterialToken::new(0.42, 8.0, 0.35, 0.28);
683        tokens.materials.glossy = MaterialToken::new(0.77, 2.0, 0.5, 0.4);
684        tokens.materials.stylized = MaterialToken::new(0.61, 5.0, 0.65, 0.55);
685
686        let expected = resolve_material_role(&tokens, WidgetMaterialRole::Flat);
687
688        assert_eq!(expected.opacity, 1.0);
689        assert_eq!(expected.blur_radius, 0.0);
690        assert_eq!(expected.specular_strength, 0.0);
691        assert_eq!(expected.rim_light_strength, 0.0);
692        assert_eq!(
693            resolve_material_role(&tokens, WidgetMaterialRole::Raised),
694            expected
695        );
696        assert_eq!(
697            resolve_material_role(&tokens, WidgetMaterialRole::Glass),
698            expected
699        );
700        assert_eq!(
701            resolve_material_role(&tokens, WidgetMaterialRole::Glossy),
702            expected
703        );
704        assert_eq!(
705            resolve_material_role(&tokens, WidgetMaterialRole::Stylized),
706            expected
707        );
708    }
709
710    #[test]
711    fn disabled_mode_resolves_to_sdr_semantics() {
712        let token = SemanticColorToken::from_sdr(Color::rgba(0.2, 0.3, 0.4, 1.0))
713            .with_wide_gamut(Color::display_p3(0.8, 0.2, 0.1, 1.0))
714            .with_hdr(Color::linear_display_p3(1.4, 0.3, 0.2, 1.0));
715        let mut tokens = HdrThemeTokens {
716            mode: HdrThemeMode::Disabled,
717            ..HdrThemeTokens::default()
718        };
719        tokens.color_roles.accent = token;
720        tokens.luminance.semantic_accent = 1.8;
721
722        let resolved = resolve_widget_hdr_style(
723            &tokens,
724            WidgetColorRole::Accent,
725            WidgetLuminanceRole::SemanticAccent,
726            WidgetMaterialRole::Glass,
727            Some(WidgetEffectRole::Glow),
728        );
729
730        assert_eq!(resolve_semantic_color(token, tokens.mode), token.sdr);
731        assert_eq!(resolved.color, token.sdr);
732        assert_eq!(resolved.peak_lift, tokens.luminance.reference_white);
733        assert_eq!(
734            resolved.material,
735            resolve_material_role(&tokens, WidgetMaterialRole::Flat)
736        );
737        assert!(resolved.effect.is_none());
738    }
739
740    #[test]
741    fn wide_gamut_only_prefers_wide_gamut_variants_but_clamps_luminance_to_reference_white() {
742        let mut tokens = HdrThemeTokens {
743            mode: HdrThemeMode::WideGamutOnly,
744            ..HdrThemeTokens::default()
745        };
746        tokens.color_roles.accent = SemanticColorToken::from_sdr(Color::rgba(0.2, 0.3, 0.4, 1.0))
747            .with_wide_gamut(Color::display_p3(0.8, 0.2, 0.1, 1.0))
748            .with_hdr(Color::linear_display_p3(1.4, 0.3, 0.2, 1.0));
749        tokens.luminance.semantic_accent = 1.8;
750
751        let resolved = resolve_widget_hdr_style(
752            &tokens,
753            WidgetColorRole::Accent,
754            WidgetLuminanceRole::SemanticAccent,
755            WidgetMaterialRole::Raised,
756            None,
757        );
758
759        assert_eq!(resolved.color, Color::display_p3(0.8, 0.2, 0.1, 1.0));
760        assert_eq!(resolved.peak_lift, tokens.luminance.reference_white);
761    }
762
763    #[test]
764    fn constrained_hdr_caps_emissive_roles_below_full_hdr() {
765        let mut constrained = HdrThemeTokens {
766            mode: HdrThemeMode::ConstrainedHdr,
767            ..HdrThemeTokens::default()
768        };
769        constrained.luminance.emissive_indicator = 2.5;
770        constrained.policy.max_constrained_lift = 1.3;
771        constrained.policy.max_emissive_lift = 2.2;
772
773        let mut full = constrained;
774        full.mode = HdrThemeMode::FullHdr;
775
776        let constrained_peak =
777            resolve_luminance_role(&constrained, WidgetLuminanceRole::EmissiveIndicator);
778        let full_peak = resolve_luminance_role(&full, WidgetLuminanceRole::EmissiveIndicator);
779
780        assert_eq!(constrained_peak, 1.3);
781        assert_eq!(full_peak, 2.2);
782        assert!(constrained_peak < full_peak);
783    }
784
785    #[test]
786    fn full_hdr_respects_max_large_area_lift_policy() {
787        let mut tokens = HdrThemeTokens {
788            mode: HdrThemeMode::FullHdr,
789            ..HdrThemeTokens::default()
790        };
791        tokens.luminance.semantic_accent = 1.8;
792        tokens.policy.max_large_area_lift = 1.25;
793
794        assert_eq!(
795            resolve_luminance_role(&tokens, WidgetLuminanceRole::SemanticAccent),
796            1.25
797        );
798    }
799}