crabshell-ui 0.5.0

Widgets, styles, layout, text and text editing, focus, hit testing and invalidation for Crabshell
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
//! Colors, fonts and styles.

use crate::{
  Border, Color, Font, Gradient, GradientShape, Mask, Padding, Radii, Rect, Shadow, Style,
};

#[test]
fn hex_parses_both_forms_in_either_case() {
  assert_eq!(Color::hex("#1e1E2e"), Color::rgb(0x1e, 0x1e, 0x2e));
  assert_eq!(Color::hex("#CDD6F480"), Color::rgba(0xcd, 0xd6, 0xf4, 0x80));
}

#[test]
fn hex_works_in_a_const() {
  const THEME: Color = Color::hex("#45475a");
  assert_eq!(THEME, Color::rgb(0x45, 0x47, 0x5a));
}

#[test]
#[should_panic(expected = "Color::hex takes")]
fn hex_rejects_a_missing_hash() {
  Color::hex("1e1e2e");
}

#[test]
#[should_panic(expected = "isn't a hex digit")]
fn hex_rejects_other_characters() {
  Color::hex("#1e1e2g");
}

#[test]
fn builders_set_their_fields_and_nothing_else() {
  const WS: Style = Style::new().padding(2, 8).radius(4.0);
  assert_eq!(
    WS,
    Style {
      padding: Some(Padding {
        top: 2,
        right: 8,
        bottom: 2,
        left: 8
      }),
      radius: Some(Radii::all(4.0)),
      ..Style::new()
    }
  );
  let bordered = Style::new().border(1.5, Color::rgb(1, 2, 3)).opacity(0.5);
  assert_eq!(
    bordered.border,
    Some(Border {
      width: 1.5,
      color: Color::rgb(1, 2, 3),
    })
  );
  assert_eq!(bordered.opacity, Some(0.5));
  assert_eq!(bordered.background, None);
}

#[test]
fn each_side_sets_its_own_padding_and_keeps_the_others() {
  const PILL: Style = Style::new()
    .padding(2, 8)
    .padding_left(12)
    .padding_bottom(5);
  assert_eq!(
    PILL.padding,
    Some(Padding {
      top: 2,
      right: 8,
      bottom: 5,
      left: 12
    })
  );
  // On a style with no padding, the other sides are 0.
  const TOP: Style = Style::new().padding_top(3);
  const RIGHT: Style = Style::new().padding_right(4);
  let sides = |style: Style| style.padding.map(|p| (p.top, p.right, p.bottom, p.left));
  assert_eq!(sides(TOP), Some((3, 0, 0, 0)));
  assert_eq!(sides(RIGHT), Some((0, 4, 0, 0)));
  // A later `padding` sets every side again.
  assert_eq!(sides(PILL.padding(1, 1)), Some((1, 1, 1, 1)));
  assert!(!PILL.same_layout(&PILL.padding_left(13)));
}

#[test]
fn merge_takes_the_fields_the_top_style_sets() {
  let base = Style::new()
    .background(Color::rgb(1, 1, 1))
    .color(Color::rgb(2, 2, 2))
    .padding(2, 8);
  let hover = Style::new().background(Color::rgb(9, 9, 9));
  let merged = base.merge(hover);
  assert_eq!(merged.background, Some(Color::rgb(9, 9, 9)));
  assert_eq!(merged.color, Some(Color::rgb(2, 2, 2)));
  assert_eq!(merged.padding, base.padding);
  assert_eq!(Style::new().merge(base), base);
}

#[test]
fn only_font_and_padding_affect_layout() {
  let base = Style::new().padding(2, 8).font(Font::new("Inter", 13.0));
  assert!(
    base.same_layout(
      &base
        .background(Color::rgb(1, 2, 3))
        .radius(9.0)
        .opacity(0.1)
    )
  );
  assert!(!base.same_layout(&base.padding(3, 8)));
  assert!(!base.same_layout(&base.font(Font::new("Inter", 14.0))));
  assert!(!base.same_layout(&base.font(Font::new("Inter", 13.0).weight(700))));
}

#[test]
fn font_defaults_to_regular_weight() {
  const BOLD: Font = Font::new("Inter", 13.0).weight(700);
  assert_eq!(Font::new("Inter", 13.0).weight, 400);
  assert_eq!(BOLD.weight, 700);
  assert_eq!(BOLD.family, "Inter");
}

#[test]
fn a_style_blends_its_paint_fields_and_takes_the_rest_from_the_end() {
  let from = Style::new()
    .background(Color::rgb(0, 0, 0))
    .radius(0.0)
    .font(Font::new("a", 10.0));
  let to = Style::new()
    .border(4.0, Color::rgb(200, 0, 0))
    .opacity(0.0)
    .font(Font::new("b", 12.0));
  let half = from.lerp(&to, 0.5);
  // A background only one side sets fades to its transparent self.
  assert_eq!(half.background, Some(Color::rgba(0, 0, 0, 128)));
  let border = half.border.expect("a border on the way");
  assert_eq!(
    (border.width, border.color),
    (2.0, Color::rgba(200, 0, 0, 128))
  );
  assert_eq!(half.radius, Some(Radii::all(0.0)));
  assert_eq!(half.opacity, Some(0.5), "unset opacity is 1");
  assert_eq!(
    half.font,
    Some(Font::new("b", 12.0)),
    "the rest is the end's"
  );
  assert_eq!(
    from.lerp(&to, 1.0).background,
    Some(Color::rgba(0, 0, 0, 0))
  );
}

#[test]
fn a_gradient_keeps_its_ends_and_the_stops_between_in_order() {
  const RED: Color = Color::rgb(255, 0, 0);
  const BLUE: Color = Color::rgb(0, 0, 255);
  const SKY: Style = Style::new().gradient(
    Gradient::linear(90.0, RED, BLUE)
      .stop(0.25, Color::rgb(0, 255, 0))
      .stop(0.75, Color::rgb(9, 9, 9)),
  );
  let gradient = SKY.gradient.expect("set by the builder");
  assert_eq!(gradient.shape, GradientShape::Linear(90.0));
  assert_eq!(
    gradient.stops(),
    [
      (0.0, RED),
      (0.25, Color::rgb(0, 255, 0)),
      (0.75, Color::rgb(9, 9, 9)),
      (1.0, BLUE)
    ]
  );
  assert_eq!(
    Gradient::radial(RED, BLUE).stops(),
    [(0.0, RED), (1.0, BLUE)]
  );
  assert_eq!(Style::new().merge(SKY).gradient, Some(gradient));
  assert!(Style::new().same_layout(&SKY), "a gradient is paint only");
}

#[test]
#[should_panic(expected = "at most four colors")]
fn a_fifth_gradient_color_is_refused() {
  let black = Color::rgb(0, 0, 0);
  let _ = Gradient::radial(black, black)
    .stop(0.2, black)
    .stop(0.4, black)
    .stop(0.6, black);
}

#[test]
fn gradients_blend_when_they_have_the_same_shape_and_colors() {
  let black = Color::rgb(0, 0, 0);
  let white = Color::rgb(200, 200, 200);
  let from = Style::new().gradient(Gradient::linear(0.0, black, black));
  let to = Style::new().gradient(Gradient::linear(90.0, white, black));
  let half = from.lerp(&to, 0.5).gradient.expect("both set one");
  assert_eq!(half.shape, GradientShape::Linear(45.0));
  assert_eq!(half.stops()[0], (0.0, Color::rgb(100, 100, 100)));
  // One that appears fades in from transparent.
  let appearing = Style::new().lerp(&to, 0.5).gradient.expect("on its way");
  assert_eq!(appearing.stops()[0].1, Color::rgba(200, 200, 200, 128));
  // Different shapes, or a different number of colors, switch at once.
  let radial = Style::new().gradient(Gradient::radial(black, white));
  assert_eq!(from.lerp(&radial, 0.5).gradient, radial.gradient);
  let three = Style::new().gradient(Gradient::linear(0.0, black, white).stop(0.5, black));
  assert_eq!(from.lerp(&three, 0.5).gradient, three.gradient);
}

#[test]
fn a_shadow_reaches_past_the_box_by_its_offset_spread_and_blur() {
  const LIFTED: Style =
    Style::new().shadow(Shadow::new(2.0, 4.0, 8.0, Color::rgba(0, 0, 0, 128)).spread(1.0));
  let shadow = LIFTED.shadow.expect("set by the builder");
  assert_eq!(
    (shadow.x, shadow.y, shadow.blur, shadow.spread),
    (2.0, 4.0, 8.0, 1.0)
  );
  // Moved by 2, 4, grown by the spread and one and a half blurs.
  let reach = shadow.reach(Rect::new(10.0, 10.0, 20.0, 10.0));
  assert_eq!(reach, Rect::new(-1.0, 1.0, 46.0, 36.0));
  assert_eq!(Style::new().merge(LIFTED).shadow, Some(shadow));
  // A spread that swallows the box still reaches between its edges, so a
  // fade from it to another shadow stays inside the two.
  let gone = Shadow::new(-100.0, 0.0, 0.0, Color::rgb(0, 0, 0)).spread(-50.0);
  assert_eq!(
    gone.reach(Rect::new(10.0, 10.0, 20.0, 20.0)),
    Rect::new(-120.0, -20.0, 80.0, 80.0)
  );
  // A shadow nobody can draw reaches nowhere past the box.
  let box_ = Rect::new(10.0, 10.0, 20.0, 10.0);
  assert_eq!(
    Shadow::new(f32::NAN, 0.0, 1.0, Color::rgb(0, 0, 0)).reach(box_),
    box_
  );
}

#[test]
fn shadows_blend_and_fade_in_from_clear() {
  let gray = Color::rgba(0, 0, 0, 200);
  let from = Style::new().shadow(Shadow::new(0.0, 0.0, 2.0, gray));
  let to = Style::new().shadow(Shadow::new(0.0, 4.0, 10.0, gray).spread(2.0));
  let half = from.lerp(&to, 0.5).shadow.expect("both set one");
  assert_eq!((half.y, half.blur, half.spread), (2.0, 6.0, 1.0));
  let appearing = Style::new().lerp(&to, 0.5).shadow.expect("on its way");
  assert_eq!(
    (appearing.y, appearing.color),
    (4.0, Color::rgba(0, 0, 0, 100))
  );
  let going = to.lerp(&Style::new(), 0.75).shadow.expect("on its way out");
  assert_eq!(going.color, Color::rgba(0, 0, 0, 50));
}

#[test]
fn merge_takes_each_place_field_from_the_style_on_top() {
  const BASE: Style = Style::new()
    .grow(1.0)
    .min_width(10.0)
    .left(4.0)
    .z(2)
    .span(2, 1);
  const OVER: Style = Style::new()
    .grow(2.0)
    .max_height(8.0)
    .align_self(crate::Align::End)
    .top(1.0);
  let merged = BASE.merge(OVER);
  assert_eq!(merged.grow, Some(2.0));
  assert_eq!(merged.min_width, Some(10.0));
  assert_eq!(merged.max_height, Some(8.0));
  assert_eq!(merged.align_self, Some(crate::Align::End));
  assert_eq!((merged.left, merged.top), (Some(4.0), Some(1.0)));
  assert_eq!(merged.z, Some(2));
  assert_eq!(merged.span, Some((2, 1)));
}

#[test]
fn place_fields_change_the_layout_and_z_only_the_paint() {
  let base = Style::new();
  for other in [
    base.grow(1.0),
    base.shrink(0.0),
    base.basis(4.0),
    base.min_width(1.0),
    base.min_height(1.0),
    base.max_width(1.0),
    base.max_height(1.0),
    base.align_self(crate::Align::Center),
    base.justify_self(crate::Align::Center),
    base.left(1.0),
    base.top(1.0),
    base.right(1.0),
    base.bottom(1.0),
    base.span(2, 2),
  ] {
    assert!(!base.same_layout(&other), "{other:?}");
  }
  assert!(base.same_layout(&base.z(3)));
}

#[test]
fn hsl_gives_the_colors_css_does() {
  // Each against what CSS's `hsl()` gives.
  const STEEL: Color = Color::hsl(210.0, 0.5, 0.4);
  assert_eq!(STEEL, Color::rgb(51, 102, 153));
  assert_eq!(Color::hsl(0.0, 1.0, 0.5), Color::rgb(255, 0, 0));
  assert_eq!(Color::hsl(120.0, 1.0, 0.25), Color::rgb(0, 128, 0));
  assert_eq!(Color::hsl(300.0, 0.76, 0.72), Color::rgb(238, 129, 238));
  assert_eq!(Color::hsl(30.0, 1.0, 0.5), Color::rgb(255, 128, 0));
  assert_eq!(Color::hsl(0.0, 0.0, 0.5), Color::rgb(128, 128, 128));
  // A hue wraps around; the others clamp.
  assert_eq!(Color::hsl(570.0, 0.5, 0.4), STEEL);
  assert_eq!(Color::hsl(-150.0, 0.5, 0.4), STEEL);
  assert_eq!(Color::hsl(10.0, 2.0, 1.5), Color::rgb(255, 255, 255));
  assert_eq!(Color::hsl(10.0, -1.0, 0.5), Color::rgb(128, 128, 128));
  assert_eq!(
    Color::hsla(210.0, 0.5, 0.4, 0.5),
    Color::rgba(51, 102, 153, 128)
  );
  assert_eq!(Color::hsla(210.0, 0.5, 0.4, 7.0).a, 255);
  // Not a number anywhere gives opaque black.
  for (h, s, l, a) in [
    (f32::NAN, 0.5, 0.5, 1.0),
    (0.0, f32::NAN, 0.5, 1.0),
    (0.0, 0.5, f32::NAN, 1.0),
    (0.0, 0.5, 0.5, f32::NAN),
    (f32::INFINITY, 0.5, 0.5, 1.0),
  ] {
    assert_eq!(
      Color::hsla(h, s, l, a),
      Color::rgb(0, 0, 0),
      "{h} {s} {l} {a}"
    );
  }
}

#[test]
fn lighter_and_darker_move_the_hsl_lightness() {
  const STEEL: Color = Color::rgb(51, 102, 153);
  const LIGHT: Color = STEEL.lighter(0.1);
  // hsl(210, 50%, 50%) and hsl(210, 50%, 30%).
  assert_eq!(LIGHT, Color::rgb(64, 128, 191));
  assert_eq!(STEEL.darker(0.1), Color::rgb(38, 77, 115));
  // They keep the alpha, clamp the lightness and the amount, and a NaN
  // leaves the color as it was.
  let clear = Color::rgba(51, 102, 153, 40);
  assert_eq!(clear.lighter(0.1), Color::rgba(64, 128, 191, 40));
  assert_eq!(STEEL.lighter(5.0), Color::rgb(255, 255, 255));
  assert_eq!(STEEL.darker(5.0), Color::rgb(0, 0, 0));
  assert_eq!(STEEL.lighter(-1.0), STEEL);
  assert_eq!(STEEL.darker(f32::NAN), STEEL);
  // Going through HSL and back keeps every color.
  for color in [
    Color::rgb(0x1e, 0x1e, 0x2e),
    Color::rgb(0xcd, 0xd6, 0xf4),
    Color::rgb(0xf3, 0x8b, 0xa8),
    Color::rgb(255, 0, 255),
    Color::rgb(1, 2, 3),
    Color::rgb(254, 255, 253),
  ] {
    assert_eq!(color.lighter(0.0), color);
  }
}

#[test]
fn with_alpha_and_mix_work_on_srgb_channels() {
  const BASE: Color = Color::hex("#1e1e2e");
  const VEIL: Color = BASE.with_alpha(0.5);
  assert_eq!(VEIL, Color::rgba(0x1e, 0x1e, 0x2e, 128));
  assert_eq!(BASE.with_alpha(2.0).a, 255);
  assert_eq!(BASE.with_alpha(-1.0).a, 0);
  assert_eq!(VEIL.with_alpha(f32::NAN), VEIL);
  // Each channel on its own, alpha included, as a style fade blends.
  const RED: Color = Color::rgb(255, 0, 0);
  const HALF: Color = RED.mix(Color::rgba(0, 0, 255, 0), 0.5);
  assert_eq!(HALF, Color::rgba(128, 0, 128, 128));
  assert_eq!(RED.mix(BASE, 0.0), RED);
  assert_eq!(RED.mix(BASE, 1.0), BASE);
  assert_eq!(RED.mix(BASE, 9.0), BASE);
  assert_eq!(RED.mix(BASE, -1.0), RED);
  assert_eq!(RED.mix(BASE, f32::NAN), RED);
  assert_eq!(
    Color::rgb(0, 100, 200).mix(Color::rgb(100, 0, 0), 0.25),
    Color::rgb(25, 75, 150)
  );
}

#[test]
fn radii_sets_each_corner_and_radius_all_four() {
  const TAB: Style = Style::new().radii(8.0, 8.0, 0.0, 2.0);
  assert_eq!(
    TAB.radius,
    Some(Radii {
      top_left: 8.0,
      top_right: 8.0,
      bottom_right: 0.0,
      bottom_left: 2.0,
    })
  );
  assert_eq!(Style::new().radius(3.0).radius, Some(Radii::all(3.0)));
  assert_eq!(
    Style::new().radius(3.0).radius,
    Style::new().radii(3.0, 3.0, 3.0, 3.0).radius
  );
  // The style on top takes all four corners.
  assert_eq!(
    TAB.merge(Style::new().radius(1.0)).radius,
    Some(Radii::all(1.0))
  );
  assert_eq!(Style::new().radius(1.0).merge(TAB).radius, TAB.radius);
  assert!(Style::new().same_layout(&TAB), "corners are paint only");
}

#[test]
fn a_fade_moves_each_corner_from_where_it_was() {
  let round = Style::new().radius(4.0);
  let tab = Style::new().radii(0.0, 8.0, 16.0, 4.0);
  let half = round.lerp(&tab, 0.5).radius.expect("both set corners");
  assert_eq!(
    [
      half.top_left,
      half.top_right,
      half.bottom_right,
      half.bottom_left
    ],
    [2.0, 6.0, 10.0, 4.0]
  );
  // Corners only one side sets fade from or to square ones.
  let appearing = Style::new().lerp(&tab, 0.25).radius.expect("on its way");
  assert_eq!(
    appearing,
    Radii::all(0.0).lerp(&tab.radius.expect("set"), 0.25)
  );
  assert_eq!(appearing.bottom_right, 4.0);
  let going = tab
    .lerp(&Style::new(), 0.75)
    .radius
    .expect("on its way out");
  assert_eq!(going.bottom_right, 4.0);
}

#[test]
fn a_sweep_gradient_blends_with_another_sweep_only() {
  const RED: Color = Color::rgb(255, 0, 0);
  const BLUE: Color = Color::rgb(0, 0, 255);
  const RING: Gradient = Gradient::sweep(45.0, RED, BLUE).stop(0.5, RED);
  assert_eq!(RING.shape, GradientShape::Sweep(45.0));
  assert_eq!(RING.stops(), [(0.0, RED), (0.5, RED), (1.0, BLUE)]);
  let from = Style::new().gradient(Gradient::sweep(0.0, RED, BLUE));
  let to = Style::new().gradient(Gradient::sweep(90.0, BLUE, BLUE));
  let half = from.lerp(&to, 0.5).gradient.expect("both set one");
  assert_eq!(half.shape, GradientShape::Sweep(45.0));
  assert_eq!(half.stops()[0], (0.0, Color::rgb(128, 0, 128)));
  let linear = Style::new().gradient(Gradient::linear(0.0, RED, BLUE));
  assert_eq!(from.lerp(&linear, 0.5).gradient, linear.gradient);
  assert_eq!(linear.lerp(&from, 0.5).gradient, from.gradient);
}

#[test]
fn border_dash_is_a_field_of_its_own() {
  const DASHED: Style = Style::new()
    .border(2.0, Color::rgb(1, 2, 3))
    .border_dash(6.0, 4.0);
  assert_eq!(DASHED.border_dash, Some((6.0, 4.0)));
  assert_eq!(DASHED.border.map(|b| b.width), Some(2.0));
  // Either order gives the same style.
  const DOTTED: Style = Style::new()
    .border_dash(0.0, 4.0)
    .border(2.0, Color::rgb(1, 2, 3));
  assert_eq!(DOTTED.border_dash, Some((0.0, 4.0)));
  // A dash alone sets no border.
  assert_eq!(Style::new().border_dash(1.0, 1.0).border, None);
  assert!(Style::new().same_layout(&DASHED), "a dash is paint only");
}

#[test]
fn a_hover_style_can_dash_a_border_another_style_sets() {
  const CARD: Style = Style::new().border(2.0, Color::rgb(1, 2, 3)).radius(8.0);
  const HOVER: Style = Style::new().border_dash(4.0, 2.0);
  let hovered = CARD.merge(HOVER);
  assert_eq!(hovered.border, CARD.border, "the card's border stays");
  assert_eq!(hovered.border_dash, Some((4.0, 2.0)));
  assert_eq!(
    CARD.border_dash(1.0, 1.0).merge(HOVER).border_dash,
    Some((4.0, 2.0)),
    "the dash on top wins"
  );
  // And a style that sets another border keeps the dash under it.
  let thick = hovered.merge(Style::new().border(4.0, Color::rgb(0, 0, 0)));
  assert_eq!(thick.border_dash, Some((4.0, 2.0)));
}

#[test]
fn a_fading_border_takes_its_dash_from_the_end() {
  let solid = Style::new().border(2.0, Color::rgb(0, 0, 0));
  let dashed = solid.border(4.0, Color::rgb(0, 0, 0)).border_dash(6.0, 4.0);
  let half = solid.lerp(&dashed, 0.5);
  assert_eq!(half.border.map(|b| b.width), Some(3.0));
  assert_eq!(half.border_dash, Some((6.0, 4.0)));
  let back = dashed.lerp(&solid, 0.5);
  assert_eq!(back.border.map(|b| b.width), Some(3.0));
  assert_eq!(back.border_dash, None);
  // Only the dash changing is no fade: it is the end's at once.
  assert_eq!(
    solid.lerp(&solid.border_dash(1.0, 1.0), 0.1).border_dash,
    Some((1.0, 1.0))
  );
}

#[test]
fn a_mask_is_a_const_paint_field_that_merges_and_changes_at_once() {
  const FADE: Gradient = Gradient::linear(180.0, Color::rgb(0, 0, 0), Color::rgba(0, 0, 0, 0));
  const ROUND: Style = Style::new().mask(Mask::ellipse());
  const FADING: Style = ROUND.mask(Mask::gradient(FADE));
  const STAR: Style = Style::new().mask(Mask::image("/star.png"));
  assert_eq!(ROUND.mask, Some(Mask::Ellipse));
  assert_eq!(FADING.mask, Some(Mask::Gradient(FADE)));
  assert_eq!(STAR.mask, Some(Mask::Image("/star.png")));
  assert_eq!(ROUND.merge(Style::new()).mask, ROUND.mask);
  assert_eq!(ROUND.merge(STAR).mask, STAR.mask);
  assert!(ROUND.same_layout(&Style::new()), "a paint property");
  // A fade takes the end style's mask from its start.
  for t in [0.0, 0.5] {
    assert_eq!(ROUND.lerp(&STAR, t).mask, STAR.mask);
    assert_eq!(ROUND.lerp(&Style::new(), t).mask, None);
    assert_eq!(Style::new().lerp(&ROUND, t).mask, ROUND.mask);
  }
}

#[test]
fn a_content_shadow_blends_as_a_box_shadow_and_switches_kind_at_once() {
  let gray = Color::rgba(0, 0, 0, 200);
  const LIFTED: Shadow = Shadow::new(0.0, 2.0, 4.0, Color::rgb(0, 0, 0)).follow_content();
  const { assert!(LIFTED.follow_content) };
  assert!(!LIFTED.cut_out && !Shadow::new(0.0, 0.0, 0.0, gray).follow_content);
  assert!(LIFTED.cut_out().follow_content, "the two go together");
  let from = Style::new().shadow(Shadow::new(0.0, 0.0, 2.0, gray));
  let to = Style::new().shadow(Shadow::new(0.0, 4.0, 10.0, gray).follow_content());
  let half = from.lerp(&to, 0.5).shadow.expect("both set one");
  assert_eq!((half.y, half.blur, half.follow_content), (2.0, 6.0, true));
  let back = to.lerp(&from, 0.5).shadow.expect("both set one");
  assert!(!back.follow_content);
  // It reaches past what casts it as far as a box shadow does.
  let rect = Rect::new(10.0, 10.0, 20.0, 10.0);
  assert_eq!(
    LIFTED.reach(rect),
    Shadow::new(0.0, 2.0, 4.0, gray).reach(rect)
  );
}