Skip to main content

iced_cube/data/calendar/
style.rs

1//! Colours of the calendar's parts, resolved from the theme's tokens.
2
3use iced::widget::button::Status;
4use iced::widget::{button, container};
5use iced::{Background, Border, Color, Shadow};
6
7use super::entry::{Emphasis, Paint, Tone};
8use crate::theme::{Tokens, contrast, fade, mix, on, radius, readable};
9
10/// The colour a [`Tone`] stands for.
11pub fn tone(tokens: &Tokens, tone: Tone) -> Color {
12    tone.colour(tokens)
13}
14
15/// The contrast a colour of the app's own keeps against the background, so
16/// its stripe, outline and dots never vanish into the page. It is low on
17/// purpose: a pale calendar colour keeps its hue, and only one too close
18/// to the page to see, such as white on white, is moved.
19pub const MARK_CONTRAST: f32 = 1.5;
20
21/// The colour a [`Paint`] stands for: its tone's, or its own made opaque
22/// and, where it is too close to the background to see, moved just far
23/// enough towards the foreground to reach [`MARK_CONTRAST`].
24pub fn paint(tokens: &Tokens, paint: impl Into<Paint>) -> Color {
25    match paint.into() {
26        Paint::Tone(entry_tone) => tone(tokens, entry_tone),
27        Paint::Colour(colour) => visible(Color { a: 1.0, ..colour }, tokens.background),
28    }
29}
30
31/// `colour`, moved towards near-black or near-white, whichever stands out
32/// from `background`, until it reaches [`MARK_CONTRAST`].
33fn visible(colour: Color, background: Color) -> Color {
34    let towards = on(background);
35    (0..=20)
36        .map(|step| mix(colour, towards, step as f32 / 20.0))
37        .find(|candidate| contrast(*candidate, background) >= MARK_CONTRAST)
38        .unwrap_or(towards)
39}
40
41/// `text` for drawing on `background` in an entry painted `with`. The
42/// theme's tones already read on their washes, so only a colour of the
43/// app's own moves the text towards whatever reads.
44fn legible(with: Paint, text: Color, background: Color) -> Color {
45    match with {
46        Paint::Tone(_) => text,
47        Paint::Colour(_) => readable(text, background),
48    }
49}
50
51/// The colours of an entry, as a bar in the month or a block in the day.
52#[derive(Debug, Clone, Copy, PartialEq)]
53pub struct Chip {
54    pub background: Color,
55    pub foreground: Color,
56    /// The stripe down its leading edge.
57    pub stripe: Color,
58}
59
60/// An entry is a wash of its paint with a solid stripe, deeper under the
61/// pointer. The selected entry is filled with the paint. A disabled entry
62/// looks like one at rest.
63pub fn chip(tokens: &Tokens, with: impl Into<Paint>, selected: bool, status: Status) -> Chip {
64    let with = with.into();
65    let colour = paint(tokens, with);
66    if selected {
67        return Chip {
68            background: colour,
69            foreground: on(colour),
70            stripe: colour,
71        };
72    }
73    let wash = match status {
74        Status::Active | Status::Disabled => 0.16,
75        Status::Hovered => 0.26,
76        Status::Pressed => 0.34,
77    };
78    // A calendar without a handler is read-only, not unavailable, so its
79    // entries keep their full colour.
80    let background = mix(tokens.background, colour, wash);
81    Chip {
82        background,
83        foreground: legible(with, tokens.foreground, background),
84        stripe: colour,
85    }
86}
87
88/// How an entry is to be drawn, besides its tone.
89#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
90pub struct Look {
91    pub selected: bool,
92    pub emphasis: Emphasis,
93    /// Drawn as the preview of a change waiting to be confirmed.
94    pub preview: bool,
95}
96
97impl Look {
98    /// Whether stripes are drawn over the fill. A selected entry or a
99    /// preview has none, so its text stays readable.
100    pub fn hatched(self) -> bool {
101        self.emphasis == Emphasis::Hatched && !self.selected && !self.preview
102    }
103
104    /// Whether a line is drawn through the title.
105    pub fn struck(self) -> bool {
106        self.emphasis == Emphasis::Struck
107    }
108}
109
110/// How strong the stripes of a hatched entry are.
111pub const HATCH_ALPHA: f32 = 0.35;
112
113/// The colours of an entry with its [`Emphasis`], as a bar or a block.
114#[derive(Debug, Clone, Copy, PartialEq)]
115pub struct Marked {
116    pub chip: Chip,
117    /// A border round the whole entry, transparent when it has none.
118    pub border: Color,
119    /// The colour of diagonal stripes over the fill.
120    pub hatch: Option<Color>,
121    /// The colour of a line through the title.
122    pub strike: Option<Color>,
123}
124
125/// An entry's colours for its look. Solid is [`chip`]. Outlined has a
126/// border in its paint over the page, Struck and Faded take muted text,
127/// Hatched lays stripes of its paint over the wash, and a preview is a
128/// faint outline of the primary colour. The selected entry is filled
129/// with its paint whatever its emphasis, keeping any strike.
130pub fn marked(tokens: &Tokens, with: impl Into<Paint>, look: Look, status: Status) -> Marked {
131    let with = with.into();
132    let colour = paint(tokens, with);
133    let solid = chip(tokens, with, look.selected, status);
134    let mut marked = Marked {
135        chip: solid,
136        border: Color::TRANSPARENT,
137        hatch: None,
138        strike: None,
139    };
140    if look.preview {
141        let colours = preview(tokens);
142        marked.chip = Chip {
143            background: colours.background,
144            foreground: tokens.muted_foreground,
145            stripe: colours.border,
146        };
147        marked.border = colours.border;
148        if look.struck() {
149            marked.strike = Some(tokens.muted_foreground);
150        }
151        return marked;
152    }
153    if look.selected {
154        if look.struck() {
155            marked.strike = Some(solid.foreground);
156        }
157        return marked;
158    }
159    match look.emphasis {
160        Emphasis::Solid => {}
161        Emphasis::Outlined => {
162            let hovered = matches!(status, Status::Hovered | Status::Pressed);
163            marked.chip.background = if hovered {
164                mix(tokens.background, colour, 0.12)
165            } else {
166                tokens.background
167            };
168            marked.chip.foreground = legible(with, tokens.foreground, marked.chip.background);
169            marked.border = colour;
170        }
171        Emphasis::Struck => {
172            let muted = legible(with, tokens.muted_foreground, marked.chip.background);
173            marked.chip.foreground = muted;
174            marked.strike = Some(muted);
175        }
176        Emphasis::Faded => {
177            marked.chip.background = mix(tokens.background, solid.background, 0.5);
178            marked.chip.foreground = legible(with, tokens.muted_foreground, marked.chip.background);
179            marked.chip.stripe = mix(tokens.background, colour, 0.5);
180        }
181        Emphasis::Hatched => marked.hatch = Some(fade(colour, HATCH_ALPHA)),
182    }
183    marked
184}
185
186/// The button style of an entry drawn with [`marked`] colours.
187pub fn marked_button(
188    tokens: &Tokens,
189    with: impl Into<Paint>,
190    look: Look,
191    status: Status,
192) -> button::Style {
193    let marked = marked(tokens, with, look, status);
194    button::Style {
195        background: Some(Background::Color(marked.chip.background)),
196        text_color: marked.chip.foreground,
197        border: Border {
198            color: marked.border,
199            width: if marked.border == Color::TRANSPARENT {
200                0.0
201            } else {
202                1.0
203            },
204            radius: radius::SM.into(),
205        },
206        shadow: Shadow::default(),
207        snap: true,
208    }
209}
210
211/// The button style of an entry drawn with [`chip`] colours.
212pub fn chip_button(
213    tokens: &Tokens,
214    with: impl Into<Paint>,
215    selected: bool,
216    status: Status,
217) -> button::Style {
218    let chip = chip(tokens, with, selected, status);
219    button::Style {
220        background: Some(Background::Color(chip.background)),
221        text_color: chip.foreground,
222        border: Border {
223            radius: radius::SM.into(),
224            ..Border::default()
225        },
226        shadow: Shadow::default(),
227        snap: true,
228    }
229}
230
231/// What a day in the month grid is.
232#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
233pub struct Day {
234    pub today: bool,
235    pub selected: bool,
236    /// Whether the day belongs to the month on show.
237    pub in_month: bool,
238}
239
240/// The colours of a day's number: a filled pill for today.
241#[derive(Debug, Clone, Copy, PartialEq)]
242pub struct DayNumber {
243    pub background: Option<Color>,
244    pub foreground: Color,
245}
246
247pub fn day_number(tokens: &Tokens, day: Day) -> DayNumber {
248    if day.today {
249        return DayNumber {
250            background: Some(tokens.primary),
251            foreground: tokens.primary_foreground,
252        };
253    }
254    DayNumber {
255        background: None,
256        foreground: if day.in_month {
257            tokens.foreground
258        } else {
259            tokens.muted_foreground
260        },
261    }
262}
263
264/// The surface of a day cell, which is a button. The selected day takes
265/// the accent with a ring, a day under the pointer a lighter accent, and
266/// days outside the month sit back.
267pub fn cell(tokens: &Tokens, day: Day, status: Status) -> button::Style {
268    let hovered = matches!(status, Status::Hovered | Status::Pressed);
269    let background = if day.selected {
270        Some(tokens.accent)
271    } else if hovered {
272        Some(mix(tokens.background, tokens.accent, 0.6))
273    } else if !day.in_month {
274        Some(mix(tokens.background, tokens.muted, 0.4))
275    } else {
276        None
277    };
278    button::Style {
279        background: background.map(Background::Color),
280        text_color: day_number(tokens, day).foreground,
281        border: Border {
282            color: if day.selected {
283                tokens.ring
284            } else {
285                Color::TRANSPARENT
286            },
287            width: 1.0,
288            radius: 0.0.into(),
289        },
290        shadow: Shadow::default(),
291        snap: true,
292    }
293}
294
295/// A day in the year view: a circle filled for today, ringed when
296/// selected and tinted under the pointer.
297pub fn year_day(tokens: &Tokens, day: Day, status: Status) -> button::Style {
298    let hovered = matches!(status, Status::Hovered | Status::Pressed);
299    let number = day_number(tokens, day);
300    let background = if day.today {
301        number.background
302    } else if day.selected || hovered {
303        Some(tokens.accent)
304    } else {
305        None
306    };
307    button::Style {
308        background: background.map(Background::Color),
309        text_color: number.foreground,
310        border: Border {
311            color: if day.selected {
312                tokens.ring
313            } else {
314                Color::TRANSPARENT
315            },
316            width: 1.0,
317            radius: radius::FULL.into(),
318        },
319        shadow: Shadow::default(),
320        snap: true,
321    }
322}
323
324/// A row in the agenda, or a day in the strip above a narrow week.
325pub fn row(tokens: &Tokens, selected: bool, hovered: bool) -> container::Style {
326    let background = if selected {
327        Some(tokens.accent)
328    } else if hovered {
329        Some(mix(tokens.background, tokens.accent, 0.6))
330    } else {
331        None
332    };
333    container::Style {
334        background: background.map(Background::Color),
335        border: Border {
336            radius: radius::MD.into(),
337            ..Border::default()
338        },
339        ..container::Style::default()
340    }
341}
342
343/// The frame around a view.
344pub fn frame(tokens: &Tokens) -> container::Style {
345    container::Style {
346        border: Border {
347            color: tokens.border,
348            width: 1.0,
349            radius: radius::LG.into(),
350        },
351        ..container::Style::default()
352    }
353}
354
355/// A timed entry in the month, which is text beside a dot: no fill at
356/// rest, the accent under the pointer and the paint when selected.
357pub fn entry_line(
358    tokens: &Tokens,
359    with: impl Into<Paint>,
360    selected: bool,
361    status: Status,
362) -> button::Style {
363    if selected {
364        return chip_button(tokens, with, true, status);
365    }
366    let background = match status {
367        Status::Hovered | Status::Pressed => Some(tokens.accent),
368        Status::Active | Status::Disabled => None,
369    };
370    button::Style {
371        background: background.map(Background::Color),
372        text_color: tokens.foreground,
373        border: Border {
374            radius: radius::SM.into(),
375            ..Border::default()
376        },
377        shadow: Shadow::default(),
378        snap: true,
379    }
380}
381
382/// A timed entry in the month with its look: [`entry_line`] at rest,
383/// with the text muted when struck, faded or a preview, a faint wash
384/// behind a hatched one, and the [`marked`] fill when selected.
385pub fn marked_line(
386    tokens: &Tokens,
387    with: impl Into<Paint>,
388    look: Look,
389    status: Status,
390) -> button::Style {
391    if look.selected {
392        return marked_button(tokens, with, look, status);
393    }
394    let mut style = entry_line(tokens, with, false, status);
395    if look.preview {
396        let colours = preview(tokens);
397        style.background = Some(Background::Color(colours.background));
398        style.border = Border {
399            color: colours.border,
400            width: 1.0,
401            radius: radius::SM.into(),
402        };
403    }
404    if look.preview || matches!(look.emphasis, Emphasis::Struck | Emphasis::Faded) {
405        style.text_color = tokens.muted_foreground;
406    }
407    style
408}
409
410/// A line between days, weeks or hours.
411pub fn line(tokens: &Tokens) -> Color {
412    tokens.border
413}
414
415/// The line showing the current time.
416pub fn now_line(tokens: &Tokens) -> Color {
417    tokens.destructive
418}
419
420/// The outline of an entry being dragged out, moved or resized.
421#[derive(Debug, Clone, Copy, PartialEq)]
422pub struct Preview {
423    pub background: Color,
424    pub border: Color,
425}
426
427pub fn preview(tokens: &Tokens) -> Preview {
428    Preview {
429        background: fade(tokens.primary, 0.16),
430        border: tokens.primary,
431    }
432}
433
434#[cfg(test)]
435mod tests {
436    use super::*;
437    use crate::theme::{dark, light};
438
439    #[test]
440    fn every_tone_has_its_own_colour() {
441        for theme in [light(), dark()] {
442            let tokens = Tokens::of(&theme);
443            let colours: Vec<_> = Tone::ALL.iter().map(|t| tone(&tokens, *t)).collect();
444            for (index, colour) in colours.iter().enumerate() {
445                assert!(!colours[..index].contains(colour), "{:?}", Tone::ALL[index]);
446            }
447        }
448    }
449
450    #[test]
451    fn a_chip_is_a_wash_that_deepens_under_the_pointer() {
452        for theme in [light(), dark()] {
453            let tokens = Tokens::of(&theme);
454            for entry_tone in Tone::ALL {
455                let rest = chip(&tokens, entry_tone, false, Status::Active);
456                let hovered = chip(&tokens, entry_tone, false, Status::Hovered);
457                let pressed = chip(&tokens, entry_tone, false, Status::Pressed);
458                assert_eq!(rest.stripe, tone(&tokens, entry_tone));
459                assert_eq!(rest.foreground, tokens.foreground);
460                assert_ne!(rest.background, tokens.background);
461                assert_ne!(rest.background, hovered.background);
462                assert_ne!(hovered.background, pressed.background);
463                assert_ne!(rest.background, rest.stripe);
464            }
465        }
466    }
467
468    #[test]
469    fn a_selected_chip_is_filled_and_keeps_its_text_readable() {
470        for theme in [light(), dark()] {
471            let tokens = Tokens::of(&theme);
472            for entry_tone in Tone::ALL {
473                let colour = tone(&tokens, entry_tone);
474                for status in [Status::Active, Status::Hovered, Status::Pressed] {
475                    let selected = chip(&tokens, entry_tone, true, status);
476                    assert_eq!(selected.background, colour);
477                    assert_eq!(selected.foreground, on(colour));
478                }
479            }
480        }
481    }
482
483    #[test]
484    fn a_read_only_calendar_keeps_its_entries_at_full_colour() {
485        for theme in [light(), dark()] {
486            let tokens = Tokens::of(&theme);
487            for entry_tone in Tone::ALL {
488                assert_eq!(
489                    chip(&tokens, entry_tone, false, Status::Disabled),
490                    chip(&tokens, entry_tone, false, Status::Active)
491                );
492                assert_eq!(
493                    entry_line(&tokens, entry_tone, false, Status::Disabled),
494                    entry_line(&tokens, entry_tone, false, Status::Active)
495                );
496            }
497        }
498    }
499
500    #[test]
501    fn a_timed_entry_in_the_month_is_plain_until_hovered_or_selected() {
502        for theme in [light(), dark()] {
503            let tokens = Tokens::of(&theme);
504            for entry_tone in Tone::ALL {
505                let rest = entry_line(&tokens, entry_tone, false, Status::Active);
506                assert_eq!(rest.background, None);
507                assert_eq!(rest.text_color, tokens.foreground);
508
509                for status in [Status::Hovered, Status::Pressed] {
510                    let hovered = entry_line(&tokens, entry_tone, false, status);
511                    assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
512                }
513
514                let selected = entry_line(&tokens, entry_tone, true, Status::Active);
515                assert_eq!(
516                    selected,
517                    chip_button(&tokens, entry_tone, true, Status::Active)
518                );
519            }
520        }
521    }
522
523    #[test]
524    fn the_chip_button_uses_the_chip_colours() {
525        let tokens = Tokens::of(&dark());
526        let colours = chip(&tokens, Tone::Warning, false, Status::Hovered);
527        let style = chip_button(&tokens, Tone::Warning, false, Status::Hovered);
528        assert_eq!(
529            style.background,
530            Some(Background::Color(colours.background))
531        );
532        assert_eq!(style.text_color, colours.foreground);
533        assert_eq!(style.border.width, 0.0);
534    }
535
536    #[test]
537    fn today_is_a_filled_pill_and_other_months_sit_back() {
538        for theme in [light(), dark()] {
539            let tokens = Tokens::of(&theme);
540            let today = day_number(
541                &tokens,
542                Day {
543                    today: true,
544                    in_month: true,
545                    selected: false,
546                },
547            );
548            assert_eq!(today.background, Some(tokens.primary));
549            assert_eq!(today.foreground, tokens.primary_foreground);
550
551            let inside = day_number(
552                &tokens,
553                Day {
554                    in_month: true,
555                    ..Day::default()
556                },
557            );
558            let outside = day_number(&tokens, Day::default());
559            assert_eq!(inside.background, None);
560            assert_eq!(inside.foreground, tokens.foreground);
561            assert_eq!(outside.foreground, tokens.muted_foreground);
562        }
563    }
564
565    #[test]
566    fn cells_show_selection_hover_and_other_months() {
567        for theme in [light(), dark()] {
568            let tokens = Tokens::of(&theme);
569            let inside = Day {
570                in_month: true,
571                ..Day::default()
572            };
573            let rest = cell(&tokens, inside, Status::Active);
574            assert_eq!(rest.background, None);
575            assert_eq!(rest.border.color, Color::TRANSPARENT);
576            assert_eq!(cell(&tokens, inside, Status::Disabled), rest);
577
578            let hovered = cell(&tokens, inside, Status::Hovered);
579            assert!(hovered.background.is_some());
580            assert_eq!(cell(&tokens, inside, Status::Pressed), hovered);
581
582            let selected = cell(
583                &tokens,
584                Day {
585                    selected: true,
586                    ..inside
587                },
588                Status::Hovered,
589            );
590            assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
591            assert_eq!(selected.border.color, tokens.ring);
592            assert_ne!(selected.background, hovered.background);
593
594            let outside = cell(&tokens, Day::default(), Status::Active);
595            assert!(outside.background.is_some());
596            assert_ne!(outside.background, selected.background);
597            assert_eq!(outside.text_color, tokens.muted_foreground);
598        }
599    }
600
601    #[test]
602    fn days_of_the_year_mark_today_selection_and_hover() {
603        for theme in [light(), dark()] {
604            let tokens = Tokens::of(&theme);
605            let plain = Day {
606                in_month: true,
607                ..Day::default()
608            };
609            let rest = year_day(&tokens, plain, Status::Active);
610            assert_eq!(rest.background, None);
611            assert_eq!(rest.text_color, tokens.foreground);
612            assert_eq!(year_day(&tokens, plain, Status::Disabled), rest);
613
614            let hovered = year_day(&tokens, plain, Status::Hovered);
615            assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
616            assert_eq!(year_day(&tokens, plain, Status::Pressed), hovered);
617
618            let today = year_day(
619                &tokens,
620                Day {
621                    today: true,
622                    ..plain
623                },
624                Status::Hovered,
625            );
626            assert_eq!(today.background, Some(Background::Color(tokens.primary)));
627            assert_eq!(today.text_color, tokens.primary_foreground);
628
629            let selected = year_day(
630                &tokens,
631                Day {
632                    selected: true,
633                    ..plain
634                },
635                Status::Active,
636            );
637            assert_eq!(selected.border.color, tokens.ring);
638            assert_eq!(rest.border.color, Color::TRANSPARENT);
639        }
640    }
641
642    #[test]
643    fn rows_show_selection_and_hover() {
644        for theme in [light(), dark()] {
645            let tokens = Tokens::of(&theme);
646            assert_eq!(row(&tokens, false, false).background, None);
647            let hovered = row(&tokens, false, true);
648            let selected = row(&tokens, true, false);
649            assert!(hovered.background.is_some());
650            assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
651            assert_ne!(hovered.background, selected.background);
652            assert_eq!(row(&tokens, true, true), selected);
653        }
654    }
655
656    #[test]
657    fn a_custom_theme_reaches_every_part() {
658        use crate::theme::Config;
659        use iced::color;
660
661        let theme = Config::light()
662            .primary(color!(0x6d28d9))
663            .accent(color!(0xede9fe))
664            .border(color!(0xddd6fe))
665            .success(color!(0x0d9488))
666            .warning(color!(0xea580c))
667            .destructive(color!(0xe11d48))
668            .foreground(color!(0x1e1b2e))
669            .build();
670        let tokens = Tokens::of(&theme);
671
672        assert_eq!(tone(&tokens, Tone::Primary), color!(0x6d28d9));
673        assert_eq!(tone(&tokens, Tone::Success), color!(0x0d9488));
674        assert_eq!(tone(&tokens, Tone::Warning), color!(0xea580c));
675        assert_eq!(tone(&tokens, Tone::Destructive), color!(0xe11d48));
676        assert_eq!(
677            chip(&tokens, Tone::Primary, true, Status::Active).background,
678            color!(0x6d28d9)
679        );
680        assert_eq!(
681            chip(&tokens, Tone::Success, false, Status::Active).foreground,
682            color!(0x1e1b2e)
683        );
684
685        let today = Day {
686            today: true,
687            in_month: true,
688            selected: false,
689        };
690        assert_eq!(
691            day_number(&tokens, today).background,
692            Some(color!(0x6d28d9))
693        );
694        assert_eq!(
695            year_day(&tokens, today, Status::Active).background,
696            Some(Background::Color(color!(0x6d28d9)))
697        );
698
699        let selected = Day {
700            selected: true,
701            in_month: true,
702            today: false,
703        };
704        assert_eq!(
705            cell(&tokens, selected, Status::Active).background,
706            Some(Background::Color(color!(0xede9fe)))
707        );
708        assert_eq!(
709            row(&tokens, true, false).background,
710            Some(Background::Color(color!(0xede9fe)))
711        );
712        assert_eq!(
713            entry_line(&tokens, Tone::Primary, false, Status::Hovered).background,
714            Some(Background::Color(color!(0xede9fe)))
715        );
716
717        assert_eq!(line(&tokens), color!(0xddd6fe));
718        assert_eq!(frame(&tokens).border.color, color!(0xddd6fe));
719        assert_eq!(now_line(&tokens), color!(0xe11d48));
720        assert_eq!(preview(&tokens).border, color!(0x6d28d9));
721    }
722
723    #[test]
724    fn lines_frames_and_previews_come_from_the_tokens() {
725        for theme in [light(), dark()] {
726            let tokens = Tokens::of(&theme);
727            assert_eq!(line(&tokens), tokens.border);
728            assert_eq!(now_line(&tokens), tokens.destructive);
729            assert_eq!(frame(&tokens).border.color, tokens.border);
730            let preview = preview(&tokens);
731            assert_eq!(preview.border, tokens.primary);
732            assert!(preview.background.a < 0.5);
733        }
734    }
735
736    fn look(emphasis: Emphasis) -> Look {
737        Look {
738            emphasis,
739            ..Look::default()
740        }
741    }
742
743    #[test]
744    fn a_solid_entry_looks_as_a_plain_chip() {
745        for theme in [light(), dark()] {
746            let tokens = Tokens::of(&theme);
747            for status in [
748                Status::Active,
749                Status::Hovered,
750                Status::Pressed,
751                Status::Disabled,
752            ] {
753                for selected in [false, true] {
754                    let marked = marked(
755                        &tokens,
756                        Tone::Success,
757                        Look {
758                            selected,
759                            ..Look::default()
760                        },
761                        status,
762                    );
763                    assert_eq!(marked.chip, chip(&tokens, Tone::Success, selected, status));
764                    assert_eq!(marked.border, Color::TRANSPARENT);
765                    assert_eq!((marked.hatch, marked.strike), (None, None));
766                }
767            }
768        }
769    }
770
771    #[test]
772    fn each_emphasis_marks_the_entry_its_own_way() {
773        for theme in [light(), dark()] {
774            let tokens = Tokens::of(&theme);
775            let colour = tone(&tokens, Tone::Warning);
776            let at = |emphasis| marked(&tokens, Tone::Warning, look(emphasis), Status::Active);
777
778            let outlined = at(Emphasis::Outlined);
779            assert_eq!(outlined.border, colour);
780            assert_eq!(outlined.chip.background, tokens.background);
781
782            let struck = at(Emphasis::Struck);
783            assert_eq!(struck.strike, Some(tokens.muted_foreground));
784            assert_eq!(struck.chip.foreground, tokens.muted_foreground);
785
786            let faded = at(Emphasis::Faded);
787            let solid = at(Emphasis::Solid);
788            assert_eq!(faded.chip.foreground, tokens.muted_foreground);
789            assert_ne!(faded.chip.background, solid.chip.background);
790
791            let hatched = at(Emphasis::Hatched);
792            assert_eq!(
793                hatched.hatch.map(|hatch| hatch.a),
794                Some(colour.a * HATCH_ALPHA)
795            );
796            assert_eq!(hatched.chip, solid.chip);
797        }
798    }
799
800    #[test]
801    fn a_selected_entry_is_filled_and_keeps_its_strike() {
802        let tokens = Tokens::of(&light());
803        for emphasis in Emphasis::ALL {
804            let selected = Look {
805                selected: true,
806                ..look(emphasis)
807            };
808            let marked = marked(&tokens, Tone::Primary, selected, Status::Active);
809            assert_eq!(
810                marked.chip,
811                chip(&tokens, Tone::Primary, true, Status::Active)
812            );
813            assert_eq!(marked.hatch, None, "{emphasis:?}");
814            assert_eq!(marked.strike.is_some(), emphasis == Emphasis::Struck);
815        }
816    }
817
818    #[test]
819    fn a_preview_is_a_faint_outline_whatever_its_emphasis() {
820        for theme in [light(), dark()] {
821            let tokens = Tokens::of(&theme);
822            let colours = preview(&tokens);
823            for emphasis in Emphasis::ALL {
824                let pending = Look {
825                    preview: true,
826                    ..look(emphasis)
827                };
828                let marked = marked(&tokens, Tone::Success, pending, Status::Hovered);
829                assert_eq!(marked.border, colours.border);
830                assert_eq!(marked.chip.background, colours.background);
831                assert_eq!(marked.chip.foreground, tokens.muted_foreground);
832                assert!(!pending.hatched());
833            }
834        }
835    }
836
837    /// Colours an app might give its calendars, from pale to deep.
838    fn calendar_colours() -> [Color; 8] {
839        use iced::color;
840        [
841            color!(0x7986cb),
842            color!(0x33b679),
843            color!(0x8e24aa),
844            color!(0xf6bf26),
845            color!(0x039be5),
846            color!(0xffffff),
847            color!(0x000000),
848            color!(0x616161),
849        ]
850    }
851
852    #[test]
853    fn a_tone_paints_as_it_always_has() {
854        for theme in [light(), dark()] {
855            let tokens = Tokens::of(&theme);
856            for entry_tone in Tone::ALL {
857                let painted = Paint::Tone(entry_tone);
858                assert_eq!(paint(&tokens, painted), tone(&tokens, entry_tone));
859                for emphasis in Emphasis::ALL {
860                    for status in [Status::Active, Status::Hovered, Status::Pressed] {
861                        assert_eq!(
862                            marked(&tokens, painted, look(emphasis), status),
863                            marked(&tokens, entry_tone, look(emphasis), status)
864                        );
865                    }
866                }
867            }
868        }
869    }
870
871    #[test]
872    fn a_colour_of_its_own_is_drawn_opaque_instead_of_the_tone() {
873        let tokens = Tokens::of(&light());
874        let teal = Color::from_rgba8(0x0f, 0x76, 0x6e, 0.3);
875        let opaque = Color { a: 1.0, ..teal };
876        assert_eq!(paint(&tokens, teal), opaque);
877        let rest = chip(&tokens, teal, false, Status::Active);
878        assert_eq!(rest.stripe, opaque);
879        assert_eq!(rest.background, mix(tokens.background, opaque, 0.16));
880        assert_eq!(chip(&tokens, teal, true, Status::Active).background, opaque);
881    }
882
883    #[test]
884    fn text_on_a_colour_of_its_own_stays_readable() {
885        for theme in [light(), dark()] {
886            let tokens = Tokens::of(&theme);
887            for colour in calendar_colours() {
888                for emphasis in Emphasis::ALL {
889                    for selected in [false, true] {
890                        for status in [Status::Active, Status::Hovered, Status::Pressed] {
891                            let marked = marked(
892                                &tokens,
893                                colour,
894                                Look {
895                                    selected,
896                                    ..look(emphasis)
897                                },
898                                status,
899                            );
900                            let ratio = crate::theme::contrast(
901                                marked.chip.foreground,
902                                marked.chip.background,
903                            );
904                            assert!(
905                                ratio >= crate::theme::TEXT_CONTRAST,
906                                "{colour:?} {emphasis:?} selected {selected} {status:?}: {ratio}"
907                            );
908                        }
909                    }
910                }
911            }
912        }
913    }
914
915    #[test]
916    fn marks_in_a_colour_of_its_own_stay_visible_on_the_page() {
917        for theme in [light(), dark()] {
918            let tokens = Tokens::of(&theme);
919            for colour in calendar_colours() {
920                let drawn = paint(&tokens, colour);
921                let ratio = contrast(drawn, tokens.background);
922                assert!(ratio >= MARK_CONTRAST - 0.01, "{colour:?}: {ratio}");
923                let outlined = marked(&tokens, colour, look(Emphasis::Outlined), Status::Active);
924                assert_eq!(outlined.border, drawn);
925                assert_eq!(outlined.chip.stripe, drawn);
926                // A colour that already stands out is drawn as given.
927                if contrast(colour, tokens.background) >= MARK_CONTRAST {
928                    assert_eq!(drawn, colour);
929                }
930            }
931        }
932        let white = Color::WHITE;
933        assert_ne!(paint(&Tokens::of(&light()), white), white);
934        assert_eq!(paint(&Tokens::of(&dark()), white), white);
935    }
936
937    #[test]
938    fn a_colour_of_its_own_takes_every_emphasis() {
939        for theme in [light(), dark()] {
940            let tokens = Tokens::of(&theme);
941            let teal = Color::from_rgb8(0x0f, 0x76, 0x6e);
942            let at = |emphasis, status| marked(&tokens, teal, look(emphasis), status);
943
944            let solid = at(Emphasis::Solid, Status::Active);
945            let hovered = at(Emphasis::Solid, Status::Hovered);
946            assert_eq!(solid.chip.stripe, teal);
947            assert_ne!(solid.chip.background, hovered.chip.background);
948
949            let outlined = at(Emphasis::Outlined, Status::Active);
950            assert_eq!(outlined.border, teal);
951            assert_eq!(outlined.chip.background, tokens.background);
952            assert_eq!(
953                at(Emphasis::Outlined, Status::Hovered).chip.background,
954                mix(tokens.background, teal, 0.12)
955            );
956
957            let faded = at(Emphasis::Faded, Status::Active);
958            assert_eq!(faded.chip.stripe, mix(tokens.background, teal, 0.5));
959            assert_eq!(
960                faded.chip.background,
961                mix(tokens.background, solid.chip.background, 0.5)
962            );
963
964            let hatched = at(Emphasis::Hatched, Status::Active);
965            assert_eq!(hatched.hatch, Some(fade(teal, HATCH_ALPHA)));
966
967            let struck = at(Emphasis::Struck, Status::Active);
968            assert_eq!(struck.strike, Some(struck.chip.foreground));
969
970            let selected = marked_button(
971                &tokens,
972                teal,
973                Look {
974                    selected: true,
975                    ..Look::default()
976                },
977                Status::Active,
978            );
979            assert_eq!(selected.background, Some(Background::Color(teal)));
980            assert_eq!(selected.text_color, on(teal));
981        }
982    }
983
984    #[test]
985    fn button_and_line_styles_carry_the_look() {
986        let tokens = Tokens::of(&dark());
987        let outlined = marked_button(
988            &tokens,
989            Tone::Primary,
990            look(Emphasis::Outlined),
991            Status::Active,
992        );
993        assert_eq!(outlined.border.width, 1.0);
994        let solid = marked_button(
995            &tokens,
996            Tone::Primary,
997            look(Emphasis::Solid),
998            Status::Active,
999        );
1000        assert_eq!(solid.border.width, 0.0);
1001        let struck = marked_line(
1002            &tokens,
1003            Tone::Primary,
1004            look(Emphasis::Struck),
1005            Status::Active,
1006        );
1007        assert_eq!(struck.text_color, tokens.muted_foreground);
1008        let plain = marked_line(
1009            &tokens,
1010            Tone::Primary,
1011            look(Emphasis::Solid),
1012            Status::Active,
1013        );
1014        assert_eq!(
1015            plain,
1016            entry_line(&tokens, Tone::Primary, false, Status::Active)
1017        );
1018    }
1019}