Skip to main content

sui_widgets/
hdr_theme.rs

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