1use crate::{
2 constraint,
3 widget::DataCell,
4};
5
6pub mod character_pixel;
7pub mod color_pixel;
8pub mod monochrome_pixel;
9
10pub trait Pixel: Copy + Into<DataCell>
12where
13 Self: Sized,
14{
15 type U: Copy;
16
17 const WIDTH: usize;
19 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 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 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 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}