mcu-dynamiccolor 0.2.2

Dynamic color system for Material Design 3
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// <FILE>crates/mcu-dynamiccolor/src/dynamic_color.rs</FILE> - <DESC>Dynamic color definition and resolution</DESC>
// <VERS>VERSION: 2.3.0</VERS>
// <WCTX>F-014: Make DynamicColor::from_palette return Result instead of panicking</WCTX>
// <CLOG>Add DynamicColorError enum and try_from_palette() for fallible construction; from_palette() now delegates to try_from_palette()</CLOG>

use mcu_contrast::Contrast;
use mcu_hct::Hct;
use mcu_palettes::TonalPalette;
use std::sync::Arc;

use crate::color_calculation::get_spec;
use crate::{ContrastCurve, ToneDeltaPair};

/// Errors that can occur when creating a DynamicColor
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DynamicColorError {
    /// second_background was provided without background
    MissingBackground { color_name: String },
    /// contrast_curve was provided without background
    MissingBackgroundForContrast { color_name: String },
    /// background was provided without contrast_curve
    MissingContrastCurve { color_name: String },
}

impl std::fmt::Display for DynamicColorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::MissingBackground { color_name } => {
                write!(
                    f,
                    "Color '{}' has second_background but no background",
                    color_name
                )
            }
            Self::MissingBackgroundForContrast { color_name } => {
                write!(
                    f,
                    "Color '{}' has contrast_curve but no background",
                    color_name
                )
            }
            Self::MissingContrastCurve { color_name } => {
                write!(
                    f,
                    "Color '{}' has background but no contrast_curve",
                    color_name
                )
            }
        }
    }
}

impl std::error::Error for DynamicColorError {}

// Import DynamicScheme from dynamic_scheme module
pub use crate::dynamic_scheme::DynamicScheme;

/// A color that adjusts itself based on UI state provided by DynamicScheme.
///
/// Colors without backgrounds do not change tone when contrast changes. Colors
/// with backgrounds become closer to their background as contrast lowers, and
/// further when contrast increases.
///
/// # Example
///
/// ```ignore
/// use mcu_dynamiccolor::DynamicColor;
/// use mcu_palettes::TonalPalette;
///
/// let color = DynamicColor::from_palette(
///     "primary",
///     |scheme| scheme.primary_palette.clone(),
///     Some(|_scheme| 40.0),
///     false,
///     None,
///     None,
///     None,
///     None,
///     None,
/// );
/// ```
#[derive(Clone)]
pub struct DynamicColor {
    /// The name of the dynamic color
    pub name: String,

    /// Function that provides a TonalPalette given DynamicScheme
    pub palette: Arc<dyn Fn(&DynamicScheme) -> TonalPalette + Send + Sync>,

    /// Function that provides a tone given DynamicScheme
    pub tone: Arc<dyn Fn(&DynamicScheme) -> f64 + Send + Sync>,

    /// Whether this dynamic color is a background
    pub is_background: bool,

    /// A factor that multiplies the chroma for this color
    pub chroma_multiplier: Option<Arc<dyn Fn(&DynamicScheme) -> f64 + Send + Sync>>,

    /// The background of the dynamic color, if it exists
    pub background: Option<Arc<dyn Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync>>,

    /// A second background of the dynamic color, if it exists
    pub second_background:
        Option<Arc<dyn Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync>>,

    /// A ContrastCurve object specifying how its contrast against its background should behave
    pub contrast_curve: Option<Arc<dyn Fn(&DynamicScheme) -> Option<ContrastCurve> + Send + Sync>>,

    /// A ToneDeltaPair object specifying a tone delta constraint between two colors
    pub tone_delta_pair: Option<Arc<dyn Fn(&DynamicScheme) -> Option<ToneDeltaPair> + Send + Sync>>,
}

impl DynamicColor {
    /// Create a DynamicColor defined by a TonalPalette and HCT tone.
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the dynamic color
    /// * `palette` - Function that provides a TonalPalette given DynamicScheme
    /// * `tone` - Function that provides a tone given DynamicScheme (if None, defaults to background tone or 50)
    /// * `is_background` - Whether this dynamic color is a background
    /// * `chroma_multiplier` - Optional function that provides a chroma multiplier
    /// * `background` - Optional function that provides the background DynamicColor
    /// * `second_background` - Optional function that provides a second background
    /// * `contrast_curve` - Optional function that provides a ContrastCurve
    /// * `tone_delta_pair` - Optional function that provides a ToneDeltaPair
    ///
    /// # Panics
    ///
    /// Panics if the configuration is invalid. Use [`try_from_palette`](Self::try_from_palette)
    /// for fallible construction.
    #[allow(clippy::too_many_arguments)]
    pub fn from_palette(
        name: impl Into<String>,
        palette: impl Fn(&DynamicScheme) -> TonalPalette + Send + Sync + 'static,
        tone: Option<impl Fn(&DynamicScheme) -> f64 + Send + Sync + 'static>,
        is_background: bool,
        chroma_multiplier: Option<impl Fn(&DynamicScheme) -> f64 + Send + Sync + 'static>,
        background: Option<impl Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync + 'static>,
        second_background: Option<
            impl Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync + 'static,
        >,
        contrast_curve: Option<
            impl Fn(&DynamicScheme) -> Option<ContrastCurve> + Send + Sync + 'static,
        >,
        tone_delta_pair: Option<
            impl Fn(&DynamicScheme) -> Option<ToneDeltaPair> + Send + Sync + 'static,
        >,
    ) -> Self {
        Self::try_from_palette(
            name,
            palette,
            tone,
            is_background,
            chroma_multiplier,
            background,
            second_background,
            contrast_curve,
            tone_delta_pair,
        )
        .expect("invalid DynamicColor configuration")
    }

    /// Try to create a DynamicColor defined by a TonalPalette and HCT tone.
    ///
    /// This is the fallible version of [`from_palette`](Self::from_palette).
    ///
    /// # Arguments
    ///
    /// * `name` - The name of the dynamic color
    /// * `palette` - Function that provides a TonalPalette given DynamicScheme
    /// * `tone` - Function that provides a tone given DynamicScheme (if None, defaults to 50)
    /// * `is_background` - Whether this dynamic color is a background
    /// * `chroma_multiplier` - Optional function that provides a chroma multiplier
    /// * `background` - Optional function that provides the background DynamicColor
    /// * `second_background` - Optional function that provides a second background
    /// * `contrast_curve` - Optional function that provides a ContrastCurve
    /// * `tone_delta_pair` - Optional function that provides a ToneDeltaPair
    ///
    /// # Errors
    ///
    /// Returns [`DynamicColorError`] if:
    /// - `second_background` is provided without `background`
    /// - `contrast_curve` is provided without `background`
    /// - `background` is provided without `contrast_curve`
    #[allow(clippy::too_many_arguments)]
    pub fn try_from_palette(
        name: impl Into<String>,
        palette: impl Fn(&DynamicScheme) -> TonalPalette + Send + Sync + 'static,
        tone: Option<impl Fn(&DynamicScheme) -> f64 + Send + Sync + 'static>,
        is_background: bool,
        chroma_multiplier: Option<impl Fn(&DynamicScheme) -> f64 + Send + Sync + 'static>,
        background: Option<impl Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync + 'static>,
        second_background: Option<
            impl Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync + 'static,
        >,
        contrast_curve: Option<
            impl Fn(&DynamicScheme) -> Option<ContrastCurve> + Send + Sync + 'static,
        >,
        tone_delta_pair: Option<
            impl Fn(&DynamicScheme) -> Option<ToneDeltaPair> + Send + Sync + 'static,
        >,
    ) -> Result<Self, DynamicColorError> {
        let name = name.into();

        // Validate constraints
        if background.is_none() && second_background.is_some() {
            return Err(DynamicColorError::MissingBackground { color_name: name });
        }
        if background.is_none() && contrast_curve.is_some() {
            return Err(DynamicColorError::MissingBackgroundForContrast { color_name: name });
        }
        if background.is_some() && contrast_curve.is_none() {
            return Err(DynamicColorError::MissingContrastCurve { color_name: name });
        }

        // Convert tone to use default if not provided
        let tone_fn: Arc<dyn Fn(&DynamicScheme) -> f64 + Send + Sync> = match tone {
            Some(t) => Arc::new(t),
            None => {
                // Default tone: background tone or 50.0
                Arc::new(|_scheme: &DynamicScheme| 50.0)
            }
        };

        Ok(DynamicColor {
            name,
            palette: Arc::new(palette),
            tone: tone_fn,
            is_background,
            chroma_multiplier: chroma_multiplier
                .map(|f| Arc::new(f) as Arc<dyn Fn(&DynamicScheme) -> f64 + Send + Sync>),
            background: background.map(|f| {
                Arc::new(f) as Arc<dyn Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync>
            }),
            second_background: second_background.map(|f| {
                Arc::new(f) as Arc<dyn Fn(&DynamicScheme) -> Option<DynamicColor> + Send + Sync>
            }),
            contrast_curve: contrast_curve.map(|f| {
                Arc::new(f) as Arc<dyn Fn(&DynamicScheme) -> Option<ContrastCurve> + Send + Sync>
            }),
            tone_delta_pair: tone_delta_pair.map(|f| {
                Arc::new(f) as Arc<dyn Fn(&DynamicScheme) -> Option<ToneDeltaPair> + Send + Sync>
            }),
        })
    }

    /// Returns an ARGB integer (i.e., a hex code).
    ///
    /// # Arguments
    ///
    /// * `scheme` - Defines the conditions of the user interface
    ///
    /// # Returns
    ///
    /// ARGB color as a 32-bit integer
    pub fn get_argb(&self, scheme: &DynamicScheme) -> u32 {
        self.get_hct(scheme).to_int()
    }

    /// Returns a color, expressed in the HCT color space.
    ///
    /// Delegates to the spec-versioned ColorCalculationDelegate for the actual
    /// calculation. The delegate handles contrast adjustments, tone delta pairs,
    /// and spec-specific behavior (e.g., 2025 spec chroma multiplier).
    ///
    /// # Arguments
    ///
    /// * `scheme` - Defines the conditions of the user interface
    ///
    /// # Returns
    ///
    /// HCT color
    pub fn get_hct(&self, scheme: &DynamicScheme) -> Hct {
        // Delegate to spec-versioned implementation
        get_spec(scheme.spec_version).get_hct(scheme, self)
    }

    /// Returns a tone, T in the HCT color space.
    ///
    /// Delegates to the spec-versioned ColorCalculationDelegate for the actual
    /// calculation. The delegate handles contrast adjustments, tone delta pairs,
    /// awkward zone handling, and spec-specific behavior.
    ///
    /// # Arguments
    ///
    /// * `scheme` - Defines the conditions of the user interface
    ///
    /// # Returns
    ///
    /// Tone value (0.0 to 100.0)
    pub fn get_tone(&self, scheme: &DynamicScheme) -> f64 {
        // Delegate to spec-versioned implementation
        get_spec(scheme.spec_version).get_tone(scheme, self)
    }

    /// Given a background tone, finds a foreground tone that achieves
    /// the target contrast ratio as closely as possible.
    ///
    /// # Arguments
    ///
    /// * `bg_tone` - The tone of the background (0.0 to 100.0)
    /// * `ratio` - The target contrast ratio (e.g., 4.5 for WCAG AA)
    ///
    /// # Returns
    ///
    /// A tone value that achieves the target contrast ratio as closely as possible
    pub fn foreground_tone(bg_tone: f64, ratio: f64) -> f64 {
        let lighter_tone = Contrast::lighter_clamped(bg_tone, ratio);
        let darker_tone = Contrast::darker_clamped(bg_tone, ratio);
        let lighter_ratio = Contrast::ratio_of_tones(lighter_tone, bg_tone);
        let darker_ratio = Contrast::ratio_of_tones(darker_tone, bg_tone);
        let prefers_lighter = Self::tone_prefers_light_foreground(bg_tone);

        if prefers_lighter {
            // Handle edge case where both fail to reach ratio
            let negligible_diff = (lighter_ratio - darker_ratio).abs() < 0.1
                && lighter_ratio < ratio
                && darker_ratio < ratio;
            if lighter_ratio >= ratio || lighter_ratio >= darker_ratio || negligible_diff {
                lighter_tone
            } else {
                darker_tone
            }
        } else {
            if darker_ratio >= ratio || darker_ratio >= lighter_ratio {
                darker_tone
            } else {
                lighter_tone
            }
        }
    }

    /// Returns whether the tone prefers a light foreground.
    ///
    /// People prefer white foregrounds on ~T60-70. Humans turn out to prefer
    /// having a dark background with light foreground on tones below 60.
    ///
    /// # Arguments
    ///
    /// * `tone` - The tone to check (0.0 to 100.0)
    ///
    /// # Returns
    ///
    /// True if the tone prefers a light foreground, false otherwise
    pub fn tone_prefers_light_foreground(tone: f64) -> bool {
        tone.round() < 60.0
    }

    /// Returns whether the tone can reach 4.5:1 contrast with a lighter color.
    ///
    /// # Arguments
    ///
    /// * `tone` - The tone to check (0.0 to 100.0)
    ///
    /// # Returns
    ///
    /// True if the tone can achieve 4.5:1 contrast with white, false otherwise
    pub fn tone_allows_light_foreground(tone: f64) -> bool {
        tone.round() <= 49.0
    }

    /// Adjusts a tone such that white has 4.5 contrast, if the tone
    /// is reasonably close to supporting it.
    ///
    /// # Arguments
    ///
    /// * `tone` - The tone to potentially adjust (0.0 to 100.0)
    ///
    /// # Returns
    ///
    /// The adjusted tone, or the original tone if no adjustment is needed
    pub fn enable_light_foreground(tone: f64) -> f64 {
        if Self::tone_prefers_light_foreground(tone) && !Self::tone_allows_light_foreground(tone) {
            49.0
        } else {
            tone
        }
    }
}

// Note: Debug implementation omitted for function pointers
// Clone is already derived above

#[cfg(test)]
mod tests {
    use super::*;

    // Helper functions for tests
    fn test_palette(_scheme: &DynamicScheme) -> TonalPalette {
        TonalPalette::from_hue_and_chroma(200.0, 50.0)
    }

    fn test_tone(_scheme: &DynamicScheme) -> f64 {
        40.0
    }

    fn surface_tone(_scheme: &DynamicScheme) -> f64 {
        95.0
    }

    // Basic tests for DynamicColor structure
    // More complex tests with actual resolution will be added when
    // the full contrast calculation logic is implemented

    #[test]
    fn test_dynamic_color_can_be_created() {
        let color = DynamicColor::from_palette(
            "test_color",
            test_palette,
            Some(test_tone as fn(&DynamicScheme) -> f64),
            false,
            None::<fn(&DynamicScheme) -> f64>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<ContrastCurve>>,
            None::<fn(&DynamicScheme) -> Option<ToneDeltaPair>>,
        );

        assert_eq!(color.name, "test_color");
        assert!(!color.is_background);
    }

    #[test]
    fn test_dynamic_color_is_background() {
        let color = DynamicColor::from_palette(
            "surface",
            test_palette,
            Some(surface_tone as fn(&DynamicScheme) -> f64),
            true, // is_background
            None::<fn(&DynamicScheme) -> f64>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<ContrastCurve>>,
            None::<fn(&DynamicScheme) -> Option<ToneDeltaPair>>,
        );

        assert!(color.is_background);
    }

    #[test]
    fn test_dynamic_color_clone() {
        let color1 = DynamicColor::from_palette(
            "test",
            test_palette,
            Some(test_tone as fn(&DynamicScheme) -> f64),
            false,
            None::<fn(&DynamicScheme) -> f64>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<DynamicColor>>,
            None::<fn(&DynamicScheme) -> Option<ContrastCurve>>,
            None::<fn(&DynamicScheme) -> Option<ToneDeltaPair>>,
        );

        let color2 = color1.clone();
        assert_eq!(color1.name, color2.name);
        assert_eq!(color1.is_background, color2.is_background);
    }
}

// <FILE>crates/mcu-dynamiccolor/src/dynamic_color.rs</FILE> - <DESC>Dynamic color definition and resolution</DESC>
// <VERS>END OF VERSION: 2.3.0</VERS>