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
use crate::render::Color;
/// The colors widgets draw with. Return one from `App::theme`, or `provide`
/// one to restyle a subtree; the default is a light look.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Theme {
/// The window.
pub background: Color,
/// Dialogs, sheets, menus, input fields and thumbs.
pub surface: Color,
/// Text on `background` and `surface`.
pub ink: Color,
/// Secondary text.
pub muted: Color,
/// Buttons, active controls and focus.
pub accent: Color,
/// Text on `accent`.
pub on_accent: Color,
/// The chosen row of a menu.
pub selected: Color,
/// Behind sliders, progress bars and segments.
pub track: Color,
/// Input borders, unchecked toggles and radios.
pub outline: Color,
/// The line under table headers and tab bars.
pub divider: Color,
/// Snackbars and tooltips, which contrast with the window.
pub inverse: Color,
/// Text on `inverse`.
pub on_inverse: Color,
}
impl Default for Theme {
fn default() -> Self {
Theme {
background: Color::WHITE,
surface: Color::WHITE,
ink: Color::hex(0x1d2030),
muted: Color::hex(0x6b7080),
accent: Color::hex(0x3b6fe0),
on_accent: Color::WHITE,
selected: Color::hex(0xe8eefc),
track: Color::hex(0xd5d8e0),
outline: Color::hex(0x9a9fb0),
divider: Color::hex(0xdfe1e8),
inverse: Color::hex(0x323232),
on_inverse: Color::WHITE,
}
}
}
/// A [`Theme`] color by name, resolved where the widget is painted.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Role {
/// [`Theme::background`].
Background,
/// [`Theme::surface`].
Surface,
/// [`Theme::ink`].
Ink,
/// [`Theme::muted`].
Muted,
/// [`Theme::accent`].
Accent,
/// [`Theme::on_accent`].
OnAccent,
/// [`Theme::selected`].
Selected,
/// [`Theme::track`].
Track,
/// [`Theme::outline`].
Outline,
/// [`Theme::divider`].
Divider,
/// [`Theme::inverse`].
Inverse,
/// [`Theme::on_inverse`].
OnInverse,
}
impl Theme {
/// The color this theme gives `role`.
pub fn color(&self, role: Role) -> Color {
match role {
Role::Background => self.background,
Role::Surface => self.surface,
Role::Ink => self.ink,
Role::Muted => self.muted,
Role::Accent => self.accent,
Role::OnAccent => self.on_accent,
Role::Selected => self.selected,
Role::Track => self.track,
Role::Outline => self.outline,
Role::Divider => self.divider,
Role::Inverse => self.inverse,
Role::OnInverse => self.on_inverse,
}
}
}
/// What a widget paints with: a fixed color, a theme role, or whatever
/// contrasts with the button or surface around it.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Ink {
/// This color, whatever the theme.
Fixed(Color),
/// The theme's color for this role.
Role(Role),
#[default]
/// The content color of the nearest `button` or `container` with `content`;
/// [`Role::Ink`] outside them.
Content,
}
impl From<Color> for Ink {
fn from(c: Color) -> Self {
Ink::Fixed(c)
}
}
impl From<Role> for Ink {
fn from(r: Role) -> Self {
Ink::Role(r)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn contrast(a: Color, b: Color) -> f32 {
let lin = |v: f32| match v <= 0.04045 {
true => v / 12.92,
false => ((v + 0.055) / 1.055).powf(2.4),
};
let l = |c: Color| 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b);
let (a, b) = (l(a), l(b));
(a.max(b) + 0.05) / (a.min(b) + 0.05)
}
#[test]
fn default_text_roles_reach_wcag_aa() {
let t = Theme::default();
for (name, fg, bg) in [
("ink/background", t.ink, t.background),
("ink/surface", t.ink, t.surface),
("muted/surface", t.muted, t.surface),
("on_accent/accent", t.on_accent, t.accent),
("on_inverse/inverse", t.on_inverse, t.inverse),
] {
assert!(contrast(fg, bg) >= 4.5, "{name}: {}", contrast(fg, bg));
}
}
}