Skip to main content

shuyi_ui/frame/raster/
test_card.rs

1//! The test card: a public test image for sprite items, generated, not
2//! art, so the same code always makes the same pixels. The raster's and
3//! `frame-png`'s tests paint it, and a game may write it into its test
4//! files.
5//!
6//! 16×16: a one-pixel white border around four coloured quadrants (red and
7//! green above blue and yellow), so a flip, a crop and a scale each show.
8
9use crate::prelude::*;
10
11/// Width and height of the card in pixels.
12pub const SIZE: u32 = 16;
13/// The border colour (RGBA).
14pub const BORDER: [u8; 4] = [255, 255, 255, 255];
15/// The quadrants' colours (RGBA): top-left, top-right, bottom-left,
16/// bottom-right.
17pub const QUADRANTS: [[u8; 4]; 4] = [
18    [204, 51, 51, 255],
19    [51, 170, 68, 255],
20    [51, 102, 204, 255],
21    [230, 190, 50, 255],
22];
23
24/// The colour of pixel `(x, y)`.
25fn pixel(x: u32, y: u32) -> [u8; 4] {
26    let edge = |v: u32| v == 0 || v == SIZE - 1;
27    if edge(x) || edge(y) {
28        return BORDER;
29    }
30    let half = SIZE / 2;
31    QUADRANTS[usize::from(y >= half) * 2 + usize::from(x >= half)]
32}
33
34/// The card's RGBA8 pixels, row-major.
35pub fn pixels() -> Vec<u8> {
36    (0..SIZE)
37        .flat_map(|y| (0..SIZE).flat_map(move |x| pixel(x, y)))
38        .collect()
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn border_and_quadrants() {
47        let [red, green, blue, yellow] = QUADRANTS;
48        for i in 0..SIZE {
49            for edge in [(i, 0), (i, 15), (0, i), (15, i)] {
50                assert_eq!(pixel(edge.0, edge.1), BORDER, "{edge:?}");
51            }
52        }
53        // Each quadrant's corners: just inside the border and at the middle.
54        for (colour, xs, ys) in [
55            (red, [1, 7], [1, 7]),
56            (green, [8, 14], [1, 7]),
57            (blue, [1, 7], [8, 14]),
58            (yellow, [8, 14], [8, 14]),
59        ] {
60            for x in xs {
61                for y in ys {
62                    assert_eq!(pixel(x, y), colour, "({x}, {y})");
63                }
64            }
65        }
66        let mut distinct = QUADRANTS.to_vec();
67        distinct.push(BORDER);
68        distinct.sort_unstable();
69        distinct.dedup();
70        assert_eq!(distinct.len(), 5);
71    }
72
73    #[test]
74    fn pixels_are_row_major() {
75        let rgba = pixels();
76        assert_eq!(rgba.len(), 16 * 16 * 4);
77        let at = |x: usize, y: usize| &rgba[(y * 16 + x) * 4..][..4];
78        assert_eq!(at(0, 0), BORDER);
79        assert_eq!(at(14, 1), QUADRANTS[1]);
80        assert_eq!(at(1, 14), QUADRANTS[2]);
81    }
82}