console_display/
pixel.rs

1use crate::{
2    constraint,
3    widget::DataCell,
4};
5
6pub mod character_pixel;
7pub mod color_pixel;
8pub mod monochrome_pixel;
9
10// TODO: Check if this can be const
11pub trait Pixel: Copy + Into<DataCell>
12where
13    Self: Sized,
14{
15    type U: Copy;
16
17    /// The width of the block of pixels.
18    const WIDTH: usize;
19    /// The height of the block of pixels.
20    const HEIGHT: usize;
21
22    fn pixels(&self) -> &[Self::U; Self::WIDTH * Self::HEIGHT];
23
24    fn pixels_mut(&mut self)
25    -> &mut [Self::U; Self::WIDTH * Self::HEIGHT];
26
27    /// Returns the value of the block at the specified coordinates.
28    ///
29    /// # Errors
30    ///
31    /// Returns an error, if the coordinates are out of bounds.
32    fn subpixel(&self, x: usize, y: usize) -> Result<Self::U, String>
33    where
34        [(); Self::WIDTH * Self::HEIGHT]:,
35    {
36        self.pixels().get(x + y * Self::WIDTH).map_or_else(
37            || Err("Coordinates out of range.".to_string()),
38            |subpixel| Ok(*subpixel),
39        )
40    }
41
42    /// Returns the value of the block at the specified coordinates.
43    ///
44    /// # Errors
45    ///
46    /// Returns an error, if the coordinates are out of bounds.
47    fn set_subpixel(
48        &mut self,
49        x: usize,
50        y: usize,
51        value: Self::U,
52    ) -> Result<(), String>
53    where
54        [(); Self::WIDTH * Self::HEIGHT]:,
55    {
56        let index = x + y * Self::WIDTH;
57        if index < self.pixels().len() {
58            self.pixels_mut()[index] = value;
59            Ok(())
60        }
61        else {
62            Err("Coordinates out of range.".to_string())
63        }
64    }
65
66    fn new(pixels: [Self::U; Self::WIDTH * Self::HEIGHT]) -> Self;
67
68    /// Builds a block of pixels from a slice of pixels.
69    ///
70    /// # Errors
71    ///
72    /// Returns an error, if the number of pixels does not match the dimensions of the block.
73    fn build(args: &[Self::U]) -> Result<Self, String>
74    where
75        [(); Self::WIDTH * Self::HEIGHT]:,
76    {
77        <[Self::U; Self::WIDTH * Self::HEIGHT]>::try_from(args)
78            .map_or_else(
79                |_| {
80                    Err(format!(
81                        "Invalid number of arguments. Expected {}, got {}",
82                        Self::WIDTH * Self::HEIGHT,
83                        args.len()
84                    ))
85                },
86                |pixels| Ok(Self::new(pixels)),
87            )
88    }
89
90    fn subpixel_static<const X: usize, const Y: usize>(&self) -> Self::U
91    where
92        [(); Self::WIDTH * Self::HEIGHT]:,
93        constraint!(X < Self::WIDTH):,
94        constraint!(Y < Self::HEIGHT):,
95    {
96        self.pixels()[X + Y * Self::WIDTH]
97    }
98
99    fn set_subpixel_static<const X: usize, const Y: usize>(
100        &mut self,
101        value: Self::U,
102    ) where
103        [(); Self::WIDTH * Self::HEIGHT]:,
104        constraint!(X < Self::WIDTH):,
105        constraint!(Y < Self::HEIGHT):,
106    {
107        self.pixels_mut()[X + Y * Self::WIDTH] = value;
108    }
109}