Skip to main content

waft_material/
color_scheme.rs

1//! Flutter counterpart: material_ui's `color_scheme.dart`.
2//!
3//! Every colour of a `ColorScheme` is read through a method (`scheme.primary()`), and its fields
4//! are private: Dart reads properties, and the nullable colours keep Flutter's fallbacks inside
5//! the getter, as Dart's `_primaryContainer ?? primary`; they are set with the `with_*` setters.
6//! `fromSeed` and `fromSwatch` take builders, since their arguments decide every colour at once.
7//! `fromImageProvider` waits for reading an image's pixels (see this folder's `PORTING.md`).
8
9use std::rc::Rc;
10
11use waft_embedder::{Brightness, Color};
12use waft_foundation::{App, DiagnosticsProperty, PRECISION_ERROR_TOLERANCE};
13use waft_material_color_utilities::{
14    DynamicColor, DynamicScheme, Hct, MaterialDynamicColors, SchemeContent, SchemeExpressive,
15    SchemeFidelity, SchemeFruitSalad, SchemeMonochrome, SchemeNeutral, SchemeRainbow,
16    SchemeTonalSpot, SchemeVibrant,
17};
18use waft_painting::ColorProperty;
19use waft_widgets::BuildContext;
20
21use crate::colors::{Colors, MaterialColor};
22use crate::theme::Theme;
23use crate::theme_data::ThemeData;
24
25/// The algorithm used to construct a `ColorScheme` in `ColorScheme::from_seed`.
26///
27/// The `tonalSpot` variant builds default Material scheme colors. These colors are
28/// mapped to light or dark tones to achieve visually accessible color
29/// pairings with sufficient contrast between foreground and background elements.
30///
31/// In some cases, the tones can prevent colors from appearing as intended,
32/// such as when a color is too light to offer enough contrast for accessibility.
33/// Color fidelity (`DynamicSchemeVariant.fidelity`) is a feature that adjusts
34/// tones in these cases to produce the intended visual results without harming
35/// visual contrast.
36#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
37pub enum DynamicSchemeVariant {
38    /// Default for Material theme colors. Builds pastel palettes with a low chroma.
39    TonalSpot,
40
41    /// The resulting color palettes match seed color, even if the seed color
42    /// is very bright (high chroma).
43    Fidelity,
44
45    /// All colors are grayscale, no chroma.
46    Monochrome,
47
48    /// Close to grayscale, a hint of chroma.
49    Neutral,
50
51    /// Pastel colors, high chroma palettes. The primary palette's chroma is at
52    /// maximum. Use `fidelity` instead if tokens should alter their tone to match
53    /// the palette vibrancy.
54    Vibrant,
55
56    /// Pastel colors, medium chroma palettes. The primary palette's hue is
57    /// different from the seed color, for variety.
58    Expressive,
59
60    /// Almost identical to `fidelity`. Tokens and palettes match the seed color.
61    /// `ColorScheme::primary_container` is the seed color, adjusted to ensure
62    /// contrast with surfaces. The tertiary palette is analogue of the seed color.
63    Content,
64
65    /// A playful theme - the seed color's hue does not appear in the theme.
66    Rainbow,
67
68    /// A playful theme - the seed color's hue does not appear in the theme.
69    FruitSalad,
70}
71
72impl DynamicSchemeVariant {
73    /// Dart's `DynamicSchemeVariant.values`, in declaration order.
74    pub const VALUES: [DynamicSchemeVariant; 9] = [
75        DynamicSchemeVariant::TonalSpot,
76        DynamicSchemeVariant::Fidelity,
77        DynamicSchemeVariant::Monochrome,
78        DynamicSchemeVariant::Neutral,
79        DynamicSchemeVariant::Vibrant,
80        DynamicSchemeVariant::Expressive,
81        DynamicSchemeVariant::Content,
82        DynamicSchemeVariant::Rainbow,
83        DynamicSchemeVariant::FruitSalad,
84    ];
85}
86
87/// {@template material_ui.color_scheme.ColorScheme}
88/// A set of 45 colors based on the
89/// [Material spec](https://m3.material.io/styles/color/the-color-system/color-roles)
90/// that can be used to configure the color properties of most components.
91/// {@endtemplate}
92///
93/// ### Colors in Material 3
94///
95/// {@macro material_ui.colors.colorRoles}
96///
97/// The main accent color groups in the scheme are `primary`, `secondary`,
98/// and `tertiary`.
99///
100/// * Primary colors are used for key components across the UI, such as the FAB,
101///   prominent buttons, and active states.
102///
103/// * Secondary colors are used for less prominent components in the UI, such as
104///   filter chips, while expanding the opportunity for color expression.
105///
106/// * Tertiary colors are used for contrasting accents that can be used to
107///   balance primary and secondary colors or bring heightened attention to
108///   an element, such as an input field. The tertiary colors are left
109///   for makers to use at their discretion and are intended to support
110///   broader color expression in products.
111///
112/// Each accent color group (primary, secondary and tertiary) includes '-Fixed'
113/// '-Dim' color roles, such as `primary_fixed` and `primary_fixed_dim`. Fixed roles
114/// are appropriate to use in places where Container roles are normally used,
115/// but they stay the same color between light and dark themes. The '-Dim' roles
116/// provide a stronger, more emphasized color with the same fixed behavior.
117///
118/// The remaining colors of the scheme are composed of neutral colors used for
119/// backgrounds and surfaces, as well as specific colors for errors, dividers
120/// and shadows. Surface colors are used for backgrounds and large, low-emphasis
121/// areas of the screen.
122///
123/// Material 3 also introduces tone-based surfaces and surface containers.
124/// They replace the old opacity-based model which applied a tinted overlay on
125/// top of surfaces based on their elevation. These colors include: `surface_bright`,
126/// `surface_dim`, `surface_container_lowest`, `surface_container_low`, `surface_container`,
127/// `surface_container_high`, and `surface_container_highest`.
128///
129/// Many of the colors have matching 'on' colors, which are used for drawing
130/// content on top of the matching color. For example, if something is using
131/// `primary` for a background color, `on_primary` would be used to paint text
132/// and icons on top of it. For this reason, the 'on' colors should have a
133/// contrast ratio with their matching colors of at least 4.5:1 in order to
134/// be readable. On '-FixedVariant' roles, such as `on_primary_fixed_variant`,
135/// also have the same color between light and dark themes, but compared
136/// with on '-Fixed' roles, such as `on_primary_fixed`, they provide a
137/// lower-emphasis option for text and icons.
138///
139/// <callout-box>
140///
141/// This example shows all Material `ColorScheme` roles in light and dark
142/// brightnesses.
143///
144// TODO(framework): Replace the following block with a @dartpad directive
145// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
146/// {@macro material_ui.dartpad_guide}
147///
148/// {@example /example/lib/color_scheme/color_scheme.0.dart#body}
149///
150/// </callout-box>
151///
152/// ### Setting Colors in Flutter
153///
154///{@macro material_ui.colors.settingColors}
155#[derive(Clone, Debug)]
156pub struct ColorScheme {
157    brightness: Brightness,
158    /// The color displayed most frequently across your app’s screens and components.
159    primary: Color,
160    /// A color that's clearly legible when drawn on `primary`.
161    ///
162    /// To ensure that an app is accessible, a contrast ratio between
163    /// `primary` and `on_primary` of at least 4.5:1 is recommended. See
164    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
165    on_primary: Color,
166    primary_container: Option<Color>,
167    on_primary_container: Option<Color>,
168    primary_fixed: Option<Color>,
169    primary_fixed_dim: Option<Color>,
170    on_primary_fixed: Option<Color>,
171    on_primary_fixed_variant: Option<Color>,
172    /// An accent color used for less prominent components in the UI, such as
173    /// filter chips, while expanding the opportunity for color expression.
174    secondary: Color,
175    /// A color that's clearly legible when drawn on `secondary`.
176    ///
177    /// To ensure that an app is accessible, a contrast ratio between
178    /// `secondary` and `on_secondary` of at least 4.5:1 is recommended. See
179    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
180    on_secondary: Color,
181    secondary_container: Option<Color>,
182    on_secondary_container: Option<Color>,
183    secondary_fixed: Option<Color>,
184    secondary_fixed_dim: Option<Color>,
185    on_secondary_fixed: Option<Color>,
186    on_secondary_fixed_variant: Option<Color>,
187    tertiary: Option<Color>,
188    on_tertiary: Option<Color>,
189    tertiary_container: Option<Color>,
190    on_tertiary_container: Option<Color>,
191    tertiary_fixed: Option<Color>,
192    tertiary_fixed_dim: Option<Color>,
193    on_tertiary_fixed: Option<Color>,
194    on_tertiary_fixed_variant: Option<Color>,
195    /// The color to use for input validation errors, e.g. for
196    /// `InputDecoration.errorText`.
197    error: Color,
198    /// A color that's clearly legible when drawn on `error`.
199    ///
200    /// To ensure that an app is accessible, a contrast ratio between
201    /// `error` and `on_error` of at least 4.5:1 is recommended. See
202    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
203    on_error: Color,
204    error_container: Option<Color>,
205    on_error_container: Option<Color>,
206    /// The background color for widgets like `Scaffold`.
207    surface: Color,
208    /// A color that's clearly legible when drawn on `surface`.
209    ///
210    /// To ensure that an app is accessible, a contrast ratio between
211    /// `surface` and `on_surface` of at least 4.5:1 is recommended. See
212    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
213    on_surface: Color,
214    surface_variant: Option<Color>,
215    surface_dim: Option<Color>,
216    surface_bright: Option<Color>,
217    surface_container_lowest: Option<Color>,
218    surface_container_low: Option<Color>,
219    surface_container: Option<Color>,
220    surface_container_high: Option<Color>,
221    surface_container_highest: Option<Color>,
222    on_surface_variant: Option<Color>,
223    outline: Option<Color>,
224    outline_variant: Option<Color>,
225    shadow: Option<Color>,
226    scrim: Option<Color>,
227    inverse_surface: Option<Color>,
228    on_inverse_surface: Option<Color>,
229    inverse_primary: Option<Color>,
230    surface_tint: Option<Color>,
231    background: Option<Color>,
232    on_background: Option<Color>,
233}
234
235impl ColorScheme {
236    /// Create a ColorScheme instance from the given colors.
237    ///
238    /// `ColorScheme::from_seed` can be used as a simpler way to create a full
239    /// color scheme derived from a single seed color.
240    ///
241    /// For the color parameters that are nullable, it is still recommended
242    /// that applications provide values for them. They are only nullable due
243    /// to backwards compatibility concerns.
244    ///
245    /// If a color is not provided, the closest fallback color from the given
246    /// colors will be used for it (e.g. `primary_container` will default
247    /// to `primary`). Material Design 3 makes use of these colors for many
248    /// component defaults, so for the best results the application should
249    /// supply colors for all the parameters. An easy way to ensure this is to
250    /// use `ColorScheme::from_seed` to generate a full set of colors.
251    ///
252    /// During the migration to Material Design 3, if an app's
253    /// `ThemeData.useMaterial3` is false, then components will only
254    /// use the following colors for defaults:
255    ///
256    /// * `primary`
257    /// * `on_primary`
258    /// * `secondary`
259    /// * `on_secondary`
260    /// * `error`
261    /// * `on_error`
262    /// * `surface`
263    /// * `on_surface`
264    ///
265    /// DEPRECATED:
266    /// * `background`
267    /// * `on_background`
268    #[allow(clippy::too_many_arguments)]
269    pub fn new(
270        brightness: Brightness,
271        primary: Color,
272        on_primary: Color,
273        secondary: Color,
274        on_secondary: Color,
275        error: Color,
276        on_error: Color,
277        surface: Color,
278        on_surface: Color,
279    ) -> ColorScheme {
280        ColorScheme {
281            brightness,
282            primary,
283            on_primary,
284            primary_container: None,
285            on_primary_container: None,
286            primary_fixed: None,
287            primary_fixed_dim: None,
288            on_primary_fixed: None,
289            on_primary_fixed_variant: None,
290            secondary,
291            on_secondary,
292            secondary_container: None,
293            on_secondary_container: None,
294            secondary_fixed: None,
295            secondary_fixed_dim: None,
296            on_secondary_fixed: None,
297            on_secondary_fixed_variant: None,
298            tertiary: None,
299            on_tertiary: None,
300            tertiary_container: None,
301            on_tertiary_container: None,
302            tertiary_fixed: None,
303            tertiary_fixed_dim: None,
304            on_tertiary_fixed: None,
305            on_tertiary_fixed_variant: None,
306            error,
307            on_error,
308            error_container: None,
309            on_error_container: None,
310            surface,
311            on_surface,
312            surface_variant: None,
313            surface_dim: None,
314            surface_bright: None,
315            surface_container_lowest: None,
316            surface_container_low: None,
317            surface_container: None,
318            surface_container_high: None,
319            surface_container_highest: None,
320            on_surface_variant: None,
321            outline: None,
322            outline_variant: None,
323            shadow: None,
324            scrim: None,
325            inverse_surface: None,
326            on_inverse_surface: None,
327            inverse_primary: None,
328            surface_tint: None,
329            background: None,
330            on_background: None,
331        }
332    }
333
334    /// Dart `ColorScheme(brightness:)`.
335    pub fn with_brightness(mut self, brightness: Brightness) -> ColorScheme {
336        self.brightness = brightness;
337        self
338    }
339
340    /// Dart `ColorScheme(primary:)`.
341    pub fn with_primary(mut self, primary: Color) -> ColorScheme {
342        self.primary = primary;
343        self
344    }
345
346    /// Dart `ColorScheme(onPrimary:)`.
347    pub fn with_on_primary(mut self, on_primary: Color) -> ColorScheme {
348        self.on_primary = on_primary;
349        self
350    }
351
352    /// Dart `ColorScheme(primaryContainer:)`.
353    pub fn with_primary_container(mut self, primary_container: Color) -> ColorScheme {
354        self.primary_container = Some(primary_container);
355        self
356    }
357
358    /// Dart `ColorScheme(onPrimaryContainer:)`.
359    pub fn with_on_primary_container(mut self, on_primary_container: Color) -> ColorScheme {
360        self.on_primary_container = Some(on_primary_container);
361        self
362    }
363
364    /// Dart `ColorScheme(primaryFixed:)`.
365    pub fn with_primary_fixed(mut self, primary_fixed: Color) -> ColorScheme {
366        self.primary_fixed = Some(primary_fixed);
367        self
368    }
369
370    /// Dart `ColorScheme(primaryFixedDim:)`.
371    pub fn with_primary_fixed_dim(mut self, primary_fixed_dim: Color) -> ColorScheme {
372        self.primary_fixed_dim = Some(primary_fixed_dim);
373        self
374    }
375
376    /// Dart `ColorScheme(onPrimaryFixed:)`.
377    pub fn with_on_primary_fixed(mut self, on_primary_fixed: Color) -> ColorScheme {
378        self.on_primary_fixed = Some(on_primary_fixed);
379        self
380    }
381
382    /// Dart `ColorScheme(onPrimaryFixedVariant:)`.
383    pub fn with_on_primary_fixed_variant(mut self, on_primary_fixed_variant: Color) -> ColorScheme {
384        self.on_primary_fixed_variant = Some(on_primary_fixed_variant);
385        self
386    }
387
388    /// Dart `ColorScheme(secondary:)`.
389    pub fn with_secondary(mut self, secondary: Color) -> ColorScheme {
390        self.secondary = secondary;
391        self
392    }
393
394    /// Dart `ColorScheme(onSecondary:)`.
395    pub fn with_on_secondary(mut self, on_secondary: Color) -> ColorScheme {
396        self.on_secondary = on_secondary;
397        self
398    }
399
400    /// Dart `ColorScheme(secondaryContainer:)`.
401    pub fn with_secondary_container(mut self, secondary_container: Color) -> ColorScheme {
402        self.secondary_container = Some(secondary_container);
403        self
404    }
405
406    /// Dart `ColorScheme(onSecondaryContainer:)`.
407    pub fn with_on_secondary_container(mut self, on_secondary_container: Color) -> ColorScheme {
408        self.on_secondary_container = Some(on_secondary_container);
409        self
410    }
411
412    /// Dart `ColorScheme(secondaryFixed:)`.
413    pub fn with_secondary_fixed(mut self, secondary_fixed: Color) -> ColorScheme {
414        self.secondary_fixed = Some(secondary_fixed);
415        self
416    }
417
418    /// Dart `ColorScheme(secondaryFixedDim:)`.
419    pub fn with_secondary_fixed_dim(mut self, secondary_fixed_dim: Color) -> ColorScheme {
420        self.secondary_fixed_dim = Some(secondary_fixed_dim);
421        self
422    }
423
424    /// Dart `ColorScheme(onSecondaryFixed:)`.
425    pub fn with_on_secondary_fixed(mut self, on_secondary_fixed: Color) -> ColorScheme {
426        self.on_secondary_fixed = Some(on_secondary_fixed);
427        self
428    }
429
430    /// Dart `ColorScheme(onSecondaryFixedVariant:)`.
431    pub fn with_on_secondary_fixed_variant(
432        mut self,
433        on_secondary_fixed_variant: Color,
434    ) -> ColorScheme {
435        self.on_secondary_fixed_variant = Some(on_secondary_fixed_variant);
436        self
437    }
438
439    /// Dart `ColorScheme(tertiary:)`.
440    pub fn with_tertiary(mut self, tertiary: Color) -> ColorScheme {
441        self.tertiary = Some(tertiary);
442        self
443    }
444
445    /// Dart `ColorScheme(onTertiary:)`.
446    pub fn with_on_tertiary(mut self, on_tertiary: Color) -> ColorScheme {
447        self.on_tertiary = Some(on_tertiary);
448        self
449    }
450
451    /// Dart `ColorScheme(tertiaryContainer:)`.
452    pub fn with_tertiary_container(mut self, tertiary_container: Color) -> ColorScheme {
453        self.tertiary_container = Some(tertiary_container);
454        self
455    }
456
457    /// Dart `ColorScheme(onTertiaryContainer:)`.
458    pub fn with_on_tertiary_container(mut self, on_tertiary_container: Color) -> ColorScheme {
459        self.on_tertiary_container = Some(on_tertiary_container);
460        self
461    }
462
463    /// Dart `ColorScheme(tertiaryFixed:)`.
464    pub fn with_tertiary_fixed(mut self, tertiary_fixed: Color) -> ColorScheme {
465        self.tertiary_fixed = Some(tertiary_fixed);
466        self
467    }
468
469    /// Dart `ColorScheme(tertiaryFixedDim:)`.
470    pub fn with_tertiary_fixed_dim(mut self, tertiary_fixed_dim: Color) -> ColorScheme {
471        self.tertiary_fixed_dim = Some(tertiary_fixed_dim);
472        self
473    }
474
475    /// Dart `ColorScheme(onTertiaryFixed:)`.
476    pub fn with_on_tertiary_fixed(mut self, on_tertiary_fixed: Color) -> ColorScheme {
477        self.on_tertiary_fixed = Some(on_tertiary_fixed);
478        self
479    }
480
481    /// Dart `ColorScheme(onTertiaryFixedVariant:)`.
482    pub fn with_on_tertiary_fixed_variant(
483        mut self,
484        on_tertiary_fixed_variant: Color,
485    ) -> ColorScheme {
486        self.on_tertiary_fixed_variant = Some(on_tertiary_fixed_variant);
487        self
488    }
489
490    /// Dart `ColorScheme(error:)`.
491    pub fn with_error(mut self, error: Color) -> ColorScheme {
492        self.error = error;
493        self
494    }
495
496    /// Dart `ColorScheme(onError:)`.
497    pub fn with_on_error(mut self, on_error: Color) -> ColorScheme {
498        self.on_error = on_error;
499        self
500    }
501
502    /// Dart `ColorScheme(errorContainer:)`.
503    pub fn with_error_container(mut self, error_container: Color) -> ColorScheme {
504        self.error_container = Some(error_container);
505        self
506    }
507
508    /// Dart `ColorScheme(onErrorContainer:)`.
509    pub fn with_on_error_container(mut self, on_error_container: Color) -> ColorScheme {
510        self.on_error_container = Some(on_error_container);
511        self
512    }
513
514    /// Dart `ColorScheme(surface:)`.
515    pub fn with_surface(mut self, surface: Color) -> ColorScheme {
516        self.surface = surface;
517        self
518    }
519
520    /// Dart `ColorScheme(onSurface:)`.
521    pub fn with_on_surface(mut self, on_surface: Color) -> ColorScheme {
522        self.on_surface = on_surface;
523        self
524    }
525
526    /// Dart `ColorScheme(surfaceDim:)`.
527    pub fn with_surface_dim(mut self, surface_dim: Color) -> ColorScheme {
528        self.surface_dim = Some(surface_dim);
529        self
530    }
531
532    /// Dart `ColorScheme(surfaceBright:)`.
533    pub fn with_surface_bright(mut self, surface_bright: Color) -> ColorScheme {
534        self.surface_bright = Some(surface_bright);
535        self
536    }
537
538    /// Dart `ColorScheme(surfaceContainerLowest:)`.
539    pub fn with_surface_container_lowest(mut self, surface_container_lowest: Color) -> ColorScheme {
540        self.surface_container_lowest = Some(surface_container_lowest);
541        self
542    }
543
544    /// Dart `ColorScheme(surfaceContainerLow:)`.
545    pub fn with_surface_container_low(mut self, surface_container_low: Color) -> ColorScheme {
546        self.surface_container_low = Some(surface_container_low);
547        self
548    }
549
550    /// Dart `ColorScheme(surfaceContainer:)`.
551    pub fn with_surface_container(mut self, surface_container: Color) -> ColorScheme {
552        self.surface_container = Some(surface_container);
553        self
554    }
555
556    /// Dart `ColorScheme(surfaceContainerHigh:)`.
557    pub fn with_surface_container_high(mut self, surface_container_high: Color) -> ColorScheme {
558        self.surface_container_high = Some(surface_container_high);
559        self
560    }
561
562    /// Dart `ColorScheme(surfaceContainerHighest:)`.
563    pub fn with_surface_container_highest(
564        mut self,
565        surface_container_highest: Color,
566    ) -> ColorScheme {
567        self.surface_container_highest = Some(surface_container_highest);
568        self
569    }
570
571    /// Dart `ColorScheme(onSurfaceVariant:)`.
572    pub fn with_on_surface_variant(mut self, on_surface_variant: Color) -> ColorScheme {
573        self.on_surface_variant = Some(on_surface_variant);
574        self
575    }
576
577    /// Dart `ColorScheme(outline:)`.
578    pub fn with_outline(mut self, outline: Color) -> ColorScheme {
579        self.outline = Some(outline);
580        self
581    }
582
583    /// Dart `ColorScheme(outlineVariant:)`.
584    pub fn with_outline_variant(mut self, outline_variant: Color) -> ColorScheme {
585        self.outline_variant = Some(outline_variant);
586        self
587    }
588
589    /// Dart `ColorScheme(shadow:)`.
590    pub fn with_shadow(mut self, shadow: Color) -> ColorScheme {
591        self.shadow = Some(shadow);
592        self
593    }
594
595    /// Dart `ColorScheme(scrim:)`.
596    pub fn with_scrim(mut self, scrim: Color) -> ColorScheme {
597        self.scrim = Some(scrim);
598        self
599    }
600
601    /// Dart `ColorScheme(inverseSurface:)`.
602    pub fn with_inverse_surface(mut self, inverse_surface: Color) -> ColorScheme {
603        self.inverse_surface = Some(inverse_surface);
604        self
605    }
606
607    /// Dart `ColorScheme(onInverseSurface:)`.
608    pub fn with_on_inverse_surface(mut self, on_inverse_surface: Color) -> ColorScheme {
609        self.on_inverse_surface = Some(on_inverse_surface);
610        self
611    }
612
613    /// Dart `ColorScheme(inversePrimary:)`.
614    pub fn with_inverse_primary(mut self, inverse_primary: Color) -> ColorScheme {
615        self.inverse_primary = Some(inverse_primary);
616        self
617    }
618
619    /// Dart `ColorScheme(surfaceTint:)`.
620    pub fn with_surface_tint(mut self, surface_tint: Color) -> ColorScheme {
621        self.surface_tint = Some(surface_tint);
622        self
623    }
624
625    #[deprecated(note = "Use surface instead. This feature was deprecated after v3.18.0-0.1.pre.")]
626    /// Dart `ColorScheme(background:)`.
627    pub fn with_background(mut self, background: Color) -> ColorScheme {
628        self.background = Some(background);
629        self
630    }
631
632    #[deprecated(
633        note = "Use onSurface instead. This feature was deprecated after v3.18.0-0.1.pre."
634    )]
635    /// Dart `ColorScheme(onBackground:)`.
636    pub fn with_on_background(mut self, on_background: Color) -> ColorScheme {
637        self.on_background = Some(on_background);
638        self
639    }
640
641    #[deprecated(
642        note = "Use surfaceContainerHighest instead. This feature was deprecated after v3.18.0-0.1.pre."
643    )]
644    /// Dart `ColorScheme(surfaceVariant:)`.
645    pub fn with_surface_variant(mut self, surface_variant: Color) -> ColorScheme {
646        self.surface_variant = Some(surface_variant);
647        self
648    }
649
650    /// Generate a `ColorScheme` derived from the given `seedColor`.
651    ///
652    /// Using the `seedColor` as a starting point, a set of tonal palettes are
653    /// constructed. By default, the tonal palettes are based on the Material 3
654    /// Color system and provide all of the `ColorScheme` colors. These colors are
655    /// designed to work well together and meet contrast requirements for
656    /// accessibility.
657    ///
658    /// If any of the optional color parameters are non-null they will be
659    /// used in place of the generated colors for that field in the resulting
660    /// color scheme. This allows apps to override specific colors for their
661    /// needs.
662    ///
663    /// Given the nature of the algorithm, the `seedColor` may not wind up as
664    /// one of the ColorScheme colors.
665    ///
666    /// The `dynamicSchemeVariant` parameter creates different types of
667    /// `DynamicScheme`s, which are used to generate different styles of `ColorScheme`s.
668    /// By default, `dynamicSchemeVariant` is set to `tonalSpot`. A `ColorScheme`
669    /// constructed by `dynamicSchemeVariant.tonalSpot` has pastel palettes and
670    /// won't be too "colorful" even if the `seedColor` has a high chroma value.
671    /// If the resulting color scheme is too dark, consider setting `dynamicSchemeVariant`
672    /// to `DynamicSchemeVariant.fidelity`, whose palettes match the seed color.
673    ///
674    /// The `contrastLevel` parameter indicates the contrast level between color
675    /// pairs, such as `primary` and `on_primary`. 0.0 is the default (normal);
676    /// -1.0 is the lowest; 1.0 is the highest. From Material Design guideline, the
677    /// medium and high contrast correspond to 0.5 and 1.0 respectively.
678    ///
679    /// <callout-box>
680    ///
681    /// This sample shows how to use `ColorScheme::from_seed` to create dynamic
682    /// color schemes with different `DynamicSchemeVariant`s and different
683    /// contrast level.
684    ///
685    // TODO(framework): Replace the following block with a @dartpad directive
686    // when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
687    /// {@macro material_ui.dartpad_guide}
688    ///
689    /// {@example /example/lib/color_scheme/color_scheme.0.dart#body}
690    ///
691    /// </callout-box>
692    ///
693    /// See also:
694    ///
695    ///  * <https://m3.material.io/styles/color/the-color-system/color-roles>, the
696    ///    Material 3 Color system specification.
697    ///  * <https://pub.dev/packages/material_color_utilities>, the package
698    ///    used to generate the tonal palettes needed for the scheme.
699    ///
700    /// The builder takes `brightness`, `dynamic_scheme_variant` and `contrast_level`; Dart's
701    /// colour overrides are the `with_*` setters on the built scheme, since each replaces
702    /// only its own colour.
703    pub fn from_seed(seed_color: Color) -> ColorSchemeFromSeedBuilder {
704        ColorSchemeFromSeedBuilder {
705            seed_color,
706            brightness: Brightness::Light,
707            dynamic_scheme_variant: DynamicSchemeVariant::TonalSpot,
708            contrast_level: 0.0,
709        }
710    }
711
712    /// Create a light ColorScheme based on a purple primary color that matches the
713    /// [baseline Material 2 color scheme](https://material.io/design/color/the-color-system.html#color-theme-creation).
714    ///
715    /// This constructor shouldn't be used to update the Material 3 color scheme.
716    ///
717    /// For Material 3, use `ColorScheme::from_seed` to create a color scheme
718    /// from a single seed color based on the Material 3 color system.
719    ///
720    /// <callout-box>
721    ///
722    // TODO(framework): Add unit tests to this code snippet.
723    // https://github.com/flutter/flutter/issues/188530
724    ///
725    /// This example demonstrates how to create a color scheme similar to `ColorScheme::light`
726    /// using the `ColorScheme::from_seed` constructor:
727    ///
728    /// ```dart
729    /// colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xff6200ee)).copyWith(
730    ///   primaryContainer: const Color(0xff6200ee),
731    ///   onPrimaryContainer: Colors.white,
732    ///   secondaryContainer: const Color(0xff03dac6),
733    ///   onSecondaryContainer: Colors.black,
734    ///   error: const Color(0xffb00020),
735    ///   onError: Colors.white,
736    /// ),
737    /// ```
738    ///
739    /// </callout-box>
740    pub fn light() -> ColorScheme {
741        ColorScheme {
742            brightness: Brightness::Light,
743            primary: Color::new(0xFF6200EE),
744            on_primary: Colors::WHITE,
745            primary_container: None,
746            on_primary_container: None,
747            primary_fixed: None,
748            primary_fixed_dim: None,
749            on_primary_fixed: None,
750            on_primary_fixed_variant: None,
751            secondary: Color::new(0xFF03DAC6),
752            on_secondary: Colors::BLACK,
753            secondary_container: None,
754            on_secondary_container: None,
755            secondary_fixed: None,
756            secondary_fixed_dim: None,
757            on_secondary_fixed: None,
758            on_secondary_fixed_variant: None,
759            tertiary: None,
760            on_tertiary: None,
761            tertiary_container: None,
762            on_tertiary_container: None,
763            tertiary_fixed: None,
764            tertiary_fixed_dim: None,
765            on_tertiary_fixed: None,
766            on_tertiary_fixed_variant: None,
767            error: Color::new(0xFFB00020),
768            on_error: Colors::WHITE,
769            error_container: None,
770            on_error_container: None,
771            surface: Colors::WHITE,
772            on_surface: Colors::BLACK,
773            surface_variant: None,
774            surface_dim: None,
775            surface_bright: None,
776            surface_container_lowest: None,
777            surface_container_low: None,
778            surface_container: None,
779            surface_container_high: None,
780            surface_container_highest: None,
781            on_surface_variant: None,
782            outline: None,
783            outline_variant: None,
784            shadow: None,
785            scrim: None,
786            inverse_surface: None,
787            on_inverse_surface: None,
788            inverse_primary: None,
789            surface_tint: None,
790            background: Some(Colors::WHITE),
791            on_background: Some(Colors::BLACK),
792        }
793    }
794
795    /// Create the dark color scheme that matches the
796    /// [baseline Material 2 color scheme](https://material.io/design/color/dark-theme.html#ui-application).
797    ///
798    /// This constructor shouldn't be used to update the Material 3 color scheme.
799    ///
800    /// For Material 3, use `ColorScheme::from_seed` to create a color scheme
801    /// from a single seed color based on the Material 3 color system.
802    /// Override the `brightness` property of `ColorScheme::from_seed` to create a
803    /// dark color scheme.
804    ///
805    /// <callout-box>
806    ///
807    // TODO(framework): Add unit tests to this code snippet.
808    // https://github.com/flutter/flutter/issues/188530
809    ///
810    /// This example demonstrates how to create a color scheme similar to `ColorScheme::dark`
811    /// using the `ColorScheme::from_seed` constructor:
812    ///
813    /// ```dart
814    /// colorScheme: ColorScheme.fromSeed(
815    ///   seedColor: const Color(0xffbb86fc),
816    ///   brightness: Brightness.dark,
817    /// ).copyWith(
818    ///   primaryContainer: const Color(0xffbb86fc),
819    ///   onPrimaryContainer: Colors.black,
820    ///   secondaryContainer: const Color(0xff03dac6),
821    ///   onSecondaryContainer: Colors.black,
822    ///   error: const Color(0xffcf6679),
823    ///   onError: Colors.black,
824    /// ),
825    /// ```
826    ///
827    /// </callout-box>
828    pub fn dark() -> ColorScheme {
829        ColorScheme {
830            brightness: Brightness::Dark,
831            primary: Color::new(0xFFBB86FC),
832            on_primary: Colors::BLACK,
833            primary_container: None,
834            on_primary_container: None,
835            primary_fixed: None,
836            primary_fixed_dim: None,
837            on_primary_fixed: None,
838            on_primary_fixed_variant: None,
839            secondary: Color::new(0xFF03DAC6),
840            on_secondary: Colors::BLACK,
841            secondary_container: None,
842            on_secondary_container: None,
843            secondary_fixed: None,
844            secondary_fixed_dim: None,
845            on_secondary_fixed: None,
846            on_secondary_fixed_variant: None,
847            tertiary: None,
848            on_tertiary: None,
849            tertiary_container: None,
850            on_tertiary_container: None,
851            tertiary_fixed: None,
852            tertiary_fixed_dim: None,
853            on_tertiary_fixed: None,
854            on_tertiary_fixed_variant: None,
855            error: Color::new(0xFFCF6679),
856            on_error: Colors::BLACK,
857            error_container: None,
858            on_error_container: None,
859            surface: Color::new(0xFF121212),
860            on_surface: Colors::WHITE,
861            surface_variant: None,
862            surface_dim: None,
863            surface_bright: None,
864            surface_container_lowest: None,
865            surface_container_low: None,
866            surface_container: None,
867            surface_container_high: None,
868            surface_container_highest: None,
869            on_surface_variant: None,
870            outline: None,
871            outline_variant: None,
872            shadow: None,
873            scrim: None,
874            inverse_surface: None,
875            on_inverse_surface: None,
876            inverse_primary: None,
877            surface_tint: None,
878            background: Some(Color::new(0xFF121212)),
879            on_background: Some(Colors::WHITE),
880        }
881    }
882
883    /// Create a high contrast ColorScheme based on a purple primary color that
884    /// matches the [baseline Material 2 color scheme](https://material.io/design/color/the-color-system.html#color-theme-creation).
885    ///
886    /// This constructor shouldn't be used to update the Material 3 color scheme.
887    ///
888    /// For Material 3, use `ColorScheme::from_seed` to create a color scheme
889    /// from a single seed color based on the Material 3 color system. To create a
890    /// high-contrast color scheme, set `contrastLevel` to 1.0.
891    ///
892    /// <callout-box>
893    ///
894    // TODO(framework): Add unit tests to this code snippet.
895    // https://github.com/flutter/flutter/issues/188530
896    ///
897    /// This example demonstrates how to create a color scheme similar to `ColorScheme::high_contrast_light`
898    /// using the `ColorScheme::from_seed` constructor:
899    ///
900    /// ```dart
901    /// colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xff0000ba)).copyWith(
902    ///   primaryContainer: const Color(0xff0000ba),
903    ///   onPrimaryContainer: Colors.white,
904    ///   secondaryContainer: const Color(0xff66fff9),
905    ///   onSecondaryContainer: Colors.black,
906    ///   error: const Color(0xff790000),
907    ///   onError: Colors.white,
908    /// ),
909    /// ```
910    ///
911    /// </callout-box>
912    pub fn high_contrast_light() -> ColorScheme {
913        ColorScheme {
914            brightness: Brightness::Light,
915            primary: Color::new(0xFF0000BA),
916            on_primary: Colors::WHITE,
917            primary_container: None,
918            on_primary_container: None,
919            primary_fixed: None,
920            primary_fixed_dim: None,
921            on_primary_fixed: None,
922            on_primary_fixed_variant: None,
923            secondary: Color::new(0xFF66FFF9),
924            on_secondary: Colors::BLACK,
925            secondary_container: None,
926            on_secondary_container: None,
927            secondary_fixed: None,
928            secondary_fixed_dim: None,
929            on_secondary_fixed: None,
930            on_secondary_fixed_variant: None,
931            tertiary: None,
932            on_tertiary: None,
933            tertiary_container: None,
934            on_tertiary_container: None,
935            tertiary_fixed: None,
936            tertiary_fixed_dim: None,
937            on_tertiary_fixed: None,
938            on_tertiary_fixed_variant: None,
939            error: Color::new(0xFF790000),
940            on_error: Colors::WHITE,
941            error_container: None,
942            on_error_container: None,
943            surface: Colors::WHITE,
944            on_surface: Colors::BLACK,
945            surface_variant: None,
946            surface_dim: None,
947            surface_bright: None,
948            surface_container_lowest: None,
949            surface_container_low: None,
950            surface_container: None,
951            surface_container_high: None,
952            surface_container_highest: None,
953            on_surface_variant: None,
954            outline: None,
955            outline_variant: None,
956            shadow: None,
957            scrim: None,
958            inverse_surface: None,
959            on_inverse_surface: None,
960            inverse_primary: None,
961            surface_tint: None,
962            background: Some(Colors::WHITE),
963            on_background: Some(Colors::BLACK),
964        }
965    }
966
967    /// Create a high contrast ColorScheme based on the dark
968    /// [baseline Material 2 color scheme](https://material.io/design/color/dark-theme.html#ui-application).
969    ///
970    /// This constructor shouldn't be used to update the Material 3 color scheme.
971    ///
972    /// For Material 3, use `ColorScheme::from_seed` to create a color scheme
973    /// from a single seed color based on the Material 3 color system.
974    /// Override the `brightness` property of `ColorScheme::from_seed` to create a
975    /// dark color scheme. To create a high-contrast color scheme, set
976    /// `contrastLevel` to 1.0.
977    ///
978    /// <callout-box>
979    ///
980    // TODO(framework): Add unit tests to this code snippet.
981    // https://github.com/flutter/flutter/issues/188530
982    ///
983    /// This example demonstrates how to create a color scheme similar to `ColorScheme::high_contrast_dark`
984    /// using the `ColorScheme::from_seed` constructor:
985    ///
986    /// ```dart
987    /// colorScheme: ColorScheme.fromSeed(
988    ///   seedColor: const Color(0xffefb7ff),
989    ///   brightness: Brightness.dark,
990    /// ).copyWith(
991    ///   primaryContainer: const Color(0xffefb7ff),
992    ///   onPrimaryContainer: Colors.black,
993    ///   secondaryContainer: const Color(0xff66fff9),
994    ///   onSecondaryContainer: Colors.black,
995    ///   error: const Color(0xff9b374d),
996    ///   onError: Colors.white,
997    /// ),
998    /// ```
999    ///
1000    /// </callout-box>
1001    pub fn high_contrast_dark() -> ColorScheme {
1002        ColorScheme {
1003            brightness: Brightness::Dark,
1004            primary: Color::new(0xFFEFB7FF),
1005            on_primary: Colors::BLACK,
1006            primary_container: None,
1007            on_primary_container: None,
1008            primary_fixed: None,
1009            primary_fixed_dim: None,
1010            on_primary_fixed: None,
1011            on_primary_fixed_variant: None,
1012            secondary: Color::new(0xFF66FFF9),
1013            on_secondary: Colors::BLACK,
1014            secondary_container: None,
1015            on_secondary_container: None,
1016            secondary_fixed: None,
1017            secondary_fixed_dim: None,
1018            on_secondary_fixed: None,
1019            on_secondary_fixed_variant: None,
1020            tertiary: None,
1021            on_tertiary: None,
1022            tertiary_container: None,
1023            on_tertiary_container: None,
1024            tertiary_fixed: None,
1025            tertiary_fixed_dim: None,
1026            on_tertiary_fixed: None,
1027            on_tertiary_fixed_variant: None,
1028            error: Color::new(0xFF9B374D),
1029            on_error: Colors::BLACK,
1030            error_container: None,
1031            on_error_container: None,
1032            surface: Color::new(0xFF121212),
1033            on_surface: Colors::WHITE,
1034            surface_variant: None,
1035            surface_dim: None,
1036            surface_bright: None,
1037            surface_container_lowest: None,
1038            surface_container_low: None,
1039            surface_container: None,
1040            surface_container_high: None,
1041            surface_container_highest: None,
1042            on_surface_variant: None,
1043            outline: None,
1044            outline_variant: None,
1045            shadow: None,
1046            scrim: None,
1047            inverse_surface: None,
1048            on_inverse_surface: None,
1049            inverse_primary: None,
1050            surface_tint: None,
1051            background: Some(Color::new(0xFF121212)),
1052            on_background: Some(Colors::WHITE),
1053        }
1054    }
1055
1056    /// Creates a color scheme from a `MaterialColor` swatch.
1057    ///
1058    /// In Material 3, this constructor is ignored by `ThemeData` when creating
1059    /// its default color scheme. Instead, `ThemeData` uses `ColorScheme::from_seed`
1060    /// to create its default color scheme. This constructor shouldn't be used
1061    /// to update the Material 3 color scheme. It will be phased out gradually;
1062    /// see https://github.com/flutter/flutter/issues/120064 for more details.
1063    ///
1064    /// If `ThemeData.useMaterial3` is false, then this constructor is used by
1065    /// `ThemeData` to create its default color scheme.
1066    pub fn from_swatch() -> ColorSchemeFromSwatchBuilder {
1067        ColorSchemeFromSwatchBuilder {
1068            primary_swatch: Colors::BLUE,
1069            accent_color: None,
1070            card_color: None,
1071            background_color: None,
1072            error_color: None,
1073            brightness: Brightness::Light,
1074        }
1075    }
1076
1077    fn brightness_for(color: Color) -> Brightness {
1078        ThemeData::estimate_brightness_for_color(color)
1079    }
1080
1081    /// The [`Brightness`] of this scheme.
1082    pub fn brightness(&self) -> Brightness {
1083        self.brightness
1084    }
1085
1086    /// The color displayed most frequently across your app’s screens and components.
1087    pub fn primary(&self) -> Color {
1088        self.primary
1089    }
1090
1091    /// A color that's clearly legible when drawn on [`primary`](Self::primary).
1092    pub fn on_primary(&self) -> Color {
1093        self.on_primary
1094    }
1095
1096    /// A color used for elements needing less emphasis than `primary`.
1097    pub fn primary_container(&self) -> Color {
1098        self.primary_container.unwrap_or(self.primary)
1099    }
1100
1101    /// A color that's clearly legible when drawn on `primary_container`.
1102    ///
1103    /// To ensure that an app is accessible, a contrast ratio between
1104    /// `primary_container` and `on_primary_container` of at least 4.5:1
1105    /// is recommended. See
1106    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1107    pub fn on_primary_container(&self) -> Color {
1108        self.on_primary_container.unwrap_or(self.on_primary)
1109    }
1110
1111    /// A substitute for `primary_container` that's the same color for the dark
1112    /// and light themes.
1113    pub fn primary_fixed(&self) -> Color {
1114        self.primary_fixed.unwrap_or(self.primary)
1115    }
1116
1117    /// A color used for elements needing more emphasis than `primary_fixed`.
1118    pub fn primary_fixed_dim(&self) -> Color {
1119        self.primary_fixed_dim.unwrap_or(self.primary)
1120    }
1121
1122    /// A color that is used for text and icons that exist on top of elements having
1123    /// `primary_fixed` color.
1124    pub fn on_primary_fixed(&self) -> Color {
1125        self.on_primary_fixed.unwrap_or(self.on_primary)
1126    }
1127
1128    /// A color that provides a lower-emphasis option for text and icons than
1129    /// `on_primary_fixed`.
1130    pub fn on_primary_fixed_variant(&self) -> Color {
1131        self.on_primary_fixed_variant.unwrap_or(self.on_primary)
1132    }
1133
1134    /// An accent color used for less prominent components in the UI, such as filter chips,
1135    /// while expanding the opportunity for color expression.
1136    pub fn secondary(&self) -> Color {
1137        self.secondary
1138    }
1139
1140    /// A color that's clearly legible when drawn on [`secondary`](Self::secondary).
1141    pub fn on_secondary(&self) -> Color {
1142        self.on_secondary
1143    }
1144
1145    /// A color used for elements needing less emphasis than `secondary`.
1146    pub fn secondary_container(&self) -> Color {
1147        self.secondary_container.unwrap_or(self.secondary)
1148    }
1149
1150    /// A color that's clearly legible when drawn on `secondary_container`.
1151    ///
1152    /// To ensure that an app is accessible, a contrast ratio between
1153    /// `secondary_container` and `on_secondary_container` of at least 4.5:1 is
1154    /// recommended. See
1155    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1156    pub fn on_secondary_container(&self) -> Color {
1157        self.on_secondary_container.unwrap_or(self.on_secondary)
1158    }
1159
1160    /// A substitute for `secondary_container` that's the same color for the dark
1161    /// and light themes.
1162    pub fn secondary_fixed(&self) -> Color {
1163        self.secondary_fixed.unwrap_or(self.secondary)
1164    }
1165
1166    /// A color used for elements needing more emphasis than `secondary_fixed`.
1167    pub fn secondary_fixed_dim(&self) -> Color {
1168        self.secondary_fixed_dim.unwrap_or(self.secondary)
1169    }
1170
1171    /// A color that is used for text and icons that exist on top of elements having
1172    /// `secondary_fixed` color.
1173    pub fn on_secondary_fixed(&self) -> Color {
1174        self.on_secondary_fixed.unwrap_or(self.on_secondary)
1175    }
1176
1177    /// A color that provides a lower-emphasis option for text and icons than
1178    /// `on_secondary_fixed`.
1179    pub fn on_secondary_fixed_variant(&self) -> Color {
1180        self.on_secondary_fixed_variant.unwrap_or(self.on_secondary)
1181    }
1182
1183    /// A color used as a contrasting accent that can balance `primary`
1184    /// and `secondary` colors or bring heightened attention to an element,
1185    /// such as an input field.
1186    pub fn tertiary(&self) -> Color {
1187        self.tertiary.unwrap_or(self.secondary)
1188    }
1189
1190    /// A color that's clearly legible when drawn on `tertiary`.
1191    ///
1192    /// To ensure that an app is accessible, a contrast ratio between
1193    /// `tertiary` and `on_tertiary` of at least 4.5:1 is recommended. See
1194    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1195    pub fn on_tertiary(&self) -> Color {
1196        self.on_tertiary.unwrap_or(self.on_secondary)
1197    }
1198
1199    /// A color used for elements needing less emphasis than `tertiary`.
1200    pub fn tertiary_container(&self) -> Color {
1201        self.tertiary_container.unwrap_or_else(|| self.tertiary())
1202    }
1203
1204    /// A color that's clearly legible when drawn on `tertiary_container`.
1205    ///
1206    /// To ensure that an app is accessible, a contrast ratio between
1207    /// `tertiary_container` and `on_tertiary_container` of at least 4.5:1 is
1208    /// recommended. See
1209    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1210    pub fn on_tertiary_container(&self) -> Color {
1211        self.on_tertiary_container
1212            .unwrap_or_else(|| self.on_tertiary())
1213    }
1214
1215    /// A substitute for `tertiary_container` that's the same color for dark
1216    /// and light themes.
1217    pub fn tertiary_fixed(&self) -> Color {
1218        self.tertiary_fixed.unwrap_or_else(|| self.tertiary())
1219    }
1220
1221    /// A color used for elements needing more emphasis than `tertiary_fixed`.
1222    pub fn tertiary_fixed_dim(&self) -> Color {
1223        self.tertiary_fixed_dim.unwrap_or_else(|| self.tertiary())
1224    }
1225
1226    /// A color that is used for text and icons that exist on top of elements having
1227    /// `tertiary_fixed` color.
1228    pub fn on_tertiary_fixed(&self) -> Color {
1229        self.on_tertiary_fixed.unwrap_or_else(|| self.on_tertiary())
1230    }
1231
1232    /// A color that provides a lower-emphasis option for text and icons than
1233    /// `on_tertiary_fixed`.
1234    pub fn on_tertiary_fixed_variant(&self) -> Color {
1235        self.on_tertiary_fixed_variant
1236            .unwrap_or_else(|| self.on_tertiary())
1237    }
1238
1239    /// The color to use for input validation errors, e.g. for an `InputDecoration`'s error text.
1240    pub fn error(&self) -> Color {
1241        self.error
1242    }
1243
1244    /// A color that's clearly legible when drawn on [`error`](Self::error).
1245    pub fn on_error(&self) -> Color {
1246        self.on_error
1247    }
1248
1249    /// A color used for error elements needing less emphasis than `error`.
1250    pub fn error_container(&self) -> Color {
1251        self.error_container.unwrap_or(self.error)
1252    }
1253
1254    /// A color that's clearly legible when drawn on `error_container`.
1255    ///
1256    /// To ensure that an app is accessible, a contrast ratio between
1257    /// `error_container` and `on_error_container` of at least 4.5:1 is
1258    /// recommended. See
1259    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1260    pub fn on_error_container(&self) -> Color {
1261        self.on_error_container.unwrap_or(self.on_error)
1262    }
1263
1264    /// The background color for widgets like `Scaffold`.
1265    pub fn surface(&self) -> Color {
1266        self.surface
1267    }
1268
1269    /// A color that's clearly legible when drawn on [`surface`](Self::surface).
1270    pub fn on_surface(&self) -> Color {
1271        self.on_surface
1272    }
1273
1274    /// A color variant of `surface` that can be used for differentiation against
1275    /// a component using `surface`.
1276    #[deprecated(
1277        note = "Use surfaceContainerHighest instead. This feature was deprecated after v3.18.0-0.1.pre."
1278    )]
1279    pub fn surface_variant(&self) -> Color {
1280        self.surface_variant.unwrap_or(self.surface)
1281    }
1282
1283    /// A color that's always darkest in the dark or light theme.
1284    pub fn surface_dim(&self) -> Color {
1285        self.surface_dim.unwrap_or(self.surface)
1286    }
1287
1288    /// A color that's always the lightest in the dark or light theme.
1289    pub fn surface_bright(&self) -> Color {
1290        self.surface_bright.unwrap_or(self.surface)
1291    }
1292
1293    /// A surface container color with the lightest tone and the least emphasis
1294    /// relative to the surface.
1295    pub fn surface_container_lowest(&self) -> Color {
1296        self.surface_container_lowest.unwrap_or(self.surface)
1297    }
1298
1299    /// A surface container color with a lighter tone that creates less emphasis
1300    /// than `surface_container` but more emphasis than `surface_container_lowest`.
1301    pub fn surface_container_low(&self) -> Color {
1302        self.surface_container_low.unwrap_or(self.surface)
1303    }
1304
1305    /// A recommended color role for a distinct area within the surface.
1306    ///
1307    /// Surface container color roles are independent of elevation. They replace the old
1308    /// opacity-based model which applied a tinted overlay on top of
1309    /// surfaces based on their elevation.
1310    ///
1311    /// Surface container colors include `surface_container_lowest`, `surface_container_low`,
1312    /// `surface_container`, `surface_container_high` and `surface_container_highest`.
1313    pub fn surface_container(&self) -> Color {
1314        self.surface_container.unwrap_or(self.surface)
1315    }
1316
1317    /// A surface container color with a darker tone. It is used to create more
1318    /// emphasis than `surface_container` but less emphasis than `surface_container_highest`.
1319    pub fn surface_container_high(&self) -> Color {
1320        self.surface_container_high.unwrap_or(self.surface)
1321    }
1322
1323    /// A surface container color with the darkest tone. It is used to create the
1324    /// most emphasis against the surface.
1325    pub fn surface_container_highest(&self) -> Color {
1326        self.surface_container_highest.unwrap_or(self.surface)
1327    }
1328
1329    /// A color that's clearly legible when drawn on `surface_variant`.
1330    ///
1331    /// To ensure that an app is accessible, a contrast ratio between
1332    /// `surface_variant` and `on_surface_variant` of at least 4.5:1 is
1333    /// recommended. See
1334    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1335    pub fn on_surface_variant(&self) -> Color {
1336        self.on_surface_variant.unwrap_or(self.on_surface)
1337    }
1338
1339    /// A utility color that creates boundaries and emphasis to improve usability.
1340    #[allow(deprecated)]
1341    pub fn outline(&self) -> Color {
1342        self.outline.unwrap_or_else(|| self.on_background())
1343    }
1344
1345    /// A utility color that creates boundaries for decorative elements when a
1346    /// 3:1 contrast isn’t required, such as for dividers or decorative elements.
1347    #[allow(deprecated)]
1348    pub fn outline_variant(&self) -> Color {
1349        self.outline_variant.unwrap_or_else(|| self.on_background())
1350    }
1351
1352    /// A color use to paint the drop shadows of elevated components.
1353    pub fn shadow(&self) -> Color {
1354        self.shadow.unwrap_or(Color::new(0xFF000000))
1355    }
1356
1357    /// A color use to paint the scrim around of modal components.
1358    pub fn scrim(&self) -> Color {
1359        self.scrim.unwrap_or(Color::new(0xFF000000))
1360    }
1361
1362    /// A surface color used for displaying the reverse of what’s seen in the
1363    /// surrounding UI, for example in a SnackBar to bring attention to
1364    /// an alert.
1365    pub fn inverse_surface(&self) -> Color {
1366        self.inverse_surface.unwrap_or(self.on_surface)
1367    }
1368
1369    /// A color that's clearly legible when drawn on `inverse_surface`.
1370    ///
1371    /// To ensure that an app is accessible, a contrast ratio between
1372    /// `inverse_surface` and `on_inverse_surface` of at least 4.5:1 is
1373    /// recommended. See
1374    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1375    pub fn on_inverse_surface(&self) -> Color {
1376        self.on_inverse_surface.unwrap_or(self.surface)
1377    }
1378
1379    /// An accent color used for displaying a highlight color on `inverse_surface`
1380    /// backgrounds, like button text in a SnackBar.
1381    pub fn inverse_primary(&self) -> Color {
1382        self.inverse_primary.unwrap_or(self.on_primary)
1383    }
1384
1385    /// A color used as an overlay on a surface color to indicate a component's
1386    /// elevation.
1387    pub fn surface_tint(&self) -> Color {
1388        self.surface_tint.unwrap_or(self.primary)
1389    }
1390
1391    /// A color that typically appears behind scrollable content.
1392    #[deprecated(note = "Use surface instead. This feature was deprecated after v3.18.0-0.1.pre.")]
1393    pub fn background(&self) -> Color {
1394        self.background.unwrap_or(self.surface)
1395    }
1396
1397    /// A color that's clearly legible when drawn on `background`.
1398    ///
1399    /// To ensure that an app is accessible, a contrast ratio between
1400    /// `background` and `on_background` of at least 4.5:1 is recommended. See
1401    /// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
1402    #[deprecated(
1403        note = "Use onSurface instead. This feature was deprecated after v3.18.0-0.1.pre."
1404    )]
1405    pub fn on_background(&self) -> Color {
1406        self.on_background.unwrap_or(self.on_surface)
1407    }
1408
1409    /// Creates a copy of this color scheme with the given fields
1410    /// replaced by the non-null parameter values.
1411    ///
1412    /// Every colour that falls back to another is filled with what it reads now, as Dart's
1413    /// `copyWith` passes `this.primaryContainer` on; the `with_*` setters then replace them.
1414    #[allow(deprecated)]
1415    pub fn copy_with(&self) -> ColorScheme {
1416        ColorScheme {
1417            brightness: self.brightness,
1418            primary: self.primary,
1419            on_primary: self.on_primary,
1420            primary_container: Some(self.primary_container()),
1421            on_primary_container: Some(self.on_primary_container()),
1422            primary_fixed: Some(self.primary_fixed()),
1423            primary_fixed_dim: Some(self.primary_fixed_dim()),
1424            on_primary_fixed: Some(self.on_primary_fixed()),
1425            on_primary_fixed_variant: Some(self.on_primary_fixed_variant()),
1426            secondary: self.secondary,
1427            on_secondary: self.on_secondary,
1428            secondary_container: Some(self.secondary_container()),
1429            on_secondary_container: Some(self.on_secondary_container()),
1430            secondary_fixed: Some(self.secondary_fixed()),
1431            secondary_fixed_dim: Some(self.secondary_fixed_dim()),
1432            on_secondary_fixed: Some(self.on_secondary_fixed()),
1433            on_secondary_fixed_variant: Some(self.on_secondary_fixed_variant()),
1434            tertiary: Some(self.tertiary()),
1435            on_tertiary: Some(self.on_tertiary()),
1436            tertiary_container: Some(self.tertiary_container()),
1437            on_tertiary_container: Some(self.on_tertiary_container()),
1438            tertiary_fixed: Some(self.tertiary_fixed()),
1439            tertiary_fixed_dim: Some(self.tertiary_fixed_dim()),
1440            on_tertiary_fixed: Some(self.on_tertiary_fixed()),
1441            on_tertiary_fixed_variant: Some(self.on_tertiary_fixed_variant()),
1442            error: self.error,
1443            on_error: self.on_error,
1444            error_container: Some(self.error_container()),
1445            on_error_container: Some(self.on_error_container()),
1446            surface: self.surface,
1447            on_surface: self.on_surface,
1448            surface_variant: Some(self.surface_variant()),
1449            surface_dim: Some(self.surface_dim()),
1450            surface_bright: Some(self.surface_bright()),
1451            surface_container_lowest: Some(self.surface_container_lowest()),
1452            surface_container_low: Some(self.surface_container_low()),
1453            surface_container: Some(self.surface_container()),
1454            surface_container_high: Some(self.surface_container_high()),
1455            surface_container_highest: Some(self.surface_container_highest()),
1456            on_surface_variant: Some(self.on_surface_variant()),
1457            outline: Some(self.outline()),
1458            outline_variant: Some(self.outline_variant()),
1459            shadow: Some(self.shadow()),
1460            scrim: Some(self.scrim()),
1461            inverse_surface: Some(self.inverse_surface()),
1462            on_inverse_surface: Some(self.on_inverse_surface()),
1463            inverse_primary: Some(self.inverse_primary()),
1464            surface_tint: Some(self.surface_tint()),
1465            background: Some(self.background()),
1466            on_background: Some(self.on_background()),
1467        }
1468    }
1469
1470    /// Linearly interpolate between two `ColorScheme` objects.
1471    ///
1472    /// {@macro dart.ui.shadow.lerp}
1473    #[allow(deprecated)]
1474    pub fn lerp(a: &ColorScheme, b: &ColorScheme, t: f64) -> ColorScheme {
1475        if std::ptr::eq(a, b) {
1476            return a.clone();
1477        }
1478        ColorScheme {
1479            brightness: if t < 0.5 { a.brightness } else { b.brightness },
1480            primary: Color::lerp(Some(a.primary), Some(b.primary), t).expect("both sides"),
1481            on_primary: Color::lerp(Some(a.on_primary), Some(b.on_primary), t).expect("both sides"),
1482            primary_container: Color::lerp(
1483                Some(a.primary_container()),
1484                Some(b.primary_container()),
1485                t,
1486            ),
1487            on_primary_container: Color::lerp(
1488                Some(a.on_primary_container()),
1489                Some(b.on_primary_container()),
1490                t,
1491            ),
1492            primary_fixed: Color::lerp(Some(a.primary_fixed()), Some(b.primary_fixed()), t),
1493            primary_fixed_dim: Color::lerp(
1494                Some(a.primary_fixed_dim()),
1495                Some(b.primary_fixed_dim()),
1496                t,
1497            ),
1498            on_primary_fixed: Color::lerp(
1499                Some(a.on_primary_fixed()),
1500                Some(b.on_primary_fixed()),
1501                t,
1502            ),
1503            on_primary_fixed_variant: Color::lerp(
1504                Some(a.on_primary_fixed_variant()),
1505                Some(b.on_primary_fixed_variant()),
1506                t,
1507            ),
1508            secondary: Color::lerp(Some(a.secondary), Some(b.secondary), t).expect("both sides"),
1509            on_secondary: Color::lerp(Some(a.on_secondary), Some(b.on_secondary), t)
1510                .expect("both sides"),
1511            secondary_container: Color::lerp(
1512                Some(a.secondary_container()),
1513                Some(b.secondary_container()),
1514                t,
1515            ),
1516            on_secondary_container: Color::lerp(
1517                Some(a.on_secondary_container()),
1518                Some(b.on_secondary_container()),
1519                t,
1520            ),
1521            secondary_fixed: Color::lerp(Some(a.secondary_fixed()), Some(b.secondary_fixed()), t),
1522            secondary_fixed_dim: Color::lerp(
1523                Some(a.secondary_fixed_dim()),
1524                Some(b.secondary_fixed_dim()),
1525                t,
1526            ),
1527            on_secondary_fixed: Color::lerp(
1528                Some(a.on_secondary_fixed()),
1529                Some(b.on_secondary_fixed()),
1530                t,
1531            ),
1532            on_secondary_fixed_variant: Color::lerp(
1533                Some(a.on_secondary_fixed_variant()),
1534                Some(b.on_secondary_fixed_variant()),
1535                t,
1536            ),
1537            tertiary: Color::lerp(Some(a.tertiary()), Some(b.tertiary()), t),
1538            on_tertiary: Color::lerp(Some(a.on_tertiary()), Some(b.on_tertiary()), t),
1539            tertiary_container: Color::lerp(
1540                Some(a.tertiary_container()),
1541                Some(b.tertiary_container()),
1542                t,
1543            ),
1544            on_tertiary_container: Color::lerp(
1545                Some(a.on_tertiary_container()),
1546                Some(b.on_tertiary_container()),
1547                t,
1548            ),
1549            tertiary_fixed: Color::lerp(Some(a.tertiary_fixed()), Some(b.tertiary_fixed()), t),
1550            tertiary_fixed_dim: Color::lerp(
1551                Some(a.tertiary_fixed_dim()),
1552                Some(b.tertiary_fixed_dim()),
1553                t,
1554            ),
1555            on_tertiary_fixed: Color::lerp(
1556                Some(a.on_tertiary_fixed()),
1557                Some(b.on_tertiary_fixed()),
1558                t,
1559            ),
1560            on_tertiary_fixed_variant: Color::lerp(
1561                Some(a.on_tertiary_fixed_variant()),
1562                Some(b.on_tertiary_fixed_variant()),
1563                t,
1564            ),
1565            error: Color::lerp(Some(a.error), Some(b.error), t).expect("both sides"),
1566            on_error: Color::lerp(Some(a.on_error), Some(b.on_error), t).expect("both sides"),
1567            error_container: Color::lerp(Some(a.error_container()), Some(b.error_container()), t),
1568            on_error_container: Color::lerp(
1569                Some(a.on_error_container()),
1570                Some(b.on_error_container()),
1571                t,
1572            ),
1573            surface: Color::lerp(Some(a.surface), Some(b.surface), t).expect("both sides"),
1574            on_surface: Color::lerp(Some(a.on_surface), Some(b.on_surface), t).expect("both sides"),
1575            surface_variant: Color::lerp(Some(a.surface_variant()), Some(b.surface_variant()), t),
1576            surface_dim: Color::lerp(Some(a.surface_dim()), Some(b.surface_dim()), t),
1577            surface_bright: Color::lerp(Some(a.surface_bright()), Some(b.surface_bright()), t),
1578            surface_container_lowest: Color::lerp(
1579                Some(a.surface_container_lowest()),
1580                Some(b.surface_container_lowest()),
1581                t,
1582            ),
1583            surface_container_low: Color::lerp(
1584                Some(a.surface_container_low()),
1585                Some(b.surface_container_low()),
1586                t,
1587            ),
1588            surface_container: Color::lerp(
1589                Some(a.surface_container()),
1590                Some(b.surface_container()),
1591                t,
1592            ),
1593            surface_container_high: Color::lerp(
1594                Some(a.surface_container_high()),
1595                Some(b.surface_container_high()),
1596                t,
1597            ),
1598            surface_container_highest: Color::lerp(
1599                Some(a.surface_container_highest()),
1600                Some(b.surface_container_highest()),
1601                t,
1602            ),
1603            on_surface_variant: Color::lerp(
1604                Some(a.on_surface_variant()),
1605                Some(b.on_surface_variant()),
1606                t,
1607            ),
1608            outline: Color::lerp(Some(a.outline()), Some(b.outline()), t),
1609            outline_variant: Color::lerp(Some(a.outline_variant()), Some(b.outline_variant()), t),
1610            shadow: Color::lerp(Some(a.shadow()), Some(b.shadow()), t),
1611            scrim: Color::lerp(Some(a.scrim()), Some(b.scrim()), t),
1612            inverse_surface: Color::lerp(Some(a.inverse_surface()), Some(b.inverse_surface()), t),
1613            on_inverse_surface: Color::lerp(
1614                Some(a.on_inverse_surface()),
1615                Some(b.on_inverse_surface()),
1616                t,
1617            ),
1618            inverse_primary: Color::lerp(Some(a.inverse_primary()), Some(b.inverse_primary()), t),
1619            surface_tint: Color::lerp(Some(a.surface_tint()), Some(b.surface_tint()), t),
1620            background: Color::lerp(Some(a.background()), Some(b.background()), t),
1621            on_background: Color::lerp(Some(a.on_background()), Some(b.on_background()), t),
1622        }
1623    }
1624
1625    fn build_dynamic_scheme(
1626        brightness: Brightness,
1627        seed_color: Color,
1628        scheme_variant: DynamicSchemeVariant,
1629        contrast_level: f64,
1630    ) -> DynamicScheme {
1631        debug_assert!(
1632            contrast_level + PRECISION_ERROR_TOLERANCE >= -1.0
1633                && contrast_level - PRECISION_ERROR_TOLERANCE <= 1.0,
1634            "contrastLevel must be between -1.0 and 1.0 inclusive."
1635        );
1636        let is_dark = brightness == Brightness::Dark;
1637        let source_color = Hct::from_int(seed_color.to_argb32());
1638        match scheme_variant {
1639            DynamicSchemeVariant::TonalSpot => {
1640                SchemeTonalSpot::new(source_color, is_dark, contrast_level)
1641            }
1642            DynamicSchemeVariant::Fidelity => {
1643                SchemeFidelity::new(source_color, is_dark, contrast_level)
1644            }
1645            DynamicSchemeVariant::Content => {
1646                SchemeContent::new(source_color, is_dark, contrast_level)
1647            }
1648            DynamicSchemeVariant::Monochrome => {
1649                SchemeMonochrome::new(source_color, is_dark, contrast_level)
1650            }
1651            DynamicSchemeVariant::Neutral => {
1652                SchemeNeutral::new(source_color, is_dark, contrast_level)
1653            }
1654            DynamicSchemeVariant::Vibrant => {
1655                SchemeVibrant::new(source_color, is_dark, contrast_level)
1656            }
1657            DynamicSchemeVariant::Expressive => {
1658                SchemeExpressive::new(source_color, is_dark, contrast_level)
1659            }
1660            DynamicSchemeVariant::Rainbow => {
1661                SchemeRainbow::new(source_color, is_dark, contrast_level)
1662            }
1663            DynamicSchemeVariant::FruitSalad => {
1664                SchemeFruitSalad::new(source_color, is_dark, contrast_level)
1665            }
1666        }
1667    }
1668    /// The [`ThemeData::color_scheme`] of the ambient `Theme`.
1669    ///
1670    /// Equivalent to `Theme::of(app, context).color_scheme`.
1671    pub fn of(app: &mut App, context: BuildContext) -> ColorScheme {
1672        Theme::of(app, context).color_scheme.clone()
1673    }
1674}
1675
1676impl PartialEq for ColorScheme {
1677    #[allow(deprecated)]
1678    fn eq(&self, other: &ColorScheme) -> bool {
1679        other.brightness == self.brightness
1680            && other.primary == self.primary
1681            && other.on_primary == self.on_primary
1682            && other.primary_container() == self.primary_container()
1683            && other.on_primary_container() == self.on_primary_container()
1684            && other.primary_fixed() == self.primary_fixed()
1685            && other.primary_fixed_dim() == self.primary_fixed_dim()
1686            && other.on_primary_fixed() == self.on_primary_fixed()
1687            && other.on_primary_fixed_variant() == self.on_primary_fixed_variant()
1688            && other.secondary == self.secondary
1689            && other.on_secondary == self.on_secondary
1690            && other.secondary_container() == self.secondary_container()
1691            && other.on_secondary_container() == self.on_secondary_container()
1692            && other.secondary_fixed() == self.secondary_fixed()
1693            && other.secondary_fixed_dim() == self.secondary_fixed_dim()
1694            && other.on_secondary_fixed() == self.on_secondary_fixed()
1695            && other.on_secondary_fixed_variant() == self.on_secondary_fixed_variant()
1696            && other.tertiary() == self.tertiary()
1697            && other.on_tertiary() == self.on_tertiary()
1698            && other.tertiary_container() == self.tertiary_container()
1699            && other.on_tertiary_container() == self.on_tertiary_container()
1700            && other.tertiary_fixed() == self.tertiary_fixed()
1701            && other.tertiary_fixed_dim() == self.tertiary_fixed_dim()
1702            && other.on_tertiary_fixed() == self.on_tertiary_fixed()
1703            && other.on_tertiary_fixed_variant() == self.on_tertiary_fixed_variant()
1704            && other.error == self.error
1705            && other.on_error == self.on_error
1706            && other.error_container() == self.error_container()
1707            && other.on_error_container() == self.on_error_container()
1708            && other.surface == self.surface
1709            && other.on_surface == self.on_surface
1710            && other.surface_dim() == self.surface_dim()
1711            && other.surface_bright() == self.surface_bright()
1712            && other.surface_container_lowest() == self.surface_container_lowest()
1713            && other.surface_container_low() == self.surface_container_low()
1714            && other.surface_container() == self.surface_container()
1715            && other.surface_container_high() == self.surface_container_high()
1716            && other.surface_container_highest() == self.surface_container_highest()
1717            && other.on_surface_variant() == self.on_surface_variant()
1718            && other.outline() == self.outline()
1719            && other.outline_variant() == self.outline_variant()
1720            && other.shadow() == self.shadow()
1721            && other.scrim() == self.scrim()
1722            && other.inverse_surface() == self.inverse_surface()
1723            && other.on_inverse_surface() == self.on_inverse_surface()
1724            && other.inverse_primary() == self.inverse_primary()
1725            && other.surface_tint() == self.surface_tint()
1726            && other.background() == self.background()
1727            && other.on_background() == self.on_background()
1728            && other.surface_variant() == self.surface_variant()
1729    }
1730}
1731
1732/// Dart's `ColorScheme.fromSeed`, collecting what decides the generated colours.
1733#[derive(Clone, Debug)]
1734pub struct ColorSchemeFromSeedBuilder {
1735    seed_color: Color,
1736    brightness: Brightness,
1737    dynamic_scheme_variant: DynamicSchemeVariant,
1738    contrast_level: f64,
1739}
1740
1741impl ColorSchemeFromSeedBuilder {
1742    /// Dart `ColorScheme.fromSeed(brightness:)`.
1743    pub fn brightness(mut self, brightness: Brightness) -> ColorSchemeFromSeedBuilder {
1744        self.brightness = brightness;
1745        self
1746    }
1747
1748    /// Dart `ColorScheme.fromSeed(dynamicSchemeVariant:)`.
1749    pub fn dynamic_scheme_variant(
1750        mut self,
1751        dynamic_scheme_variant: DynamicSchemeVariant,
1752    ) -> ColorSchemeFromSeedBuilder {
1753        self.dynamic_scheme_variant = dynamic_scheme_variant;
1754        self
1755    }
1756
1757    /// Dart `ColorScheme.fromSeed(contrastLevel:)`.
1758    pub fn contrast_level(mut self, contrast_level: f64) -> ColorSchemeFromSeedBuilder {
1759        self.contrast_level = contrast_level;
1760        self
1761    }
1762
1763    /// The scheme generated from the seed color.
1764    pub fn build(self) -> ColorScheme {
1765        let scheme = ColorScheme::build_dynamic_scheme(
1766            self.brightness,
1767            self.seed_color,
1768            self.dynamic_scheme_variant,
1769            self.contrast_level,
1770        );
1771        let color = |dynamic_color: Rc<DynamicColor>| Color::new(dynamic_color.get_argb(&scheme));
1772
1773        ColorScheme {
1774            brightness: self.brightness,
1775            primary: color(MaterialDynamicColors::primary()),
1776            on_primary: color(MaterialDynamicColors::on_primary()),
1777            primary_container: Some(color(MaterialDynamicColors::primary_container())),
1778            on_primary_container: Some(color(MaterialDynamicColors::on_primary_container())),
1779            primary_fixed: Some(color(MaterialDynamicColors::primary_fixed())),
1780            primary_fixed_dim: Some(color(MaterialDynamicColors::primary_fixed_dim())),
1781            on_primary_fixed: Some(color(MaterialDynamicColors::on_primary_fixed())),
1782            on_primary_fixed_variant: Some(
1783                color(MaterialDynamicColors::on_primary_fixed_variant()),
1784            ),
1785            secondary: color(MaterialDynamicColors::secondary()),
1786            on_secondary: color(MaterialDynamicColors::on_secondary()),
1787            secondary_container: Some(color(MaterialDynamicColors::secondary_container())),
1788            on_secondary_container: Some(color(MaterialDynamicColors::on_secondary_container())),
1789            secondary_fixed: Some(color(MaterialDynamicColors::secondary_fixed())),
1790            secondary_fixed_dim: Some(color(MaterialDynamicColors::secondary_fixed_dim())),
1791            on_secondary_fixed: Some(color(MaterialDynamicColors::on_secondary_fixed())),
1792            on_secondary_fixed_variant: Some(color(
1793                MaterialDynamicColors::on_secondary_fixed_variant(),
1794            )),
1795            tertiary: Some(color(MaterialDynamicColors::tertiary())),
1796            on_tertiary: Some(color(MaterialDynamicColors::on_tertiary())),
1797            tertiary_container: Some(color(MaterialDynamicColors::tertiary_container())),
1798            on_tertiary_container: Some(color(MaterialDynamicColors::on_tertiary_container())),
1799            tertiary_fixed: Some(color(MaterialDynamicColors::tertiary_fixed())),
1800            tertiary_fixed_dim: Some(color(MaterialDynamicColors::tertiary_fixed_dim())),
1801            on_tertiary_fixed: Some(color(MaterialDynamicColors::on_tertiary_fixed())),
1802            on_tertiary_fixed_variant: Some(color(
1803                MaterialDynamicColors::on_tertiary_fixed_variant(),
1804            )),
1805            error: color(MaterialDynamicColors::error()),
1806            on_error: color(MaterialDynamicColors::on_error()),
1807            error_container: Some(color(MaterialDynamicColors::error_container())),
1808            on_error_container: Some(color(MaterialDynamicColors::on_error_container())),
1809            surface: color(MaterialDynamicColors::surface()),
1810            on_surface: color(MaterialDynamicColors::on_surface()),
1811            surface_variant: Some(color(MaterialDynamicColors::surface_variant())),
1812            surface_dim: Some(color(MaterialDynamicColors::surface_dim())),
1813            surface_bright: Some(color(MaterialDynamicColors::surface_bright())),
1814            surface_container_lowest: Some(
1815                color(MaterialDynamicColors::surface_container_lowest()),
1816            ),
1817            surface_container_low: Some(color(MaterialDynamicColors::surface_container_low())),
1818            surface_container: Some(color(MaterialDynamicColors::surface_container())),
1819            surface_container_high: Some(color(MaterialDynamicColors::surface_container_high())),
1820            surface_container_highest: Some(color(
1821                MaterialDynamicColors::surface_container_highest(),
1822            )),
1823            on_surface_variant: Some(color(MaterialDynamicColors::on_surface_variant())),
1824            outline: Some(color(MaterialDynamicColors::outline())),
1825            outline_variant: Some(color(MaterialDynamicColors::outline_variant())),
1826            shadow: Some(color(MaterialDynamicColors::shadow())),
1827            scrim: Some(color(MaterialDynamicColors::scrim())),
1828            inverse_surface: Some(color(MaterialDynamicColors::inverse_surface())),
1829            on_inverse_surface: Some(color(MaterialDynamicColors::inverse_on_surface())),
1830            inverse_primary: Some(color(MaterialDynamicColors::inverse_primary())),
1831            surface_tint: Some(color(MaterialDynamicColors::primary())),
1832            background: Some(color(MaterialDynamicColors::background())),
1833            on_background: Some(color(MaterialDynamicColors::on_background())),
1834        }
1835    }
1836}
1837
1838/// Dart's `ColorScheme.fromSwatch`, collecting its arguments.
1839#[derive(Clone, Debug)]
1840pub struct ColorSchemeFromSwatchBuilder {
1841    primary_swatch: MaterialColor,
1842    accent_color: Option<Color>,
1843    card_color: Option<Color>,
1844    background_color: Option<Color>,
1845    error_color: Option<Color>,
1846    brightness: Brightness,
1847}
1848
1849impl ColorSchemeFromSwatchBuilder {
1850    /// Dart `ColorScheme.fromSwatch(primarySwatch:)`.
1851    pub fn primary_swatch(mut self, primary_swatch: MaterialColor) -> ColorSchemeFromSwatchBuilder {
1852        self.primary_swatch = primary_swatch;
1853        self
1854    }
1855
1856    /// Dart `ColorScheme.fromSwatch(accentColor:)`.
1857    pub fn accent_color(mut self, accent_color: Color) -> ColorSchemeFromSwatchBuilder {
1858        self.accent_color = Some(accent_color);
1859        self
1860    }
1861
1862    /// Dart `ColorScheme.fromSwatch(cardColor:)`.
1863    pub fn card_color(mut self, card_color: Color) -> ColorSchemeFromSwatchBuilder {
1864        self.card_color = Some(card_color);
1865        self
1866    }
1867
1868    /// Dart `ColorScheme.fromSwatch(backgroundColor:)`.
1869    pub fn background_color(mut self, background_color: Color) -> ColorSchemeFromSwatchBuilder {
1870        self.background_color = Some(background_color);
1871        self
1872    }
1873
1874    /// Dart `ColorScheme.fromSwatch(errorColor:)`.
1875    pub fn error_color(mut self, error_color: Color) -> ColorSchemeFromSwatchBuilder {
1876        self.error_color = Some(error_color);
1877        self
1878    }
1879
1880    /// Dart `ColorScheme.fromSwatch(brightness:)`.
1881    pub fn brightness(mut self, brightness: Brightness) -> ColorSchemeFromSwatchBuilder {
1882        self.brightness = brightness;
1883        self
1884    }
1885
1886    /// The scheme made from the swatch.
1887    #[allow(deprecated)]
1888    pub fn build(self) -> ColorScheme {
1889        let is_dark = self.brightness == Brightness::Dark;
1890        let primary_is_dark =
1891            ColorScheme::brightness_for(self.primary_swatch.primary()) == Brightness::Dark;
1892        let secondary = self.accent_color.unwrap_or_else(|| {
1893            if is_dark {
1894                Colors::TEAL_ACCENT.get(&200).unwrap()
1895            } else {
1896                self.primary_swatch.primary()
1897            }
1898        });
1899        let secondary_is_dark = ColorScheme::brightness_for(secondary) == Brightness::Dark;
1900
1901        ColorScheme::new(
1902            self.brightness,
1903            self.primary_swatch.primary(),
1904            if primary_is_dark {
1905                Colors::WHITE
1906            } else {
1907                Colors::BLACK
1908            },
1909            secondary,
1910            if secondary_is_dark {
1911                Colors::WHITE
1912            } else {
1913                Colors::BLACK
1914            },
1915            self.error_color
1916                .unwrap_or_else(|| Colors::RED.get(&700).unwrap()),
1917            if is_dark {
1918                Colors::BLACK
1919            } else {
1920                Colors::WHITE
1921            },
1922            self.card_color.unwrap_or_else(|| {
1923                if is_dark {
1924                    Colors::GREY.get(&800).unwrap()
1925                } else {
1926                    Colors::WHITE
1927                }
1928            }),
1929            if is_dark {
1930                Colors::WHITE
1931            } else {
1932                Colors::BLACK
1933            },
1934        )
1935        // DEPRECATED (newest deprecations at the bottom)
1936        .with_background(self.background_color.unwrap_or_else(|| {
1937            if is_dark {
1938                Colors::GREY.get(&700).unwrap()
1939            } else {
1940                self.primary_swatch.get(&200).unwrap()
1941            }
1942        }))
1943        .with_on_background(if primary_is_dark {
1944            Colors::WHITE
1945        } else {
1946            Colors::BLACK
1947        })
1948    }
1949}
1950
1951impl waft_foundation::Diagnosticable for ColorScheme {
1952    fn debug_fill_properties(&self, properties: &mut waft_foundation::DiagnosticPropertiesBuilder) {
1953        let default_scheme = ColorScheme::light();
1954        properties.add(
1955            DiagnosticsProperty::<waft_foundation::DartEnum<Brightness>>::new(
1956                "brightness",
1957                waft_foundation::DartEnum(self.brightness()),
1958            )
1959            .default_value(Some(waft_foundation::DartEnum(default_scheme.brightness()))),
1960        );
1961        macro_rules! color_property {
1962            ($($name:literal => $field:ident),* $(,)?) => {
1963                $(
1964                    properties.add(
1965                        ColorProperty::new($name, self.$field())
1966                            .default_value(Some(default_scheme.$field())),
1967                    );
1968                )*
1969            };
1970        }
1971        color_property!(
1972            "primary" => primary,
1973            "onPrimary" => on_primary,
1974            "primaryContainer" => primary_container,
1975            "onPrimaryContainer" => on_primary_container,
1976            "primaryFixed" => primary_fixed,
1977            "primaryFixedDim" => primary_fixed_dim,
1978            "onPrimaryFixed" => on_primary_fixed,
1979            "onPrimaryFixedVariant" => on_primary_fixed_variant,
1980            "secondary" => secondary,
1981            "onSecondary" => on_secondary,
1982            "secondaryContainer" => secondary_container,
1983            "onSecondaryContainer" => on_secondary_container,
1984            "secondaryFixed" => secondary_fixed,
1985            "secondaryFixedDim" => secondary_fixed_dim,
1986            "onSecondaryFixed" => on_secondary_fixed,
1987            "onSecondaryFixedVariant" => on_secondary_fixed_variant,
1988            "tertiary" => tertiary,
1989            "onTertiary" => on_tertiary,
1990            "tertiaryContainer" => tertiary_container,
1991            "onTertiaryContainer" => on_tertiary_container,
1992            "tertiaryFixed" => tertiary_fixed,
1993            "tertiaryFixedDim" => tertiary_fixed_dim,
1994            "onTertiaryFixed" => on_tertiary_fixed,
1995            "onTertiaryFixedVariant" => on_tertiary_fixed_variant,
1996            "error" => error,
1997            "onError" => on_error,
1998            "errorContainer" => error_container,
1999            "onErrorContainer" => on_error_container,
2000            "surface" => surface,
2001            "onSurface" => on_surface,
2002            "surfaceDim" => surface_dim,
2003            "surfaceBright" => surface_bright,
2004            "surfaceContainerLowest" => surface_container_lowest,
2005            "surfaceContainerLow" => surface_container_low,
2006            "surfaceContainer" => surface_container,
2007            "surfaceContainerHigh" => surface_container_high,
2008            "surfaceContainerHighest" => surface_container_highest,
2009            "onSurfaceVariant" => on_surface_variant,
2010            "outline" => outline,
2011            "outlineVariant" => outline_variant,
2012            "shadow" => shadow,
2013            "scrim" => scrim,
2014            "inverseSurface" => inverse_surface,
2015            "onInverseSurface" => on_inverse_surface,
2016            "inversePrimary" => inverse_primary,
2017            "surfaceTint" => surface_tint,
2018        );
2019        // DEPRECATED (newest deprecations at the bottom)
2020        #[allow(deprecated)]
2021        {
2022            color_property!(
2023                "background" => background,
2024                "onBackground" => on_background,
2025                "surfaceVariant" => surface_variant,
2026            );
2027        }
2028    }
2029}
2030
2031impl std::fmt::Display for ColorScheme {
2032    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2033        f.write_str(&waft_foundation::Diagnosticable::to_string(self))
2034    }
2035}
2036
2037#[cfg(test)]
2038#[allow(deprecated)]
2039mod tests {
2040    use std::cell::RefCell;
2041    use std::panic::{AssertUnwindSafe, catch_unwind};
2042
2043    use waft_foundation::Listener;
2044    use waft_widgets::{Builder, Container, IntoWidget, SizedBox};
2045
2046    use waft_widgets::testing::{WidgetTester, find, test_widgets};
2047
2048    use super::*;
2049    use crate::filled_button::FilledButton;
2050    use crate::material::Material;
2051    use crate::test_support::material_app;
2052
2053    #[test]
2054    fn color_scheme_lerp_special_cases() {
2055        let scheme = ColorScheme::light();
2056        assert!(ColorScheme::lerp(&scheme, &scheme, 0.5) == scheme);
2057    }
2058
2059    #[test]
2060    fn light_scheme_matches_the_spec() {
2061        let scheme = ColorScheme::light();
2062        // Colors should match the Material Design baseline default theme:
2063        // https://material.io/design/color/dark-theme.html#ui-application
2064        // with the new Material 3 colors defaulting to values from the M2
2065        // baseline.
2066        assert_eq!(scheme.brightness, Brightness::Light);
2067        assert_eq!(scheme.primary, Color::new(0xFF6200EE));
2068        assert_eq!(scheme.on_primary, Color::new(0xFFFFFFFF));
2069        assert_eq!(scheme.primary_container(), scheme.primary);
2070        assert_eq!(scheme.on_primary_container(), scheme.on_primary);
2071        assert_eq!(scheme.primary_fixed(), scheme.primary);
2072        assert_eq!(scheme.primary_fixed_dim(), scheme.primary);
2073        assert_eq!(scheme.on_primary_fixed(), scheme.on_primary);
2074        assert_eq!(scheme.on_primary_fixed_variant(), scheme.on_primary);
2075        assert_eq!(scheme.secondary, Color::new(0xFF03DAC6));
2076        assert_eq!(scheme.on_secondary, Color::new(0xFF000000));
2077        assert_eq!(scheme.secondary_container(), scheme.secondary);
2078        assert_eq!(scheme.on_secondary_container(), scheme.on_secondary);
2079        assert_eq!(scheme.secondary_fixed(), scheme.secondary);
2080        assert_eq!(scheme.secondary_fixed_dim(), scheme.secondary);
2081        assert_eq!(scheme.on_secondary_fixed(), scheme.on_secondary);
2082        assert_eq!(scheme.on_secondary_fixed_variant(), scheme.on_secondary);
2083        assert_eq!(scheme.tertiary(), scheme.secondary);
2084        assert_eq!(scheme.on_tertiary(), scheme.on_secondary);
2085        assert_eq!(scheme.tertiary_container(), scheme.tertiary());
2086        assert_eq!(scheme.on_tertiary_container(), scheme.on_tertiary());
2087        assert_eq!(scheme.tertiary_fixed(), scheme.tertiary());
2088        assert_eq!(scheme.tertiary_fixed_dim(), scheme.tertiary());
2089        assert_eq!(scheme.on_tertiary_fixed(), scheme.on_tertiary());
2090        assert_eq!(scheme.on_tertiary_fixed_variant(), scheme.on_tertiary());
2091        assert_eq!(scheme.error, Color::new(0xFFB00020));
2092        assert_eq!(scheme.on_error, Color::new(0xFFFFFFFF));
2093        assert_eq!(scheme.error_container(), scheme.error);
2094        assert_eq!(scheme.on_error_container(), scheme.on_error);
2095        assert_eq!(scheme.background(), Color::new(0xFFFFFFFF));
2096        assert_eq!(scheme.on_background(), Color::new(0xFF000000));
2097        assert_eq!(scheme.surface, Color::new(0xFFFFFFFF));
2098        assert_eq!(scheme.surface_bright(), scheme.surface);
2099        assert_eq!(scheme.surface_dim(), scheme.surface);
2100        assert_eq!(scheme.surface_container_lowest(), scheme.surface);
2101        assert_eq!(scheme.surface_container_low(), scheme.surface);
2102        assert_eq!(scheme.surface_container(), scheme.surface);
2103        assert_eq!(scheme.surface_container_high(), scheme.surface);
2104        assert_eq!(scheme.surface_container_highest(), scheme.surface);
2105        assert_eq!(scheme.on_surface, Color::new(0xFF000000));
2106        assert_eq!(scheme.surface_variant(), scheme.surface);
2107        assert_eq!(scheme.on_surface_variant(), scheme.on_surface);
2108        assert_eq!(scheme.outline(), scheme.on_background());
2109        assert_eq!(scheme.outline_variant(), scheme.on_background());
2110        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2111        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2112        assert_eq!(scheme.inverse_surface(), scheme.on_surface);
2113        assert_eq!(scheme.on_inverse_surface(), scheme.surface);
2114        assert_eq!(scheme.inverse_primary(), scheme.on_primary);
2115        assert_eq!(scheme.surface_tint(), scheme.primary);
2116    }
2117
2118    #[test]
2119    fn dark_scheme_matches_the_spec() {
2120        let scheme = ColorScheme::dark();
2121        // Colors should match the Material Design baseline dark theme:
2122        // https://material.io/design/color/dark-theme.html#ui-application
2123        // with the new Material 3 colors defaulting to values from the M2
2124        // baseline.
2125        assert_eq!(scheme.brightness, Brightness::Dark);
2126        assert_eq!(scheme.primary, Color::new(0xFFBB86FC));
2127        assert_eq!(scheme.on_primary, Color::new(0xFF000000));
2128        assert_eq!(scheme.primary_container(), scheme.primary);
2129        assert_eq!(scheme.on_primary_container(), scheme.on_primary);
2130        assert_eq!(scheme.primary_fixed(), scheme.primary);
2131        assert_eq!(scheme.primary_fixed_dim(), scheme.primary);
2132        assert_eq!(scheme.on_primary_fixed(), scheme.on_primary);
2133        assert_eq!(scheme.on_primary_fixed_variant(), scheme.on_primary);
2134        assert_eq!(scheme.secondary, Color::new(0xFF03DAC6));
2135        assert_eq!(scheme.on_secondary, Color::new(0xFF000000));
2136        assert_eq!(scheme.secondary_container(), scheme.secondary);
2137        assert_eq!(scheme.on_secondary_container(), scheme.on_secondary);
2138        assert_eq!(scheme.secondary_fixed(), scheme.secondary);
2139        assert_eq!(scheme.secondary_fixed_dim(), scheme.secondary);
2140        assert_eq!(scheme.on_secondary_fixed(), scheme.on_secondary);
2141        assert_eq!(scheme.on_secondary_fixed_variant(), scheme.on_secondary);
2142        assert_eq!(scheme.tertiary(), scheme.secondary);
2143        assert_eq!(scheme.on_tertiary(), scheme.on_secondary);
2144        assert_eq!(scheme.tertiary_container(), scheme.tertiary());
2145        assert_eq!(scheme.on_tertiary_container(), scheme.on_tertiary());
2146        assert_eq!(scheme.tertiary_fixed(), scheme.tertiary());
2147        assert_eq!(scheme.tertiary_fixed_dim(), scheme.tertiary());
2148        assert_eq!(scheme.on_tertiary_fixed(), scheme.on_tertiary());
2149        assert_eq!(scheme.on_tertiary_fixed_variant(), scheme.on_tertiary());
2150        assert_eq!(scheme.error, Color::new(0xFFCF6679));
2151        assert_eq!(scheme.on_error, Color::new(0xFF000000));
2152        assert_eq!(scheme.error_container(), scheme.error);
2153        assert_eq!(scheme.on_error_container(), scheme.on_error);
2154        assert_eq!(scheme.background(), Color::new(0xFF121212));
2155        assert_eq!(scheme.on_background(), Color::new(0xFFFFFFFF));
2156        assert_eq!(scheme.surface, Color::new(0xFF121212));
2157        assert_eq!(scheme.surface_bright(), scheme.surface);
2158        assert_eq!(scheme.surface_dim(), scheme.surface);
2159        assert_eq!(scheme.surface_container_lowest(), scheme.surface);
2160        assert_eq!(scheme.surface_container_low(), scheme.surface);
2161        assert_eq!(scheme.surface_container(), scheme.surface);
2162        assert_eq!(scheme.surface_container_high(), scheme.surface);
2163        assert_eq!(scheme.surface_container_highest(), scheme.surface);
2164        assert_eq!(scheme.on_surface, Color::new(0xFFFFFFFF));
2165        assert_eq!(scheme.surface_variant(), scheme.surface);
2166        assert_eq!(scheme.on_surface_variant(), scheme.on_surface);
2167        assert_eq!(scheme.outline(), scheme.on_background());
2168        assert_eq!(scheme.outline_variant(), scheme.on_background());
2169        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2170        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2171        assert_eq!(scheme.inverse_surface(), scheme.on_surface);
2172        assert_eq!(scheme.on_inverse_surface(), scheme.surface);
2173        assert_eq!(scheme.inverse_primary(), scheme.on_primary);
2174        assert_eq!(scheme.surface_tint(), scheme.primary);
2175    }
2176
2177    #[test]
2178    fn high_contrast_light_scheme_matches_the_spec() {
2179        let scheme = ColorScheme::high_contrast_light();
2180        // Colors are based off of the Material Design baseline default theme:
2181        // https://material.io/design/color/dark-theme.html#ui-application
2182        // with the new Material 3 colors defaulting to values from the M2
2183        // baseline.
2184        assert_eq!(scheme.brightness, Brightness::Light);
2185        assert_eq!(scheme.primary, Color::new(0xFF0000BA));
2186        assert_eq!(scheme.on_primary, Color::new(0xFFFFFFFF));
2187        assert_eq!(scheme.primary_container(), scheme.primary);
2188        assert_eq!(scheme.on_primary_container(), scheme.on_primary);
2189        assert_eq!(scheme.primary_fixed(), scheme.primary);
2190        assert_eq!(scheme.primary_fixed_dim(), scheme.primary);
2191        assert_eq!(scheme.on_primary_fixed(), scheme.on_primary);
2192        assert_eq!(scheme.on_primary_fixed_variant(), scheme.on_primary);
2193        assert_eq!(scheme.secondary, Color::new(0xFF66FFF9));
2194        assert_eq!(scheme.on_secondary, Color::new(0xFF000000));
2195        assert_eq!(scheme.secondary_container(), scheme.secondary);
2196        assert_eq!(scheme.on_secondary_container(), scheme.on_secondary);
2197        assert_eq!(scheme.secondary_fixed(), scheme.secondary);
2198        assert_eq!(scheme.secondary_fixed_dim(), scheme.secondary);
2199        assert_eq!(scheme.on_secondary_fixed(), scheme.on_secondary);
2200        assert_eq!(scheme.on_secondary_fixed_variant(), scheme.on_secondary);
2201        assert_eq!(scheme.tertiary(), scheme.secondary);
2202        assert_eq!(scheme.on_tertiary(), scheme.on_secondary);
2203        assert_eq!(scheme.tertiary_container(), scheme.tertiary());
2204        assert_eq!(scheme.on_tertiary_container(), scheme.on_tertiary());
2205        assert_eq!(scheme.tertiary_fixed(), scheme.tertiary());
2206        assert_eq!(scheme.tertiary_fixed_dim(), scheme.tertiary());
2207        assert_eq!(scheme.on_tertiary_fixed(), scheme.on_tertiary());
2208        assert_eq!(scheme.on_tertiary_fixed_variant(), scheme.on_tertiary());
2209        assert_eq!(scheme.error, Color::new(0xFF790000));
2210        assert_eq!(scheme.on_error, Color::new(0xFFFFFFFF));
2211        assert_eq!(scheme.error_container(), scheme.error);
2212        assert_eq!(scheme.on_error_container(), scheme.on_error);
2213        assert_eq!(scheme.background(), Color::new(0xFFFFFFFF));
2214        assert_eq!(scheme.on_background(), Color::new(0xFF000000));
2215        assert_eq!(scheme.surface, Color::new(0xFFFFFFFF));
2216        assert_eq!(scheme.surface_bright(), scheme.surface);
2217        assert_eq!(scheme.surface_dim(), scheme.surface);
2218        assert_eq!(scheme.surface_container_lowest(), scheme.surface);
2219        assert_eq!(scheme.surface_container_low(), scheme.surface);
2220        assert_eq!(scheme.surface_container(), scheme.surface);
2221        assert_eq!(scheme.surface_container_high(), scheme.surface);
2222        assert_eq!(scheme.surface_container_highest(), scheme.surface);
2223        assert_eq!(scheme.on_surface, Color::new(0xFF000000));
2224        assert_eq!(scheme.surface_variant(), scheme.surface);
2225        assert_eq!(scheme.on_surface_variant(), scheme.on_surface);
2226        assert_eq!(scheme.outline(), scheme.on_background());
2227        assert_eq!(scheme.outline_variant(), scheme.on_background());
2228        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2229        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2230        assert_eq!(scheme.inverse_surface(), scheme.on_surface);
2231        assert_eq!(scheme.on_inverse_surface(), scheme.surface);
2232        assert_eq!(scheme.inverse_primary(), scheme.on_primary);
2233        assert_eq!(scheme.surface_tint(), scheme.primary);
2234    }
2235
2236    #[test]
2237    fn high_contrast_dark_scheme_matches_the_spec() {
2238        let scheme = ColorScheme::high_contrast_dark();
2239        // Colors are based off of the Material Design baseline dark theme:
2240        // https://material.io/design/color/dark-theme.html#ui-application
2241        // with the new Material 3 colors defaulting to values from the M2
2242        // baseline.
2243        assert_eq!(scheme.brightness, Brightness::Dark);
2244        assert_eq!(scheme.primary, Color::new(0xFFEFB7FF));
2245        assert_eq!(scheme.on_primary, Color::new(0xFF000000));
2246        assert_eq!(scheme.primary_container(), scheme.primary);
2247        assert_eq!(scheme.on_primary_container(), scheme.on_primary);
2248        assert_eq!(scheme.primary_fixed(), scheme.primary);
2249        assert_eq!(scheme.primary_fixed_dim(), scheme.primary);
2250        assert_eq!(scheme.on_primary_fixed(), scheme.on_primary);
2251        assert_eq!(scheme.on_primary_fixed_variant(), scheme.on_primary);
2252        assert_eq!(scheme.secondary, Color::new(0xFF66FFF9));
2253        assert_eq!(scheme.on_secondary, Color::new(0xFF000000));
2254        assert_eq!(scheme.secondary_container(), scheme.secondary);
2255        assert_eq!(scheme.on_secondary_container(), scheme.on_secondary);
2256        assert_eq!(scheme.secondary_fixed(), scheme.secondary);
2257        assert_eq!(scheme.secondary_fixed_dim(), scheme.secondary);
2258        assert_eq!(scheme.on_secondary_fixed(), scheme.on_secondary);
2259        assert_eq!(scheme.on_secondary_fixed_variant(), scheme.on_secondary);
2260        assert_eq!(scheme.tertiary(), scheme.secondary);
2261        assert_eq!(scheme.on_tertiary(), scheme.on_secondary);
2262        assert_eq!(scheme.tertiary_container(), scheme.tertiary());
2263        assert_eq!(scheme.on_tertiary_container(), scheme.on_tertiary());
2264        assert_eq!(scheme.tertiary_fixed(), scheme.tertiary());
2265        assert_eq!(scheme.tertiary_fixed_dim(), scheme.tertiary());
2266        assert_eq!(scheme.on_tertiary_fixed(), scheme.on_tertiary());
2267        assert_eq!(scheme.on_tertiary_fixed_variant(), scheme.on_tertiary());
2268        assert_eq!(scheme.error, Color::new(0xFF9B374D));
2269        assert_eq!(scheme.on_error, Color::new(0xFF000000));
2270        assert_eq!(scheme.error_container(), scheme.error);
2271        assert_eq!(scheme.on_error_container(), scheme.on_error);
2272        assert_eq!(scheme.background(), Color::new(0xFF121212));
2273        assert_eq!(scheme.on_background(), Color::new(0xFFFFFFFF));
2274        assert_eq!(scheme.surface, Color::new(0xFF121212));
2275        assert_eq!(scheme.surface_bright(), scheme.surface);
2276        assert_eq!(scheme.surface_dim(), scheme.surface);
2277        assert_eq!(scheme.surface_container_lowest(), scheme.surface);
2278        assert_eq!(scheme.surface_container_low(), scheme.surface);
2279        assert_eq!(scheme.surface_container(), scheme.surface);
2280        assert_eq!(scheme.surface_container_high(), scheme.surface);
2281        assert_eq!(scheme.surface_container_highest(), scheme.surface);
2282        assert_eq!(scheme.on_surface, Color::new(0xFFFFFFFF));
2283        assert_eq!(scheme.surface_variant(), scheme.surface);
2284        assert_eq!(scheme.on_surface_variant(), scheme.on_surface);
2285        assert_eq!(scheme.outline(), scheme.on_background());
2286        assert_eq!(scheme.outline_variant(), scheme.on_background());
2287        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2288        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2289        assert_eq!(scheme.inverse_surface(), scheme.on_surface);
2290        assert_eq!(scheme.on_inverse_surface(), scheme.surface);
2291        assert_eq!(scheme.inverse_primary(), scheme.on_primary);
2292        assert_eq!(scheme.surface_tint(), scheme.primary);
2293    }
2294
2295    #[test]
2296    fn can_generate_a_light_scheme_from_a_seed_color() {
2297        let scheme = ColorScheme::from_seed(Colors::BLUE.primary()).build();
2298        assert_eq!(scheme.primary, Color::new(0xFF36618E));
2299        assert_eq!(scheme.on_primary, Color::new(0xFFFFFFFF));
2300        assert_eq!(scheme.primary_container(), Color::new(0xFFD1E4FF));
2301        assert_eq!(scheme.on_primary_container(), Color::new(0xFF194975));
2302        assert_eq!(scheme.primary_fixed(), Color::new(0xFFD1E4FF));
2303        assert_eq!(scheme.primary_fixed_dim(), Color::new(0xFFA0CAFD));
2304        assert_eq!(scheme.on_primary_fixed(), Color::new(0xFF001D36));
2305        assert_eq!(scheme.on_primary_fixed_variant(), Color::new(0xFF194975));
2306        assert_eq!(scheme.secondary, Color::new(0xFF535F70));
2307        assert_eq!(scheme.on_secondary, Color::new(0xFFFFFFFF));
2308        assert_eq!(scheme.secondary_container(), Color::new(0xFFD7E3F7));
2309        assert_eq!(scheme.on_secondary_container(), Color::new(0xFF3B4858));
2310        assert_eq!(scheme.secondary_fixed(), Color::new(0xFFD7E3F7));
2311        assert_eq!(scheme.secondary_fixed_dim(), Color::new(0xFFBBC7DB));
2312        assert_eq!(scheme.on_secondary_fixed(), Color::new(0xFF101C2B));
2313        assert_eq!(scheme.on_secondary_fixed_variant(), Color::new(0xFF3B4858));
2314        assert_eq!(scheme.tertiary(), Color::new(0xFF6B5778));
2315        assert_eq!(scheme.on_tertiary(), Color::new(0xFFFFFFFF));
2316        assert_eq!(scheme.tertiary_container(), Color::new(0xFFF2DAFF));
2317        assert_eq!(scheme.on_tertiary_container(), Color::new(0xFF523F5F));
2318        assert_eq!(scheme.tertiary_fixed(), Color::new(0xFFF2DAFF));
2319        assert_eq!(scheme.tertiary_fixed_dim(), Color::new(0xFFD6BEE4));
2320        assert_eq!(scheme.on_tertiary_fixed(), Color::new(0xFF251431));
2321        assert_eq!(scheme.on_tertiary_fixed_variant(), Color::new(0xFF523F5F));
2322        assert_eq!(scheme.error, Color::new(0xFFBA1A1A));
2323        assert_eq!(scheme.on_error, Color::new(0xFFFFFFFF));
2324        assert_eq!(scheme.error_container(), Color::new(0xFFFFDAD6));
2325        assert_eq!(scheme.on_error_container(), Color::new(0xFF93000A));
2326        assert_eq!(scheme.outline(), Color::new(0xFF73777F));
2327        assert_eq!(scheme.outline_variant(), Color::new(0xFFC3C7CF));
2328        assert_eq!(scheme.background(), Color::new(0xFFF8F9FF));
2329        assert_eq!(scheme.on_background(), Color::new(0xFF191C20));
2330        assert_eq!(scheme.surface, Color::new(0xFFF8F9FF));
2331        assert_eq!(scheme.surface_bright(), Color::new(0xFFF8F9FF));
2332        assert_eq!(scheme.surface_dim(), Color::new(0xFFD8DAE0));
2333        assert_eq!(scheme.surface_container_lowest(), Color::new(0xFFFFFFFF));
2334        assert_eq!(scheme.surface_container_low(), Color::new(0xFFF2F3FA));
2335        assert_eq!(scheme.surface_container(), Color::new(0xFFECEEF4));
2336        assert_eq!(scheme.surface_container_high(), Color::new(0xFFE6E8EE));
2337        assert_eq!(scheme.surface_container_highest(), Color::new(0xFFE1E2E8));
2338        assert_eq!(scheme.on_surface, Color::new(0xFF191C20));
2339        assert_eq!(scheme.surface_variant(), Color::new(0xFFDFE2EB));
2340        assert_eq!(scheme.on_surface_variant(), Color::new(0xFF43474E));
2341        assert_eq!(scheme.inverse_surface(), Color::new(0xFF2E3135));
2342        assert_eq!(scheme.on_inverse_surface(), Color::new(0xFFEFF0F7));
2343        assert_eq!(scheme.inverse_primary(), Color::new(0xFFA0CAFD));
2344        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2345        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2346        assert_eq!(scheme.surface_tint(), Color::new(0xFF36618E));
2347        assert_eq!(scheme.brightness, Brightness::Light);
2348    }
2349
2350    #[test]
2351    fn copy_with_overrides_given_colors() {
2352        let scheme = ColorScheme::light()
2353            .copy_with()
2354            .with_brightness(Brightness::Dark)
2355            .with_primary(Color::new(0x00000001))
2356            .with_on_primary(Color::new(0x00000002))
2357            .with_primary_container(Color::new(0x00000003))
2358            .with_on_primary_container(Color::new(0x00000004))
2359            .with_primary_fixed(Color::new(0x0000001D))
2360            .with_primary_fixed_dim(Color::new(0x0000001E))
2361            .with_on_primary_fixed(Color::new(0x0000001F))
2362            .with_on_primary_fixed_variant(Color::new(0x00000020))
2363            .with_secondary(Color::new(0x00000005))
2364            .with_on_secondary(Color::new(0x00000006))
2365            .with_secondary_container(Color::new(0x00000007))
2366            .with_on_secondary_container(Color::new(0x00000008))
2367            .with_secondary_fixed(Color::new(0x00000021))
2368            .with_secondary_fixed_dim(Color::new(0x00000022))
2369            .with_on_secondary_fixed(Color::new(0x00000023))
2370            .with_on_secondary_fixed_variant(Color::new(0x00000024))
2371            .with_tertiary(Color::new(0x00000009))
2372            .with_on_tertiary(Color::new(0x0000000A))
2373            .with_tertiary_container(Color::new(0x0000000B))
2374            .with_on_tertiary_container(Color::new(0x0000000C))
2375            .with_tertiary_fixed(Color::new(0x00000025))
2376            .with_tertiary_fixed_dim(Color::new(0x00000026))
2377            .with_on_tertiary_fixed(Color::new(0x00000027))
2378            .with_on_tertiary_fixed_variant(Color::new(0x00000028))
2379            .with_error(Color::new(0x0000000D))
2380            .with_on_error(Color::new(0x0000000E))
2381            .with_error_container(Color::new(0x0000000F))
2382            .with_on_error_container(Color::new(0x00000010))
2383            .with_background(Color::new(0x00000011))
2384            .with_on_background(Color::new(0x00000012))
2385            .with_surface(Color::new(0x00000013))
2386            .with_surface_dim(Color::new(0x00000029))
2387            .with_surface_bright(Color::new(0x0000002A))
2388            .with_surface_container_lowest(Color::new(0x0000002B))
2389            .with_surface_container_low(Color::new(0x0000002C))
2390            .with_surface_container(Color::new(0x0000002D))
2391            .with_surface_container_high(Color::new(0x0000002E))
2392            .with_surface_container_highest(Color::new(0x0000002F))
2393            .with_on_surface(Color::new(0x00000014))
2394            .with_surface_variant(Color::new(0x00000015))
2395            .with_on_surface_variant(Color::new(0x00000016))
2396            .with_outline(Color::new(0x00000017))
2397            .with_outline_variant(Color::new(0x00000117))
2398            .with_shadow(Color::new(0x00000018))
2399            .with_scrim(Color::new(0x00000118))
2400            .with_inverse_surface(Color::new(0x00000019))
2401            .with_on_inverse_surface(Color::new(0x0000001A))
2402            .with_inverse_primary(Color::new(0x0000001B))
2403            .with_surface_tint(Color::new(0x0000001C));
2404
2405        assert_eq!(scheme.brightness, Brightness::Dark);
2406        assert_eq!(scheme.primary, Color::new(0x00000001));
2407        assert_eq!(scheme.on_primary, Color::new(0x00000002));
2408        assert_eq!(scheme.primary_container(), Color::new(0x00000003));
2409        assert_eq!(scheme.on_primary_container(), Color::new(0x00000004));
2410        assert_eq!(scheme.primary_fixed(), Color::new(0x0000001D));
2411        assert_eq!(scheme.primary_fixed_dim(), Color::new(0x0000001E));
2412        assert_eq!(scheme.on_primary_fixed(), Color::new(0x0000001F));
2413        assert_eq!(scheme.on_primary_fixed_variant(), Color::new(0x00000020));
2414        assert_eq!(scheme.secondary, Color::new(0x00000005));
2415        assert_eq!(scheme.on_secondary, Color::new(0x00000006));
2416        assert_eq!(scheme.secondary_container(), Color::new(0x00000007));
2417        assert_eq!(scheme.on_secondary_container(), Color::new(0x00000008));
2418        assert_eq!(scheme.secondary_fixed(), Color::new(0x00000021));
2419        assert_eq!(scheme.secondary_fixed_dim(), Color::new(0x00000022));
2420        assert_eq!(scheme.on_secondary_fixed(), Color::new(0x00000023));
2421        assert_eq!(scheme.on_secondary_fixed_variant(), Color::new(0x00000024));
2422        assert_eq!(scheme.tertiary(), Color::new(0x00000009));
2423        assert_eq!(scheme.on_tertiary(), Color::new(0x0000000A));
2424        assert_eq!(scheme.tertiary_container(), Color::new(0x0000000B));
2425        assert_eq!(scheme.on_tertiary_container(), Color::new(0x0000000C));
2426        assert_eq!(scheme.tertiary_fixed(), Color::new(0x00000025));
2427        assert_eq!(scheme.tertiary_fixed_dim(), Color::new(0x00000026));
2428        assert_eq!(scheme.on_tertiary_fixed(), Color::new(0x00000027));
2429        assert_eq!(scheme.on_tertiary_fixed_variant(), Color::new(0x00000028));
2430        assert_eq!(scheme.error, Color::new(0x0000000D));
2431        assert_eq!(scheme.on_error, Color::new(0x0000000E));
2432        assert_eq!(scheme.error_container(), Color::new(0x0000000F));
2433        assert_eq!(scheme.on_error_container(), Color::new(0x00000010));
2434        assert_eq!(scheme.background(), Color::new(0x00000011));
2435        assert_eq!(scheme.on_background(), Color::new(0x00000012));
2436        assert_eq!(scheme.surface, Color::new(0x00000013));
2437        assert_eq!(scheme.surface_dim(), Color::new(0x00000029));
2438        assert_eq!(scheme.surface_bright(), Color::new(0x0000002A));
2439        assert_eq!(scheme.surface_container_lowest(), Color::new(0x0000002B));
2440        assert_eq!(scheme.surface_container_low(), Color::new(0x0000002C));
2441        assert_eq!(scheme.surface_container(), Color::new(0x0000002D));
2442        assert_eq!(scheme.surface_container_high(), Color::new(0x0000002E));
2443        assert_eq!(scheme.surface_container_highest(), Color::new(0x0000002F));
2444        assert_eq!(scheme.on_surface, Color::new(0x00000014));
2445        assert_eq!(scheme.surface_variant(), Color::new(0x00000015));
2446        assert_eq!(scheme.on_surface_variant(), Color::new(0x00000016));
2447        assert_eq!(scheme.outline(), Color::new(0x00000017));
2448        assert_eq!(scheme.outline_variant(), Color::new(0x00000117));
2449        assert_eq!(scheme.shadow(), Color::new(0x00000018));
2450        assert_eq!(scheme.scrim(), Color::new(0x00000118));
2451        assert_eq!(scheme.inverse_surface(), Color::new(0x00000019));
2452        assert_eq!(scheme.on_inverse_surface(), Color::new(0x0000001A));
2453        assert_eq!(scheme.inverse_primary(), Color::new(0x0000001B));
2454        assert_eq!(scheme.surface_tint(), Color::new(0x0000001C));
2455    }
2456
2457    #[test]
2458    fn can_generate_a_dark_scheme_from_a_seed_color() {
2459        let scheme = ColorScheme::from_seed(Colors::BLUE.primary())
2460            .brightness(Brightness::Dark)
2461            .build();
2462        assert_eq!(scheme.primary, Color::new(0xFFA0CAFD));
2463        assert_eq!(scheme.on_primary, Color::new(0xFF003258));
2464        assert_eq!(scheme.primary_container(), Color::new(0xFF194975));
2465        assert_eq!(scheme.on_primary_container(), Color::new(0xFFD1E4FF));
2466        assert_eq!(scheme.primary_fixed(), Color::new(0xFFD1E4FF));
2467        assert_eq!(scheme.primary_fixed_dim(), Color::new(0xFFA0CAFD));
2468        assert_eq!(scheme.on_primary_fixed(), Color::new(0xFF001D36));
2469        assert_eq!(scheme.on_primary_fixed_variant(), Color::new(0xFF194975));
2470        assert_eq!(scheme.secondary, Color::new(0xFFBBC7DB));
2471        assert_eq!(scheme.on_secondary, Color::new(0xFF253140));
2472        assert_eq!(scheme.secondary_container(), Color::new(0xFF3B4858));
2473        assert_eq!(scheme.on_secondary_container(), Color::new(0xFFD7E3F7));
2474        assert_eq!(scheme.secondary_fixed(), Color::new(0xFFD7E3F7));
2475        assert_eq!(scheme.secondary_fixed_dim(), Color::new(0xFFBBC7DB));
2476        assert_eq!(scheme.on_secondary_fixed(), Color::new(0xFF101C2B));
2477        assert_eq!(scheme.on_secondary_fixed_variant(), Color::new(0xFF3B4858));
2478        assert_eq!(scheme.tertiary(), Color::new(0xFFD6BEE4));
2479        assert_eq!(scheme.on_tertiary(), Color::new(0xFF3B2948));
2480        assert_eq!(scheme.tertiary_container(), Color::new(0xFF523F5F));
2481        assert_eq!(scheme.on_tertiary_container(), Color::new(0xFFF2DAFF));
2482        assert_eq!(scheme.tertiary_fixed(), Color::new(0xFFF2DAFF));
2483        assert_eq!(scheme.tertiary_fixed_dim(), Color::new(0xFFD6BEE4));
2484        assert_eq!(scheme.on_tertiary_fixed(), Color::new(0xFF251431));
2485        assert_eq!(scheme.on_tertiary_fixed_variant(), Color::new(0xFF523F5F));
2486        assert_eq!(scheme.error, Color::new(0xFFFFB4AB));
2487        assert_eq!(scheme.on_error, Color::new(0xFF690005));
2488        assert_eq!(scheme.error_container(), Color::new(0xFF93000A));
2489        assert_eq!(scheme.on_error_container(), Color::new(0xFFFFDAD6));
2490        assert_eq!(scheme.outline(), Color::new(0xFF8D9199));
2491        assert_eq!(scheme.outline_variant(), Color::new(0xFF43474E));
2492        assert_eq!(scheme.background(), Color::new(0xFF111418));
2493        assert_eq!(scheme.on_background(), Color::new(0xFFE1E2E8));
2494        assert_eq!(scheme.surface, Color::new(0xFF111418));
2495        assert_eq!(scheme.surface_dim(), Color::new(0xFF111418));
2496        assert_eq!(scheme.surface_bright(), Color::new(0xFF36393E));
2497        assert_eq!(scheme.surface_container_lowest(), Color::new(0xFF0B0E13));
2498        assert_eq!(scheme.surface_container_low(), Color::new(0xFF191C20));
2499        assert_eq!(scheme.surface_container(), Color::new(0xFF1D2024));
2500        assert_eq!(scheme.surface_container_high(), Color::new(0xFF272A2F));
2501        assert_eq!(scheme.surface_container_highest(), Color::new(0xFF32353A));
2502        assert_eq!(scheme.on_surface, Color::new(0xFFE1E2E8));
2503        assert_eq!(scheme.surface_variant(), Color::new(0xFF43474E));
2504        assert_eq!(scheme.on_surface_variant(), Color::new(0xFFC3C7CF));
2505        assert_eq!(scheme.inverse_surface(), Color::new(0xFFE1E2E8));
2506        assert_eq!(scheme.on_inverse_surface(), Color::new(0xFF2E3135));
2507        assert_eq!(scheme.inverse_primary(), Color::new(0xFF36618E));
2508        assert_eq!(scheme.shadow(), Color::new(0xFF000000));
2509        assert_eq!(scheme.scrim(), Color::new(0xFF000000));
2510        assert_eq!(scheme.surface_tint(), Color::new(0xFFA0CAFD));
2511        assert_eq!(scheme.brightness, Brightness::Dark);
2512    }
2513
2514    #[test]
2515    fn can_override_specific_colors_in_a_generated_scheme() {
2516        let base_scheme = ColorScheme::from_seed(Colors::BLUE.primary()).build();
2517        let primary_override = Color::new(0xFFABCDEF);
2518        let scheme = ColorScheme::from_seed(Colors::BLUE.primary())
2519            .build()
2520            .with_primary(primary_override);
2521        assert_eq!(scheme.primary, primary_override);
2522        // The rest should be the same.
2523        // The rest should be the same.
2524        assert_eq!(scheme.on_primary, base_scheme.on_primary);
2525        assert_eq!(scheme.primary_container(), base_scheme.primary_container());
2526        assert_eq!(
2527            scheme.on_primary_container(),
2528            base_scheme.on_primary_container()
2529        );
2530        assert_eq!(scheme.primary_fixed(), base_scheme.primary_fixed());
2531        assert_eq!(scheme.primary_fixed_dim(), base_scheme.primary_fixed_dim());
2532        assert_eq!(scheme.on_primary_fixed(), base_scheme.on_primary_fixed());
2533        assert_eq!(
2534            scheme.on_primary_fixed_variant(),
2535            base_scheme.on_primary_fixed_variant()
2536        );
2537        assert_eq!(scheme.secondary, base_scheme.secondary);
2538        assert_eq!(scheme.on_secondary, base_scheme.on_secondary);
2539        assert_eq!(
2540            scheme.secondary_container(),
2541            base_scheme.secondary_container()
2542        );
2543        assert_eq!(
2544            scheme.on_secondary_container(),
2545            base_scheme.on_secondary_container()
2546        );
2547        assert_eq!(scheme.secondary_fixed(), base_scheme.secondary_fixed());
2548        assert_eq!(
2549            scheme.secondary_fixed_dim(),
2550            base_scheme.secondary_fixed_dim()
2551        );
2552        assert_eq!(
2553            scheme.on_secondary_fixed(),
2554            base_scheme.on_secondary_fixed()
2555        );
2556        assert_eq!(
2557            scheme.on_secondary_fixed_variant(),
2558            base_scheme.on_secondary_fixed_variant()
2559        );
2560        assert_eq!(scheme.tertiary(), base_scheme.tertiary());
2561        assert_eq!(scheme.on_tertiary(), base_scheme.on_tertiary());
2562        assert_eq!(
2563            scheme.tertiary_container(),
2564            base_scheme.tertiary_container()
2565        );
2566        assert_eq!(
2567            scheme.on_tertiary_container(),
2568            base_scheme.on_tertiary_container()
2569        );
2570        assert_eq!(scheme.tertiary_fixed(), base_scheme.tertiary_fixed());
2571        assert_eq!(
2572            scheme.tertiary_fixed_dim(),
2573            base_scheme.tertiary_fixed_dim()
2574        );
2575        assert_eq!(scheme.on_tertiary_fixed(), base_scheme.on_tertiary_fixed());
2576        assert_eq!(
2577            scheme.on_tertiary_fixed_variant(),
2578            base_scheme.on_tertiary_fixed_variant()
2579        );
2580        assert_eq!(scheme.error, base_scheme.error);
2581        assert_eq!(scheme.on_error, base_scheme.on_error);
2582        assert_eq!(scheme.error_container(), base_scheme.error_container());
2583        assert_eq!(
2584            scheme.on_error_container(),
2585            base_scheme.on_error_container()
2586        );
2587        assert_eq!(scheme.outline(), base_scheme.outline());
2588        assert_eq!(scheme.outline_variant(), base_scheme.outline_variant());
2589        assert_eq!(scheme.background(), base_scheme.background());
2590        assert_eq!(scheme.on_background(), base_scheme.on_background());
2591        assert_eq!(scheme.surface, base_scheme.surface);
2592        assert_eq!(scheme.surface_bright(), base_scheme.surface_bright());
2593        assert_eq!(scheme.surface_dim(), base_scheme.surface_dim());
2594        assert_eq!(
2595            scheme.surface_container_lowest(),
2596            base_scheme.surface_container_lowest()
2597        );
2598        assert_eq!(
2599            scheme.surface_container_low(),
2600            base_scheme.surface_container_low()
2601        );
2602        assert_eq!(scheme.surface_container(), base_scheme.surface_container());
2603        assert_eq!(
2604            scheme.surface_container_high(),
2605            base_scheme.surface_container_high()
2606        );
2607        assert_eq!(
2608            scheme.surface_container_highest(),
2609            base_scheme.surface_container_highest()
2610        );
2611        assert_eq!(scheme.on_surface, base_scheme.on_surface);
2612        assert_eq!(scheme.surface_variant(), base_scheme.surface_variant());
2613        assert_eq!(
2614            scheme.on_surface_variant(),
2615            base_scheme.on_surface_variant()
2616        );
2617        assert_eq!(scheme.inverse_surface(), base_scheme.inverse_surface());
2618        assert_eq!(
2619            scheme.on_inverse_surface(),
2620            base_scheme.on_inverse_surface()
2621        );
2622        assert_eq!(scheme.inverse_primary(), base_scheme.inverse_primary());
2623        assert_eq!(scheme.shadow(), base_scheme.shadow());
2624        assert_eq!(scheme.scrim(), base_scheme.shadow());
2625        assert_eq!(scheme.surface_tint(), base_scheme.surface_tint());
2626        assert_eq!(scheme.brightness, base_scheme.brightness);
2627    }
2628
2629    fn expect_colors_match_dynamic_scheme(
2630        color_scheme: &ColorScheme,
2631        dynamic_scheme: &DynamicScheme,
2632    ) {
2633        assert_eq!(
2634            color_scheme.primary.to_argb32(),
2635            MaterialDynamicColors::primary().get_argb(dynamic_scheme)
2636        );
2637        assert_eq!(
2638            color_scheme.on_primary.to_argb32(),
2639            MaterialDynamicColors::on_primary().get_argb(dynamic_scheme)
2640        );
2641        assert_eq!(
2642            color_scheme.primary_container().to_argb32(),
2643            MaterialDynamicColors::primary_container().get_argb(dynamic_scheme)
2644        );
2645        assert_eq!(
2646            color_scheme.on_primary_container().to_argb32(),
2647            MaterialDynamicColors::on_primary_container().get_argb(dynamic_scheme)
2648        );
2649        assert_eq!(
2650            color_scheme.primary_fixed().to_argb32(),
2651            MaterialDynamicColors::primary_fixed().get_argb(dynamic_scheme)
2652        );
2653        assert_eq!(
2654            color_scheme.primary_fixed_dim().to_argb32(),
2655            MaterialDynamicColors::primary_fixed_dim().get_argb(dynamic_scheme)
2656        );
2657        assert_eq!(
2658            color_scheme.on_primary_fixed().to_argb32(),
2659            MaterialDynamicColors::on_primary_fixed().get_argb(dynamic_scheme)
2660        );
2661        assert_eq!(
2662            color_scheme.on_primary_fixed_variant().to_argb32(),
2663            MaterialDynamicColors::on_primary_fixed_variant().get_argb(dynamic_scheme)
2664        );
2665        assert_eq!(
2666            color_scheme.secondary.to_argb32(),
2667            MaterialDynamicColors::secondary().get_argb(dynamic_scheme)
2668        );
2669        assert_eq!(
2670            color_scheme.on_secondary.to_argb32(),
2671            MaterialDynamicColors::on_secondary().get_argb(dynamic_scheme)
2672        );
2673        assert_eq!(
2674            color_scheme.secondary_container().to_argb32(),
2675            MaterialDynamicColors::secondary_container().get_argb(dynamic_scheme)
2676        );
2677        assert_eq!(
2678            color_scheme.on_secondary_container().to_argb32(),
2679            MaterialDynamicColors::on_secondary_container().get_argb(dynamic_scheme)
2680        );
2681        assert_eq!(
2682            color_scheme.secondary_fixed().to_argb32(),
2683            MaterialDynamicColors::secondary_fixed().get_argb(dynamic_scheme)
2684        );
2685        assert_eq!(
2686            color_scheme.secondary_fixed_dim().to_argb32(),
2687            MaterialDynamicColors::secondary_fixed_dim().get_argb(dynamic_scheme)
2688        );
2689        assert_eq!(
2690            color_scheme.on_secondary_fixed().to_argb32(),
2691            MaterialDynamicColors::on_secondary_fixed().get_argb(dynamic_scheme)
2692        );
2693        assert_eq!(
2694            color_scheme.on_secondary_fixed_variant().to_argb32(),
2695            MaterialDynamicColors::on_secondary_fixed_variant().get_argb(dynamic_scheme)
2696        );
2697        assert_eq!(
2698            color_scheme.tertiary().to_argb32(),
2699            MaterialDynamicColors::tertiary().get_argb(dynamic_scheme)
2700        );
2701        assert_eq!(
2702            color_scheme.on_tertiary().to_argb32(),
2703            MaterialDynamicColors::on_tertiary().get_argb(dynamic_scheme)
2704        );
2705        assert_eq!(
2706            color_scheme.tertiary_container().to_argb32(),
2707            MaterialDynamicColors::tertiary_container().get_argb(dynamic_scheme)
2708        );
2709        assert_eq!(
2710            color_scheme.on_tertiary_container().to_argb32(),
2711            MaterialDynamicColors::on_tertiary_container().get_argb(dynamic_scheme)
2712        );
2713        assert_eq!(
2714            color_scheme.tertiary_fixed().to_argb32(),
2715            MaterialDynamicColors::tertiary_fixed().get_argb(dynamic_scheme)
2716        );
2717        assert_eq!(
2718            color_scheme.tertiary_fixed_dim().to_argb32(),
2719            MaterialDynamicColors::tertiary_fixed_dim().get_argb(dynamic_scheme)
2720        );
2721        assert_eq!(
2722            color_scheme.on_tertiary_fixed().to_argb32(),
2723            MaterialDynamicColors::on_tertiary_fixed().get_argb(dynamic_scheme)
2724        );
2725        assert_eq!(
2726            color_scheme.on_tertiary_fixed_variant().to_argb32(),
2727            MaterialDynamicColors::on_tertiary_fixed_variant().get_argb(dynamic_scheme)
2728        );
2729        assert_eq!(
2730            color_scheme.error.to_argb32(),
2731            MaterialDynamicColors::error().get_argb(dynamic_scheme)
2732        );
2733        assert_eq!(
2734            color_scheme.on_error.to_argb32(),
2735            MaterialDynamicColors::on_error().get_argb(dynamic_scheme)
2736        );
2737        assert_eq!(
2738            color_scheme.error_container().to_argb32(),
2739            MaterialDynamicColors::error_container().get_argb(dynamic_scheme)
2740        );
2741        assert_eq!(
2742            color_scheme.on_error_container().to_argb32(),
2743            MaterialDynamicColors::on_error_container().get_argb(dynamic_scheme)
2744        );
2745        assert_eq!(
2746            color_scheme.background().to_argb32(),
2747            MaterialDynamicColors::background().get_argb(dynamic_scheme)
2748        );
2749        assert_eq!(
2750            color_scheme.on_background().to_argb32(),
2751            MaterialDynamicColors::on_background().get_argb(dynamic_scheme)
2752        );
2753        assert_eq!(
2754            color_scheme.surface.to_argb32(),
2755            MaterialDynamicColors::surface().get_argb(dynamic_scheme)
2756        );
2757        assert_eq!(
2758            color_scheme.surface_dim().to_argb32(),
2759            MaterialDynamicColors::surface_dim().get_argb(dynamic_scheme)
2760        );
2761        assert_eq!(
2762            color_scheme.surface_bright().to_argb32(),
2763            MaterialDynamicColors::surface_bright().get_argb(dynamic_scheme)
2764        );
2765        assert_eq!(
2766            color_scheme.surface_container_lowest().to_argb32(),
2767            MaterialDynamicColors::surface_container_lowest().get_argb(dynamic_scheme)
2768        );
2769        assert_eq!(
2770            color_scheme.surface_container_low().to_argb32(),
2771            MaterialDynamicColors::surface_container_low().get_argb(dynamic_scheme)
2772        );
2773        assert_eq!(
2774            color_scheme.surface_container().to_argb32(),
2775            MaterialDynamicColors::surface_container().get_argb(dynamic_scheme)
2776        );
2777        assert_eq!(
2778            color_scheme.surface_container_high().to_argb32(),
2779            MaterialDynamicColors::surface_container_high().get_argb(dynamic_scheme)
2780        );
2781        assert_eq!(
2782            color_scheme.surface_container_highest().to_argb32(),
2783            MaterialDynamicColors::surface_container_highest().get_argb(dynamic_scheme)
2784        );
2785        assert_eq!(
2786            color_scheme.on_surface.to_argb32(),
2787            MaterialDynamicColors::on_surface().get_argb(dynamic_scheme)
2788        );
2789        assert_eq!(
2790            color_scheme.surface_variant().to_argb32(),
2791            MaterialDynamicColors::surface_variant().get_argb(dynamic_scheme)
2792        );
2793        assert_eq!(
2794            color_scheme.on_surface_variant().to_argb32(),
2795            MaterialDynamicColors::on_surface_variant().get_argb(dynamic_scheme)
2796        );
2797        assert_eq!(
2798            color_scheme.outline().to_argb32(),
2799            MaterialDynamicColors::outline().get_argb(dynamic_scheme)
2800        );
2801        assert_eq!(
2802            color_scheme.outline_variant().to_argb32(),
2803            MaterialDynamicColors::outline_variant().get_argb(dynamic_scheme)
2804        );
2805        assert_eq!(
2806            color_scheme.shadow().to_argb32(),
2807            MaterialDynamicColors::shadow().get_argb(dynamic_scheme)
2808        );
2809        assert_eq!(
2810            color_scheme.scrim().to_argb32(),
2811            MaterialDynamicColors::scrim().get_argb(dynamic_scheme)
2812        );
2813        assert_eq!(
2814            color_scheme.inverse_surface().to_argb32(),
2815            MaterialDynamicColors::inverse_surface().get_argb(dynamic_scheme)
2816        );
2817        assert_eq!(
2818            color_scheme.on_inverse_surface().to_argb32(),
2819            MaterialDynamicColors::inverse_on_surface().get_argb(dynamic_scheme)
2820        );
2821        assert_eq!(
2822            color_scheme.inverse_primary().to_argb32(),
2823            MaterialDynamicColors::inverse_primary().get_argb(dynamic_scheme)
2824        );
2825    }
2826
2827    #[test]
2828    fn from_seed_asserts_on_invalid_contrast_levels() {
2829        let below = catch_unwind(AssertUnwindSafe(|| {
2830            ColorScheme::from_seed(Colors::BLUE.primary())
2831                .contrast_level(-1.5)
2832                .build()
2833        }));
2834        assert!(below.is_err());
2835
2836        let above = catch_unwind(AssertUnwindSafe(|| {
2837            ColorScheme::from_seed(Colors::BLUE.primary())
2838                .contrast_level(1.5)
2839                .build()
2840        }));
2841        assert!(above.is_err());
2842    }
2843
2844    fn dynamic_scheme_for(
2845        variant: DynamicSchemeVariant,
2846        source_color: Hct,
2847        is_dark: bool,
2848        contrast_level: f64,
2849    ) -> DynamicScheme {
2850        match variant {
2851            DynamicSchemeVariant::TonalSpot => {
2852                SchemeTonalSpot::new(source_color, is_dark, contrast_level)
2853            }
2854            DynamicSchemeVariant::Fidelity => {
2855                SchemeFidelity::new(source_color, is_dark, contrast_level)
2856            }
2857            DynamicSchemeVariant::Content => {
2858                SchemeContent::new(source_color, is_dark, contrast_level)
2859            }
2860            DynamicSchemeVariant::Monochrome => {
2861                SchemeMonochrome::new(source_color, is_dark, contrast_level)
2862            }
2863            DynamicSchemeVariant::Neutral => {
2864                SchemeNeutral::new(source_color, is_dark, contrast_level)
2865            }
2866            DynamicSchemeVariant::Vibrant => {
2867                SchemeVibrant::new(source_color, is_dark, contrast_level)
2868            }
2869            DynamicSchemeVariant::Expressive => {
2870                SchemeExpressive::new(source_color, is_dark, contrast_level)
2871            }
2872            DynamicSchemeVariant::Rainbow => {
2873                SchemeRainbow::new(source_color, is_dark, contrast_level)
2874            }
2875            DynamicSchemeVariant::FruitSalad => {
2876                SchemeFruitSalad::new(source_color, is_dark, contrast_level)
2877            }
2878        }
2879    }
2880
2881    #[test]
2882    fn color_values_in_from_seed_with_different_variants_match_values_in_dynamic_scheme() {
2883        let seed_color = Colors::ORANGE.primary();
2884        let source_color = Hct::from_int(seed_color.to_argb32());
2885        for scheme_variant in DynamicSchemeVariant::VALUES {
2886            let dynamic_scheme = dynamic_scheme_for(scheme_variant, source_color, false, 0.0);
2887            let color_scheme = ColorScheme::from_seed(seed_color)
2888                .dynamic_scheme_variant(scheme_variant)
2889                .build();
2890
2891            expect_colors_match_dynamic_scheme(&color_scheme, &dynamic_scheme);
2892        }
2893    }
2894
2895    #[test]
2896    fn colors_in_high_contrast_color_scheme_match_colors_in_dynamic_scheme() {
2897        let seed_color = Colors::BLUE.primary();
2898        let source_color = Hct::from_int(seed_color.to_argb32());
2899
2900        let colors_match_dynamic_scheme_colors =
2901            |scheme_variant: DynamicSchemeVariant, brightness: Brightness, contrast_level: f64| {
2902                let is_dark = brightness == Brightness::Dark;
2903                let dynamic_scheme =
2904                    dynamic_scheme_for(scheme_variant, source_color, is_dark, contrast_level);
2905
2906                let color_scheme = ColorScheme::from_seed(seed_color)
2907                    .brightness(brightness)
2908                    .dynamic_scheme_variant(scheme_variant)
2909                    .contrast_level(contrast_level)
2910                    .build();
2911
2912                expect_colors_match_dynamic_scheme(&color_scheme, &dynamic_scheme);
2913            };
2914
2915        for scheme_variant in DynamicSchemeVariant::VALUES {
2916            colors_match_dynamic_scheme_colors(scheme_variant, Brightness::Light, 1.0); // High contrast
2917            colors_match_dynamic_scheme_colors(scheme_variant, Brightness::Dark, 1.0);
2918
2919            colors_match_dynamic_scheme_colors(scheme_variant, Brightness::Light, 0.5); // Medium contrast
2920            colors_match_dynamic_scheme_colors(scheme_variant, Brightness::Dark, 0.5);
2921        }
2922    }
2923
2924    /// Dart's `_testFilledButtonColor`: the colour of the `Material` a `FilledButton` builds
2925    /// under `ThemeData(colorScheme: scheme)`.
2926    fn test_filled_button_color(
2927        tester: &mut WidgetTester,
2928        scheme: ColorScheme,
2929        expectation: Color,
2930    ) {
2931        tester.pump_widget(Container::new().into_widget()); // reset
2932        let theme = ThemeData::builder()
2933            .color_scheme(scheme)
2934            .build(&tester.cell.borrow());
2935        tester.pump_widget(material_app(
2936            theme,
2937            FilledButton::new(
2938                Some(Listener::new(|_app| {})),
2939                Some(SizedBox::square(Some(200.0))),
2940            )
2941            .into_widget(),
2942        ));
2943        let button = find::by_type::<FilledButton>().evaluate(tester);
2944        assert_eq!(button.len(), 1);
2945        let materials: Vec<_> = find::by_type::<Material>()
2946            .evaluate(tester)
2947            .into_iter()
2948            .filter(|material| tester.ancestors(*material).contains(&button[0]))
2949            .collect();
2950        assert_eq!(materials.len(), 1);
2951        let color = tester.widget::<Material, _>(materials[0], |material| material.color);
2952        assert_eq!(color, Some(expectation));
2953    }
2954
2955    #[test]
2956    fn color_scheme_from_seed_with_different_variants_spot_checks() {
2957        test_widgets(|tester| {
2958            // Default (Variant.tonalSpot).
2959            for (seed, expectation) in [
2960                (0xFF000000, 0xFF8C4A60),
2961                (0xFF00FF00, 0xFF406836),
2962                (0xFF6559F5, 0xFF5B5891),
2963                (0xFFFFFFFF, 0xFF006874),
2964            ] {
2965                test_filled_button_color(
2966                    tester,
2967                    ColorScheme::from_seed(Color::new(seed)).build(),
2968                    Color::new(expectation),
2969                );
2970            }
2971
2972            // Variant.fidelity.
2973            for (seed, expectation) in [
2974                (0xFF000000, 0xFF000000),
2975                (0xFF00FF00, 0xFF026E00),
2976                (0xFF6559F5, 0xFF4C3CDB),
2977                (0xFFFFFFFF, 0xFF5D5F5F),
2978            ] {
2979                test_filled_button_color(
2980                    tester,
2981                    ColorScheme::from_seed(Color::new(seed))
2982                        .dynamic_scheme_variant(DynamicSchemeVariant::Fidelity)
2983                        .build(),
2984                    Color::new(expectation),
2985                );
2986            }
2987        });
2988    }
2989
2990    #[test]
2991    fn color_scheme_of_context_is_equivalent_to_theme_of_context_color_scheme() {
2992        test_widgets(|tester| {
2993            let color_scheme = ColorScheme::from_seed(Colors::RED.primary()).build();
2994            let seen: Rc<RefCell<Option<(ColorScheme, ColorScheme)>>> = Rc::default();
2995            let theme = ThemeData::from(color_scheme.clone()).build(&tester.cell.borrow());
2996            tester.pump_widget(material_app(
2997                theme,
2998                Builder::new({
2999                    let seen = Rc::clone(&seen);
3000                    move |app, context| {
3001                        let color_scheme_of_theme = Theme::of(app, context).color_scheme.clone();
3002                        let color_scheme_from_context = ColorScheme::of(app, context);
3003                        *seen.borrow_mut() =
3004                            Some((color_scheme_of_theme, color_scheme_from_context));
3005                        SizedBox::shrink().into_widget()
3006                    }
3007                })
3008                .into_widget(),
3009            ));
3010            let (color_scheme_of_theme, color_scheme_from_context) =
3011                seen.borrow_mut().take().expect("the probe built");
3012            assert_eq!(color_scheme_of_theme, color_scheme);
3013            assert_eq!(color_scheme_from_context, color_scheme);
3014        });
3015    }
3016}