iced-cube 0.0.21

Themeable application components for the iced GUI library, with Lucide icons
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//! Colours of the calendar's parts, resolved from the theme's tokens.

use iced::widget::button::Status;
use iced::widget::{button, container};
use iced::{Background, Border, Color, Shadow};

use super::entry::{Emphasis, Paint, Tone};
use crate::theme::{Tokens, contrast, fade, mix, on, radius, readable};

/// The colour a [`Tone`] stands for.
pub fn tone(tokens: &Tokens, tone: Tone) -> Color {
    tone.colour(tokens)
}

/// The contrast a colour of the app's own keeps against the background, so
/// its stripe, outline and dots never vanish into the page. It is low on
/// purpose: a pale calendar colour keeps its hue, and only one too close
/// to the page to see, such as white on white, is moved.
pub const MARK_CONTRAST: f32 = 1.5;

/// The colour a [`Paint`] stands for: its tone's, or its own made opaque
/// and, where it is too close to the background to see, moved just far
/// enough towards the foreground to reach [`MARK_CONTRAST`].
pub fn paint(tokens: &Tokens, paint: impl Into<Paint>) -> Color {
    match paint.into() {
        Paint::Tone(entry_tone) => tone(tokens, entry_tone),
        Paint::Colour(colour) => visible(Color { a: 1.0, ..colour }, tokens.background),
    }
}

/// `colour`, moved towards near-black or near-white, whichever stands out
/// from `background`, until it reaches [`MARK_CONTRAST`].
fn visible(colour: Color, background: Color) -> Color {
    let towards = on(background);
    (0..=20)
        .map(|step| mix(colour, towards, step as f32 / 20.0))
        .find(|candidate| contrast(*candidate, background) >= MARK_CONTRAST)
        .unwrap_or(towards)
}

/// `text` for drawing on `background` in an entry painted `with`. The
/// theme's tones already read on their washes, so only a colour of the
/// app's own moves the text towards whatever reads.
fn legible(with: Paint, text: Color, background: Color) -> Color {
    match with {
        Paint::Tone(_) => text,
        Paint::Colour(_) => readable(text, background),
    }
}

/// The colours of an entry, as a bar in the month or a block in the day.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Chip {
    pub background: Color,
    pub foreground: Color,
    /// The stripe down its leading edge.
    pub stripe: Color,
}

/// An entry is a wash of its paint with a solid stripe, deeper under the
/// pointer. The selected entry is filled with the paint. A disabled entry
/// looks like one at rest.
pub fn chip(tokens: &Tokens, with: impl Into<Paint>, selected: bool, status: Status) -> Chip {
    let with = with.into();
    let colour = paint(tokens, with);
    if selected {
        return Chip {
            background: colour,
            foreground: on(colour),
            stripe: colour,
        };
    }
    let wash = match status {
        Status::Active | Status::Disabled => 0.16,
        Status::Hovered => 0.26,
        Status::Pressed => 0.34,
    };
    // A calendar without a handler is read-only, not unavailable, so its
    // entries keep their full colour.
    let background = mix(tokens.background, colour, wash);
    Chip {
        background,
        foreground: legible(with, tokens.foreground, background),
        stripe: colour,
    }
}

/// How an entry is to be drawn, besides its tone.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Look {
    pub selected: bool,
    pub emphasis: Emphasis,
    /// Drawn as the preview of a change waiting to be confirmed.
    pub preview: bool,
}

impl Look {
    /// Whether stripes are drawn over the fill. A selected entry or a
    /// preview has none, so its text stays readable.
    pub fn hatched(self) -> bool {
        self.emphasis == Emphasis::Hatched && !self.selected && !self.preview
    }

    /// Whether a line is drawn through the title.
    pub fn struck(self) -> bool {
        self.emphasis == Emphasis::Struck
    }
}

/// How strong the stripes of a hatched entry are.
pub const HATCH_ALPHA: f32 = 0.35;

/// The colours of an entry with its [`Emphasis`], as a bar or a block.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Marked {
    pub chip: Chip,
    /// A border round the whole entry, transparent when it has none.
    pub border: Color,
    /// The colour of diagonal stripes over the fill.
    pub hatch: Option<Color>,
    /// The colour of a line through the title.
    pub strike: Option<Color>,
}

/// An entry's colours for its look. Solid is [`chip`]. Outlined has a
/// border in its paint over the page, Struck and Faded take muted text,
/// Hatched lays stripes of its paint over the wash, and a preview is a
/// faint outline of the primary colour. The selected entry is filled
/// with its paint whatever its emphasis, keeping any strike.
pub fn marked(tokens: &Tokens, with: impl Into<Paint>, look: Look, status: Status) -> Marked {
    let with = with.into();
    let colour = paint(tokens, with);
    let solid = chip(tokens, with, look.selected, status);
    let mut marked = Marked {
        chip: solid,
        border: Color::TRANSPARENT,
        hatch: None,
        strike: None,
    };
    if look.preview {
        let colours = preview(tokens);
        marked.chip = Chip {
            background: colours.background,
            foreground: tokens.muted_foreground,
            stripe: colours.border,
        };
        marked.border = colours.border;
        if look.struck() {
            marked.strike = Some(tokens.muted_foreground);
        }
        return marked;
    }
    if look.selected {
        if look.struck() {
            marked.strike = Some(solid.foreground);
        }
        return marked;
    }
    match look.emphasis {
        Emphasis::Solid => {}
        Emphasis::Outlined => {
            let hovered = matches!(status, Status::Hovered | Status::Pressed);
            marked.chip.background = if hovered {
                mix(tokens.background, colour, 0.12)
            } else {
                tokens.background
            };
            marked.chip.foreground = legible(with, tokens.foreground, marked.chip.background);
            marked.border = colour;
        }
        Emphasis::Struck => {
            let muted = legible(with, tokens.muted_foreground, marked.chip.background);
            marked.chip.foreground = muted;
            marked.strike = Some(muted);
        }
        Emphasis::Faded => {
            marked.chip.background = mix(tokens.background, solid.background, 0.5);
            marked.chip.foreground = legible(with, tokens.muted_foreground, marked.chip.background);
            marked.chip.stripe = mix(tokens.background, colour, 0.5);
        }
        Emphasis::Hatched => marked.hatch = Some(fade(colour, HATCH_ALPHA)),
    }
    marked
}

/// The button style of an entry drawn with [`marked`] colours.
pub fn marked_button(
    tokens: &Tokens,
    with: impl Into<Paint>,
    look: Look,
    status: Status,
) -> button::Style {
    let marked = marked(tokens, with, look, status);
    button::Style {
        background: Some(Background::Color(marked.chip.background)),
        text_color: marked.chip.foreground,
        border: Border {
            color: marked.border,
            width: if marked.border == Color::TRANSPARENT {
                0.0
            } else {
                1.0
            },
            radius: radius::SM.into(),
        },
        shadow: Shadow::default(),
        snap: true,
    }
}

/// The button style of an entry drawn with [`chip`] colours.
pub fn chip_button(
    tokens: &Tokens,
    with: impl Into<Paint>,
    selected: bool,
    status: Status,
) -> button::Style {
    let chip = chip(tokens, with, selected, status);
    button::Style {
        background: Some(Background::Color(chip.background)),
        text_color: chip.foreground,
        border: Border {
            radius: radius::SM.into(),
            ..Border::default()
        },
        shadow: Shadow::default(),
        snap: true,
    }
}

/// What a day in the month grid is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Day {
    pub today: bool,
    pub selected: bool,
    /// Whether the day belongs to the month on show.
    pub in_month: bool,
}

/// The colours of a day's number: a filled pill for today.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DayNumber {
    pub background: Option<Color>,
    pub foreground: Color,
}

pub fn day_number(tokens: &Tokens, day: Day) -> DayNumber {
    if day.today {
        return DayNumber {
            background: Some(tokens.primary),
            foreground: tokens.primary_foreground,
        };
    }
    DayNumber {
        background: None,
        foreground: if day.in_month {
            tokens.foreground
        } else {
            tokens.muted_foreground
        },
    }
}

/// The surface of a day cell, which is a button. The selected day takes
/// the accent with a ring, a day under the pointer a lighter accent, and
/// days outside the month sit back.
pub fn cell(tokens: &Tokens, day: Day, status: Status) -> button::Style {
    let hovered = matches!(status, Status::Hovered | Status::Pressed);
    let background = if day.selected {
        Some(tokens.accent)
    } else if hovered {
        Some(mix(tokens.background, tokens.accent, 0.6))
    } else if !day.in_month {
        Some(mix(tokens.background, tokens.muted, 0.4))
    } else {
        None
    };
    button::Style {
        background: background.map(Background::Color),
        text_color: day_number(tokens, day).foreground,
        border: Border {
            color: if day.selected {
                tokens.ring
            } else {
                Color::TRANSPARENT
            },
            width: 1.0,
            radius: 0.0.into(),
        },
        shadow: Shadow::default(),
        snap: true,
    }
}

/// A day in the year view: a circle filled for today, ringed when
/// selected and tinted under the pointer.
pub fn year_day(tokens: &Tokens, day: Day, status: Status) -> button::Style {
    let hovered = matches!(status, Status::Hovered | Status::Pressed);
    let number = day_number(tokens, day);
    let background = if day.today {
        number.background
    } else if day.selected || hovered {
        Some(tokens.accent)
    } else {
        None
    };
    button::Style {
        background: background.map(Background::Color),
        text_color: number.foreground,
        border: Border {
            color: if day.selected {
                tokens.ring
            } else {
                Color::TRANSPARENT
            },
            width: 1.0,
            radius: radius::FULL.into(),
        },
        shadow: Shadow::default(),
        snap: true,
    }
}

/// A row in the agenda, or a day in the strip above a narrow week.
pub fn row(tokens: &Tokens, selected: bool, hovered: bool) -> container::Style {
    let background = if selected {
        Some(tokens.accent)
    } else if hovered {
        Some(mix(tokens.background, tokens.accent, 0.6))
    } else {
        None
    };
    container::Style {
        background: background.map(Background::Color),
        border: Border {
            radius: radius::MD.into(),
            ..Border::default()
        },
        ..container::Style::default()
    }
}

/// The frame around a view.
pub fn frame(tokens: &Tokens) -> container::Style {
    container::Style {
        border: Border {
            color: tokens.border,
            width: 1.0,
            radius: radius::LG.into(),
        },
        ..container::Style::default()
    }
}

/// A timed entry in the month, which is text beside a dot: no fill at
/// rest, the accent under the pointer and the paint when selected.
pub fn entry_line(
    tokens: &Tokens,
    with: impl Into<Paint>,
    selected: bool,
    status: Status,
) -> button::Style {
    if selected {
        return chip_button(tokens, with, true, status);
    }
    let background = match status {
        Status::Hovered | Status::Pressed => Some(tokens.accent),
        Status::Active | Status::Disabled => None,
    };
    button::Style {
        background: background.map(Background::Color),
        text_color: tokens.foreground,
        border: Border {
            radius: radius::SM.into(),
            ..Border::default()
        },
        shadow: Shadow::default(),
        snap: true,
    }
}

/// A timed entry in the month with its look: [`entry_line`] at rest,
/// with the text muted when struck, faded or a preview, a faint wash
/// behind a hatched one, and the [`marked`] fill when selected.
pub fn marked_line(
    tokens: &Tokens,
    with: impl Into<Paint>,
    look: Look,
    status: Status,
) -> button::Style {
    if look.selected {
        return marked_button(tokens, with, look, status);
    }
    let mut style = entry_line(tokens, with, false, status);
    if look.preview {
        let colours = preview(tokens);
        style.background = Some(Background::Color(colours.background));
        style.border = Border {
            color: colours.border,
            width: 1.0,
            radius: radius::SM.into(),
        };
    }
    if look.preview || matches!(look.emphasis, Emphasis::Struck | Emphasis::Faded) {
        style.text_color = tokens.muted_foreground;
    }
    style
}

/// A line between days, weeks or hours.
pub fn line(tokens: &Tokens) -> Color {
    tokens.border
}

/// The line showing the current time.
pub fn now_line(tokens: &Tokens) -> Color {
    tokens.destructive
}

/// The outline of an entry being dragged out, moved or resized.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Preview {
    pub background: Color,
    pub border: Color,
}

pub fn preview(tokens: &Tokens) -> Preview {
    Preview {
        background: fade(tokens.primary, 0.16),
        border: tokens.primary,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::theme::{dark, light};

    #[test]
    fn every_tone_has_its_own_colour() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let colours: Vec<_> = Tone::ALL.iter().map(|t| tone(&tokens, *t)).collect();
            for (index, colour) in colours.iter().enumerate() {
                assert!(!colours[..index].contains(colour), "{:?}", Tone::ALL[index]);
            }
        }
    }

    #[test]
    fn a_chip_is_a_wash_that_deepens_under_the_pointer() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for entry_tone in Tone::ALL {
                let rest = chip(&tokens, entry_tone, false, Status::Active);
                let hovered = chip(&tokens, entry_tone, false, Status::Hovered);
                let pressed = chip(&tokens, entry_tone, false, Status::Pressed);
                assert_eq!(rest.stripe, tone(&tokens, entry_tone));
                assert_eq!(rest.foreground, tokens.foreground);
                assert_ne!(rest.background, tokens.background);
                assert_ne!(rest.background, hovered.background);
                assert_ne!(hovered.background, pressed.background);
                assert_ne!(rest.background, rest.stripe);
            }
        }
    }

    #[test]
    fn a_selected_chip_is_filled_and_keeps_its_text_readable() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for entry_tone in Tone::ALL {
                let colour = tone(&tokens, entry_tone);
                for status in [Status::Active, Status::Hovered, Status::Pressed] {
                    let selected = chip(&tokens, entry_tone, true, status);
                    assert_eq!(selected.background, colour);
                    assert_eq!(selected.foreground, on(colour));
                }
            }
        }
    }

    #[test]
    fn a_read_only_calendar_keeps_its_entries_at_full_colour() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for entry_tone in Tone::ALL {
                assert_eq!(
                    chip(&tokens, entry_tone, false, Status::Disabled),
                    chip(&tokens, entry_tone, false, Status::Active)
                );
                assert_eq!(
                    entry_line(&tokens, entry_tone, false, Status::Disabled),
                    entry_line(&tokens, entry_tone, false, Status::Active)
                );
            }
        }
    }

    #[test]
    fn a_timed_entry_in_the_month_is_plain_until_hovered_or_selected() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for entry_tone in Tone::ALL {
                let rest = entry_line(&tokens, entry_tone, false, Status::Active);
                assert_eq!(rest.background, None);
                assert_eq!(rest.text_color, tokens.foreground);

                for status in [Status::Hovered, Status::Pressed] {
                    let hovered = entry_line(&tokens, entry_tone, false, status);
                    assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
                }

                let selected = entry_line(&tokens, entry_tone, true, Status::Active);
                assert_eq!(
                    selected,
                    chip_button(&tokens, entry_tone, true, Status::Active)
                );
            }
        }
    }

    #[test]
    fn the_chip_button_uses_the_chip_colours() {
        let tokens = Tokens::of(&dark());
        let colours = chip(&tokens, Tone::Warning, false, Status::Hovered);
        let style = chip_button(&tokens, Tone::Warning, false, Status::Hovered);
        assert_eq!(
            style.background,
            Some(Background::Color(colours.background))
        );
        assert_eq!(style.text_color, colours.foreground);
        assert_eq!(style.border.width, 0.0);
    }

    #[test]
    fn today_is_a_filled_pill_and_other_months_sit_back() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let today = day_number(
                &tokens,
                Day {
                    today: true,
                    in_month: true,
                    selected: false,
                },
            );
            assert_eq!(today.background, Some(tokens.primary));
            assert_eq!(today.foreground, tokens.primary_foreground);

            let inside = day_number(
                &tokens,
                Day {
                    in_month: true,
                    ..Day::default()
                },
            );
            let outside = day_number(&tokens, Day::default());
            assert_eq!(inside.background, None);
            assert_eq!(inside.foreground, tokens.foreground);
            assert_eq!(outside.foreground, tokens.muted_foreground);
        }
    }

    #[test]
    fn cells_show_selection_hover_and_other_months() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let inside = Day {
                in_month: true,
                ..Day::default()
            };
            let rest = cell(&tokens, inside, Status::Active);
            assert_eq!(rest.background, None);
            assert_eq!(rest.border.color, Color::TRANSPARENT);
            assert_eq!(cell(&tokens, inside, Status::Disabled), rest);

            let hovered = cell(&tokens, inside, Status::Hovered);
            assert!(hovered.background.is_some());
            assert_eq!(cell(&tokens, inside, Status::Pressed), hovered);

            let selected = cell(
                &tokens,
                Day {
                    selected: true,
                    ..inside
                },
                Status::Hovered,
            );
            assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
            assert_eq!(selected.border.color, tokens.ring);
            assert_ne!(selected.background, hovered.background);

            let outside = cell(&tokens, Day::default(), Status::Active);
            assert!(outside.background.is_some());
            assert_ne!(outside.background, selected.background);
            assert_eq!(outside.text_color, tokens.muted_foreground);
        }
    }

    #[test]
    fn days_of_the_year_mark_today_selection_and_hover() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let plain = Day {
                in_month: true,
                ..Day::default()
            };
            let rest = year_day(&tokens, plain, Status::Active);
            assert_eq!(rest.background, None);
            assert_eq!(rest.text_color, tokens.foreground);
            assert_eq!(year_day(&tokens, plain, Status::Disabled), rest);

            let hovered = year_day(&tokens, plain, Status::Hovered);
            assert_eq!(hovered.background, Some(Background::Color(tokens.accent)));
            assert_eq!(year_day(&tokens, plain, Status::Pressed), hovered);

            let today = year_day(
                &tokens,
                Day {
                    today: true,
                    ..plain
                },
                Status::Hovered,
            );
            assert_eq!(today.background, Some(Background::Color(tokens.primary)));
            assert_eq!(today.text_color, tokens.primary_foreground);

            let selected = year_day(
                &tokens,
                Day {
                    selected: true,
                    ..plain
                },
                Status::Active,
            );
            assert_eq!(selected.border.color, tokens.ring);
            assert_eq!(rest.border.color, Color::TRANSPARENT);
        }
    }

    #[test]
    fn rows_show_selection_and_hover() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            assert_eq!(row(&tokens, false, false).background, None);
            let hovered = row(&tokens, false, true);
            let selected = row(&tokens, true, false);
            assert!(hovered.background.is_some());
            assert_eq!(selected.background, Some(Background::Color(tokens.accent)));
            assert_ne!(hovered.background, selected.background);
            assert_eq!(row(&tokens, true, true), selected);
        }
    }

    #[test]
    fn a_custom_theme_reaches_every_part() {
        use crate::theme::Config;
        use iced::color;

        let theme = Config::light()
            .primary(color!(0x6d28d9))
            .accent(color!(0xede9fe))
            .border(color!(0xddd6fe))
            .success(color!(0x0d9488))
            .warning(color!(0xea580c))
            .destructive(color!(0xe11d48))
            .foreground(color!(0x1e1b2e))
            .build();
        let tokens = Tokens::of(&theme);

        assert_eq!(tone(&tokens, Tone::Primary), color!(0x6d28d9));
        assert_eq!(tone(&tokens, Tone::Success), color!(0x0d9488));
        assert_eq!(tone(&tokens, Tone::Warning), color!(0xea580c));
        assert_eq!(tone(&tokens, Tone::Destructive), color!(0xe11d48));
        assert_eq!(
            chip(&tokens, Tone::Primary, true, Status::Active).background,
            color!(0x6d28d9)
        );
        assert_eq!(
            chip(&tokens, Tone::Success, false, Status::Active).foreground,
            color!(0x1e1b2e)
        );

        let today = Day {
            today: true,
            in_month: true,
            selected: false,
        };
        assert_eq!(
            day_number(&tokens, today).background,
            Some(color!(0x6d28d9))
        );
        assert_eq!(
            year_day(&tokens, today, Status::Active).background,
            Some(Background::Color(color!(0x6d28d9)))
        );

        let selected = Day {
            selected: true,
            in_month: true,
            today: false,
        };
        assert_eq!(
            cell(&tokens, selected, Status::Active).background,
            Some(Background::Color(color!(0xede9fe)))
        );
        assert_eq!(
            row(&tokens, true, false).background,
            Some(Background::Color(color!(0xede9fe)))
        );
        assert_eq!(
            entry_line(&tokens, Tone::Primary, false, Status::Hovered).background,
            Some(Background::Color(color!(0xede9fe)))
        );

        assert_eq!(line(&tokens), color!(0xddd6fe));
        assert_eq!(frame(&tokens).border.color, color!(0xddd6fe));
        assert_eq!(now_line(&tokens), color!(0xe11d48));
        assert_eq!(preview(&tokens).border, color!(0x6d28d9));
    }

    #[test]
    fn lines_frames_and_previews_come_from_the_tokens() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            assert_eq!(line(&tokens), tokens.border);
            assert_eq!(now_line(&tokens), tokens.destructive);
            assert_eq!(frame(&tokens).border.color, tokens.border);
            let preview = preview(&tokens);
            assert_eq!(preview.border, tokens.primary);
            assert!(preview.background.a < 0.5);
        }
    }

    fn look(emphasis: Emphasis) -> Look {
        Look {
            emphasis,
            ..Look::default()
        }
    }

    #[test]
    fn a_solid_entry_looks_as_a_plain_chip() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for status in [
                Status::Active,
                Status::Hovered,
                Status::Pressed,
                Status::Disabled,
            ] {
                for selected in [false, true] {
                    let marked = marked(
                        &tokens,
                        Tone::Success,
                        Look {
                            selected,
                            ..Look::default()
                        },
                        status,
                    );
                    assert_eq!(marked.chip, chip(&tokens, Tone::Success, selected, status));
                    assert_eq!(marked.border, Color::TRANSPARENT);
                    assert_eq!((marked.hatch, marked.strike), (None, None));
                }
            }
        }
    }

    #[test]
    fn each_emphasis_marks_the_entry_its_own_way() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let colour = tone(&tokens, Tone::Warning);
            let at = |emphasis| marked(&tokens, Tone::Warning, look(emphasis), Status::Active);

            let outlined = at(Emphasis::Outlined);
            assert_eq!(outlined.border, colour);
            assert_eq!(outlined.chip.background, tokens.background);

            let struck = at(Emphasis::Struck);
            assert_eq!(struck.strike, Some(tokens.muted_foreground));
            assert_eq!(struck.chip.foreground, tokens.muted_foreground);

            let faded = at(Emphasis::Faded);
            let solid = at(Emphasis::Solid);
            assert_eq!(faded.chip.foreground, tokens.muted_foreground);
            assert_ne!(faded.chip.background, solid.chip.background);

            let hatched = at(Emphasis::Hatched);
            assert_eq!(
                hatched.hatch.map(|hatch| hatch.a),
                Some(colour.a * HATCH_ALPHA)
            );
            assert_eq!(hatched.chip, solid.chip);
        }
    }

    #[test]
    fn a_selected_entry_is_filled_and_keeps_its_strike() {
        let tokens = Tokens::of(&light());
        for emphasis in Emphasis::ALL {
            let selected = Look {
                selected: true,
                ..look(emphasis)
            };
            let marked = marked(&tokens, Tone::Primary, selected, Status::Active);
            assert_eq!(
                marked.chip,
                chip(&tokens, Tone::Primary, true, Status::Active)
            );
            assert_eq!(marked.hatch, None, "{emphasis:?}");
            assert_eq!(marked.strike.is_some(), emphasis == Emphasis::Struck);
        }
    }

    #[test]
    fn a_preview_is_a_faint_outline_whatever_its_emphasis() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let colours = preview(&tokens);
            for emphasis in Emphasis::ALL {
                let pending = Look {
                    preview: true,
                    ..look(emphasis)
                };
                let marked = marked(&tokens, Tone::Success, pending, Status::Hovered);
                assert_eq!(marked.border, colours.border);
                assert_eq!(marked.chip.background, colours.background);
                assert_eq!(marked.chip.foreground, tokens.muted_foreground);
                assert!(!pending.hatched());
            }
        }
    }

    /// Colours an app might give its calendars, from pale to deep.
    fn calendar_colours() -> [Color; 8] {
        use iced::color;
        [
            color!(0x7986cb),
            color!(0x33b679),
            color!(0x8e24aa),
            color!(0xf6bf26),
            color!(0x039be5),
            color!(0xffffff),
            color!(0x000000),
            color!(0x616161),
        ]
    }

    #[test]
    fn a_tone_paints_as_it_always_has() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for entry_tone in Tone::ALL {
                let painted = Paint::Tone(entry_tone);
                assert_eq!(paint(&tokens, painted), tone(&tokens, entry_tone));
                for emphasis in Emphasis::ALL {
                    for status in [Status::Active, Status::Hovered, Status::Pressed] {
                        assert_eq!(
                            marked(&tokens, painted, look(emphasis), status),
                            marked(&tokens, entry_tone, look(emphasis), status)
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn a_colour_of_its_own_is_drawn_opaque_instead_of_the_tone() {
        let tokens = Tokens::of(&light());
        let teal = Color::from_rgba8(0x0f, 0x76, 0x6e, 0.3);
        let opaque = Color { a: 1.0, ..teal };
        assert_eq!(paint(&tokens, teal), opaque);
        let rest = chip(&tokens, teal, false, Status::Active);
        assert_eq!(rest.stripe, opaque);
        assert_eq!(rest.background, mix(tokens.background, opaque, 0.16));
        assert_eq!(chip(&tokens, teal, true, Status::Active).background, opaque);
    }

    #[test]
    fn text_on_a_colour_of_its_own_stays_readable() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for colour in calendar_colours() {
                for emphasis in Emphasis::ALL {
                    for selected in [false, true] {
                        for status in [Status::Active, Status::Hovered, Status::Pressed] {
                            let marked = marked(
                                &tokens,
                                colour,
                                Look {
                                    selected,
                                    ..look(emphasis)
                                },
                                status,
                            );
                            let ratio = crate::theme::contrast(
                                marked.chip.foreground,
                                marked.chip.background,
                            );
                            assert!(
                                ratio >= crate::theme::TEXT_CONTRAST,
                                "{colour:?} {emphasis:?} selected {selected} {status:?}: {ratio}"
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn marks_in_a_colour_of_its_own_stay_visible_on_the_page() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            for colour in calendar_colours() {
                let drawn = paint(&tokens, colour);
                let ratio = contrast(drawn, tokens.background);
                assert!(ratio >= MARK_CONTRAST - 0.01, "{colour:?}: {ratio}");
                let outlined = marked(&tokens, colour, look(Emphasis::Outlined), Status::Active);
                assert_eq!(outlined.border, drawn);
                assert_eq!(outlined.chip.stripe, drawn);
                // A colour that already stands out is drawn as given.
                if contrast(colour, tokens.background) >= MARK_CONTRAST {
                    assert_eq!(drawn, colour);
                }
            }
        }
        let white = Color::WHITE;
        assert_ne!(paint(&Tokens::of(&light()), white), white);
        assert_eq!(paint(&Tokens::of(&dark()), white), white);
    }

    #[test]
    fn a_colour_of_its_own_takes_every_emphasis() {
        for theme in [light(), dark()] {
            let tokens = Tokens::of(&theme);
            let teal = Color::from_rgb8(0x0f, 0x76, 0x6e);
            let at = |emphasis, status| marked(&tokens, teal, look(emphasis), status);

            let solid = at(Emphasis::Solid, Status::Active);
            let hovered = at(Emphasis::Solid, Status::Hovered);
            assert_eq!(solid.chip.stripe, teal);
            assert_ne!(solid.chip.background, hovered.chip.background);

            let outlined = at(Emphasis::Outlined, Status::Active);
            assert_eq!(outlined.border, teal);
            assert_eq!(outlined.chip.background, tokens.background);
            assert_eq!(
                at(Emphasis::Outlined, Status::Hovered).chip.background,
                mix(tokens.background, teal, 0.12)
            );

            let faded = at(Emphasis::Faded, Status::Active);
            assert_eq!(faded.chip.stripe, mix(tokens.background, teal, 0.5));
            assert_eq!(
                faded.chip.background,
                mix(tokens.background, solid.chip.background, 0.5)
            );

            let hatched = at(Emphasis::Hatched, Status::Active);
            assert_eq!(hatched.hatch, Some(fade(teal, HATCH_ALPHA)));

            let struck = at(Emphasis::Struck, Status::Active);
            assert_eq!(struck.strike, Some(struck.chip.foreground));

            let selected = marked_button(
                &tokens,
                teal,
                Look {
                    selected: true,
                    ..Look::default()
                },
                Status::Active,
            );
            assert_eq!(selected.background, Some(Background::Color(teal)));
            assert_eq!(selected.text_color, on(teal));
        }
    }

    #[test]
    fn button_and_line_styles_carry_the_look() {
        let tokens = Tokens::of(&dark());
        let outlined = marked_button(
            &tokens,
            Tone::Primary,
            look(Emphasis::Outlined),
            Status::Active,
        );
        assert_eq!(outlined.border.width, 1.0);
        let solid = marked_button(
            &tokens,
            Tone::Primary,
            look(Emphasis::Solid),
            Status::Active,
        );
        assert_eq!(solid.border.width, 0.0);
        let struck = marked_line(
            &tokens,
            Tone::Primary,
            look(Emphasis::Struck),
            Status::Active,
        );
        assert_eq!(struck.text_color, tokens.muted_foreground);
        let plain = marked_line(
            &tokens,
            Tone::Primary,
            look(Emphasis::Solid),
            Status::Active,
        );
        assert_eq!(
            plain,
            entry_line(&tokens, Tone::Primary, false, Status::Active)
        );
    }
}