super-voice-app-core/src/visualizer/palette/tests.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | use super::{ |
3 | | Appearance, Gradient, MIN_CONTRAST, MURKY_HUES, Palette, Rgba, STOPS, contrast, gradient, |
4 | | linear_to_oklab, monochrome, rgba_to_linear, swatch, |
5 | | }; |
6 | | use crate::visualizer::Feature::{self, Stt, Tts}; |
7 | | |
8 | | const APPEARANCES: [Appearance; 2] = [Appearance::Light, Appearance::Dark]; |
9 | | |
10 | | /// macOS blue, the system's default accent. |
11 | 261 | fn blue() -> Rgba { |
12 | 261 | Rgba::rgb8(0x00, 0x7a, 0xff) |
13 | 261 | } |
14 | | |
15 | | /// macOS graphite, the grey accent. |
16 | 5 | fn grey() -> Rgba { |
17 | 5 | Rgba::rgb8(0x8e, 0x8e, 0x93) |
18 | 5 | } |
19 | | |
20 | 0 | fn name(appearance: Appearance) -> &'static str { |
21 | 0 | match appearance { |
22 | 0 | Appearance::Light => "light", |
23 | 0 | Appearance::Dark => "dark", |
24 | | } |
25 | 0 | } |
26 | | |
27 | 3 | fn every_gradient() -> Vec<(Palette, Feature, Appearance, Gradient)> { |
28 | 3 | let mut all = Vec::new(); |
29 | 3 | for palette in Palette::all() { |
30 | 39 | for feature in [Stt, Tts] { |
31 | 78 | for appearance in APPEARANCES { |
32 | 156 | all.push(( |
33 | 156 | palette, |
34 | 156 | feature, |
35 | 156 | appearance, |
36 | 156 | gradient(palette, feature, appearance, blue()), |
37 | 156 | )); |
38 | | } |
39 | | } |
40 | | } |
41 | 3 | all |
42 | 3 | } |
43 | | |
44 | | /// The distance between two colors in Oklab: about 0.02 is the least anyone |
45 | | /// notices. |
46 | 374 | fn distance(a: Rgba, b: Rgba) -> f64 { |
47 | 374 | let (a, b) = ( |
48 | 374 | linear_to_oklab(rgba_to_linear(a)), |
49 | 374 | linear_to_oklab(rgba_to_linear(b)), |
50 | 374 | ); |
51 | 374 | a.iter() |
52 | 374 | .zip(b) |
53 | 1.12k | .map(|(x, y)| (x - y).powi(2)) |
54 | | .sum::<f64>() |
55 | | .sqrt() |
56 | 374 | } |
57 | | |
58 | | /// A gradient's average color, in Oklab. |
59 | 52 | fn mean(gradient: &Gradient) -> [f64; 3] { |
60 | 52 | let mut sum = [0.0; 3]; |
61 | 52 | for stop in gradient.stops { |
62 | 364 | let lab = linear_to_oklab(rgba_to_linear(stop)); |
63 | 364 | for (s, v) in sum.iter_mut().zip(lab) { |
64 | 1.09k | *s += v; |
65 | | } |
66 | | } |
67 | 156 | sum52 .map(|s| s / f64::from(u8::try_from(STOPS).unwrap())) |
68 | 52 | } |
69 | | |
70 | 420 | fn hue_and_chroma(color: Rgba) -> (f64, f64) { |
71 | 420 | let [_, a, b] = linear_to_oklab(rgba_to_linear(color)); |
72 | 420 | (b.atan2(a).to_degrees().rem_euclid(360.0), a.hypot(b)) |
73 | 420 | } |
74 | | |
75 | | #[test] |
76 | 1 | fn the_menu_offers_every_palette_in_declaration_order() { |
77 | 1 | assert_eq!(Palette::all()[0], Palette::Accent, "the accent leads"); |
78 | 1 | for (i, palette) in Palette::all().iter().enumerate() { |
79 | 13 | assert_eq!(*palette as usize, i, "{palette:?}"); |
80 | | } |
81 | 1 | } |
82 | | |
83 | | #[test] |
84 | 1 | fn the_menu_bar_samples_are_the_measured_ones() { |
85 | | // Linear values of #ECECEC and #242424, to f32 precision. |
86 | 1 | for (appearance, expected) in [ |
87 | 1 | (Appearance::Light, 0.838_799), |
88 | 1 | (Appearance::Dark, 0.017_642), |
89 | 1 | ] { |
90 | 2 | for v in appearance.background() { |
91 | 6 | assert!((v - expected).abs() < 1e-5, "{appearance:?}: {v}"); |
92 | | } |
93 | | } |
94 | 1 | } |
95 | | |
96 | | #[test] |
97 | 1 | fn every_color_keeps_3_to_1() { |
98 | 1 | for accent in [blue(), grey()] { |
99 | 2 | for palette in Palette::all() { |
100 | 26 | for feature in [Stt, Tts] { |
101 | 52 | for appearance in APPEARANCES { |
102 | 104 | let background = appearance.background(); |
103 | 104 | for stop in gradient(palette, feature, appearance, accent).stops { |
104 | 728 | let ratio = contrast(rgba_to_linear(stop), background); |
105 | 728 | assert!( |
106 | 728 | ratio >= MIN_CONTRAST - 0.01, |
107 | | "{palette:?} {feature:?} on a {} bar, accent {accent:?}: {stop:?} at {ratio:.2}:1", |
108 | | name(appearance) |
109 | | ); |
110 | | } |
111 | | } |
112 | | } |
113 | | } |
114 | | } |
115 | 1 | } |
116 | | |
117 | | /// The point of a gradient: it moves, and it doesn't jump. |
118 | | #[test] |
119 | 1 | fn every_gradient_moves_smoothly() { |
120 | 1 | for (palette, feature, appearance, gradient) in every_gradient() { |
121 | 52 | let ends = distance(gradient.stops[0], gradient.stops[STOPS - 1]); |
122 | 52 | assert!( |
123 | 52 | ends > 0.04, |
124 | | "{palette:?} {feature:?} {appearance:?}: flat ({ends:.3})" |
125 | | ); |
126 | 52 | for pair in gradient.stops.windows(2) { |
127 | 312 | let step = distance(pair[0], pair[1]); |
128 | 312 | assert!( |
129 | 312 | step < 0.12, |
130 | | "{palette:?} {feature:?} {appearance:?}: jumps {step:.3}" |
131 | | ); |
132 | | } |
133 | | } |
134 | 1 | } |
135 | | |
136 | | /// No color sinks into olive or a dark yellow-green, the least liked colors: |
137 | | /// a deep shade of a yellow-green hue is what makes one. (The accent's |
138 | | /// companion keeps a wider berth, [`MURKY_HUES`], to land clearly away from |
139 | | /// them; a deep emerald past it is a clean green.) |
140 | | #[test] |
141 | 1 | fn no_color_turns_olive() { |
142 | | const OLIVE: std::ops::Range<f64> = 95.0..135.0; |
143 | 1 | for (palette, feature, appearance, gradient) in every_gradient() { |
144 | 52 | for stop in gradient.stops { |
145 | 364 | let l = linear_to_oklab(rgba_to_linear(stop))[0]; |
146 | 364 | let (hue, chroma) = hue_and_chroma(stop); |
147 | 364 | assert!( |
148 | 364 | !(l < 0.7 && chroma > 0.05251 && OLIVE.contains(&hue)219 ), |
149 | | "{palette:?} {feature:?} {appearance:?}: {stop:?} at {hue:.0} degrees, lightness {l:.2}" |
150 | | ); |
151 | | } |
152 | | } |
153 | 1 | } |
154 | | |
155 | | /// The features stay apart on either menu bar, enough to tell which is in |
156 | | /// front from its color alone. |
157 | | #[test] |
158 | 1 | fn the_two_features_are_told_apart() { |
159 | 1 | for palette in Palette::all() { |
160 | 13 | for appearance in APPEARANCES { |
161 | 52 | let [stt, tts] = [Stt, Tts]26 .map(|f| mean(&gradient(palette, f, appearance, blue()))); |
162 | 26 | let apart = stt |
163 | 26 | .iter() |
164 | 26 | .zip(tts) |
165 | 78 | .map(|(a, b)| (a - b).powi(2)) |
166 | | .sum::<f64>() |
167 | | .sqrt(); |
168 | 26 | assert!( |
169 | 26 | apart > 0.10, |
170 | | "{palette:?} on a {} bar: {apart:.3}", |
171 | | name(appearance) |
172 | | ); |
173 | | } |
174 | | } |
175 | 1 | } |
176 | | |
177 | | /// Each gradient runs from its deep end to its light one, which the bars draw |
178 | | /// from the bottom up. |
179 | | #[test] |
180 | 1 | fn every_gradient_starts_deep() { |
181 | 1 | for (palette, feature, appearance, gradient) in every_gradient() { |
182 | 52 | let [first, last] = [gradient.stops[0], gradient.stops[STOPS - 1]] |
183 | 104 | .map(|c| linear_to_oklab(rgba_to_linear(c))[0]); |
184 | 52 | let ok = match appearance { |
185 | 26 | Appearance::Dark => first < last, |
186 | 26 | Appearance::Light => first <= last + 0.02, |
187 | | }; |
188 | 52 | assert!( |
189 | 52 | ok, |
190 | | "{palette:?} {feature:?} {appearance:?}: {first:.2} to {last:.2}" |
191 | | ); |
192 | | } |
193 | 1 | } |
194 | | |
195 | | #[test] |
196 | 1 | fn the_accent_palette_follows_the_accent() { |
197 | 1 | for accent in [Rgba::rgb(0.9, 0.2, 0.2), Rgba::rgb(0.2, 0.4, 0.9), blue()] { |
198 | | // Speech to text's bars reach the accent at their tips, wherever the |
199 | | // accent already clears 3:1 against the bar. |
200 | 3 | let g = gradient(Palette::Accent, Stt, Appearance::Dark, accent); |
201 | 3 | let tips = g.stops[STOPS - 1]; |
202 | 3 | assert!(distance(tips, accent) < 0.005, "{accent:?}: {tips:?}"); |
203 | | } |
204 | | // Every other palette ignores it. |
205 | 1 | let white = Rgba::rgb(1.0, 1.0, 1.0); |
206 | 1 | let black = Rgba::rgb(0.0, 0.0, 0.0); |
207 | 1 | assert_eq!( |
208 | 1 | gradient(Palette::Sunset, Stt, Appearance::Dark, white), |
209 | 1 | gradient(Palette::Sunset, Stt, Appearance::Dark, black) |
210 | | ); |
211 | 1 | } |
212 | | |
213 | | /// The menu shows a pair in its own colors on either menu bar: the dark |
214 | | /// one's, which the light one's only deepen. |
215 | | #[test] |
216 | 1 | fn the_designed_pairs_have_their_own_light_menu_bar_shades() { |
217 | 1 | for palette in Palette::all() { |
218 | 13 | if palette == Palette::Accent { |
219 | 1 | continue; |
220 | 12 | } |
221 | 12 | for feature in [Stt, Tts] { |
222 | 24 | assert_ne!( |
223 | 24 | gradient(palette, feature, Appearance::Dark, blue()), |
224 | 24 | gradient(palette, feature, Appearance::Light, blue()), |
225 | | "{palette:?} {feature:?} looks the same on either menu bar" |
226 | | ); |
227 | | } |
228 | | } |
229 | 1 | } |
230 | | |
231 | | /// Whatever the accent, pale like a sand, deep, or one of the system's own, |
232 | | /// the pair shows it as the system has it wherever it already clears 3:1, |
233 | | /// and its companion has a color of its own, never a murky yellow-green. |
234 | | #[test] |
235 | 1 | fn the_accent_pair_shows_the_accent_as_it_is() { |
236 | 1 | let accents = [ |
237 | 1 | ("orange", Rgba::rgb8(0xe0, 0x8a, 0x20)), |
238 | 1 | ("purple", Rgba::rgb8(0xa2, 0x8a, 0xff)), |
239 | 1 | ("blue", blue()), |
240 | 1 | ("sand", Rgba::rgb8(0xee, 0xd4, 0x9f)), |
241 | 1 | ]; |
242 | 1 | for ((name_, accent), appearance) in accents |
243 | 1 | .into_iter() |
244 | 8 | .flat_map(|a| APPEARANCES4 .map(|p| (a, p))) |
245 | | { |
246 | 8 | let clears = contrast(rgba_to_linear(accent), appearance.background()) >= MIN_CONTRAST; |
247 | 8 | let stt = gradient(Palette::Accent, Stt, appearance, accent); |
248 | 8 | let shown = stt.stops[STOPS - 1]; |
249 | 8 | if clears { |
250 | 5 | assert!(distance(shown, accent) < 0.005, "{name_}: shows {shown:?}"); |
251 | 3 | } |
252 | | |
253 | 8 | let tts = gradient(Palette::Accent, Tts, appearance, accent); |
254 | 8 | for stop in tts.stops { |
255 | 56 | let (hue, chroma) = hue_and_chroma(stop); |
256 | 56 | assert!(chroma > 0.05, "{name_}: a gray companion, {chroma:.3}"); |
257 | 56 | assert!( |
258 | 56 | !MURKY_HUES.contains(&hue), |
259 | | "{name_}: a murky companion at {hue:.0} degrees" |
260 | | ); |
261 | | } |
262 | | } |
263 | 1 | } |
264 | | |
265 | | #[test] |
266 | 1 | fn a_grey_accent_is_deepened_only_as_far_as_the_light_bar_needs() { |
267 | 1 | let light = gradient(Palette::Accent, Stt, Appearance::Light, grey()); |
268 | 1 | let tips = light.stops[STOPS - 1]; |
269 | 1 | assert!(distance(tips, grey()) > 0.005, "grey is under 3:1 on light"); |
270 | 1 | let dark = gradient(Palette::Accent, Stt, Appearance::Dark, grey()); |
271 | 1 | assert!(distance(dark.stops[STOPS - 1], grey()) < 0.005); |
272 | 1 | } |
273 | | |
274 | | #[test] |
275 | 1 | fn settings_files_name_a_palette_by_its_variant() { |
276 | 1 | assert_eq!( |
277 | 1 | serde_json::to_string(&Palette::EmberAndAsh).unwrap(), |
278 | 1 | "\"EmberAndAsh\"" |
279 | | ); |
280 | 1 | assert_eq!( |
281 | 1 | serde_json::from_str::<Palette>("\"Rose\"").unwrap(), |
282 | 1 | Palette::Rose |
283 | | ); |
284 | 1 | } |
285 | | |
286 | | #[test] |
287 | 1 | fn accent_is_the_default() { |
288 | 1 | assert_eq!(Palette::default(), Palette::Accent); |
289 | 1 | } |
290 | | |
291 | | #[test] |
292 | 1 | fn monochrome_is_black_on_light_and_white_on_dark() { |
293 | 1 | let light = monochrome(Appearance::Light); |
294 | 7 | assert!1 (light.stops.iter()1 .all(|c| *c |
295 | 7 | == Rgba { |
296 | 7 | r: 0.0, |
297 | 7 | g: 0.0, |
298 | 7 | b: 0.0, |
299 | 7 | a: 0.85 |
300 | 7 | })); |
301 | 1 | let dark = monochrome(Appearance::Dark); |
302 | 7 | assert!1 (dark.stops.iter()1 .all(|c| *c == Rgba::rgb(1.0, 1.0, 1.0))); |
303 | 1 | } |
304 | | |
305 | | #[test] |
306 | 1 | fn a_swatch_is_the_two_ends() { |
307 | 1 | let g = gradient(Palette::Aurora, Tts, Appearance::Dark, blue()); |
308 | 1 | assert_eq!( |
309 | 1 | swatch(Palette::Aurora, Tts, Appearance::Dark, blue()), |
310 | 1 | [g.stops[0], g.stops[STOPS - 1]] |
311 | | ); |
312 | 1 | } |