velox_engine/display/
pixel.rs1use crate::style::Color;
4
5#[repr(u8)]
7#[derive(Copy, Clone, PartialEq, Eq, Debug)]
8pub enum SpriteFormat {
9 A4 = 0,
11 A8 = 1,
13 Rgb565 = 2,
15 Rgba8888 = 5,
17}
18
19
20pub trait PixelFormat: Copy + Clone + Default + PartialEq + Eq + Send + Sync + 'static {
22 const BLACK: Self;
24
25 fn from_color(c: Color) -> Self;
27 fn to_color(self) -> Color;
29 #[inline(always)]
31 fn blend(self, fg: Color, coverage: u8) -> Self {
32 let alpha = (((fg.a() as u32) * (coverage as u32) + 127) / 255) as u16;
33 if alpha == 0 {
34 return self;
35 }
36 if alpha >= 255 {
37 return Self::from_color(fg);
38 }
39 let bg = self.to_color();
40 let inv = 255 - alpha;
41 let r = (((fg.r() as u16) * alpha + (bg.r() as u16) * inv + 127) / 255) as u8;
42 let g = (((fg.g() as u16) * alpha + (bg.g() as u16) * inv + 127) / 255) as u8;
43 let b = (((fg.b() as u16) * alpha + (bg.b() as u16) * inv + 127) / 255) as u8;
44 Self::from_color(Color::rgb(r, g, b))
45 }
46}
47
48#[repr(transparent)]
50#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
51pub struct Rgb565(pub u16);
52
53impl PixelFormat for Rgb565 {
54 const BLACK: Self = Self(0);
55
56 #[inline(always)]
57 fn from_color(c: Color) -> Self {
58 let r = ((c.r() as u16) >> 3) & 0x1F;
59 let g = ((c.g() as u16) >> 2) & 0x3F;
60 let b = ((c.b() as u16) >> 3) & 0x1F;
61 Self((r << 11) | (g << 5) | b)
62 }
63
64 #[inline(always)]
65 fn to_color(self) -> Color {
66 let r5 = ((self.0 >> 11) & 0x1F) as u8;
67 let g6 = ((self.0 >> 5) & 0x3F) as u8;
68 let b5 = (self.0 & 0x1F) as u8;
69 let r = (r5 << 3) | (r5 >> 2);
70 let g = (g6 << 2) | (g6 >> 4);
71 let b = (b5 << 3) | (b5 >> 2);
72 Color::rgb(r, g, b)
73 }
74}
75
76#[repr(transparent)]
78#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
79pub struct Xrgb8888(pub u32);
80
81impl PixelFormat for Xrgb8888 {
82 const BLACK: Self = Self(0);
83
84 #[inline(always)]
85 fn from_color(c: Color) -> Self {
86 Self(0xFF00_0000 | (c.0 & 0x00FF_FFFF))
87 }
88
89 #[inline(always)]
90 fn to_color(self) -> Color {
91 Color(0xFF00_0000 | (self.0 & 0x00FF_FFFF))
92 }
93}
94
95#[repr(transparent)]
100#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
101pub struct Rgba8888(pub u32);
102
103impl Rgba8888 {
104 #[inline(always)]
106 pub const fn to_rgba_bytes(self) -> [u8; 4] {
107 self.0.to_le_bytes()
108 }
109}
110
111impl PixelFormat for Rgba8888 {
112 const BLACK: Self = Self(0xFF00_0000);
113
114 #[inline(always)]
115 fn from_color(c: Color) -> Self {
116 let r = c.r() as u32;
117 let g = (c.g() as u32) << 8;
118 let b = (c.b() as u32) << 16;
119 let a = (c.a() as u32) << 24;
120 Self(a | b | g | r)
121 }
122
123 #[inline(always)]
124 fn to_color(self) -> Color {
125 let r = (self.0 & 0xFF) as u8;
126 let g = ((self.0 >> 8) & 0xFF) as u8;
127 let b = ((self.0 >> 16) & 0xFF) as u8;
128 let a = ((self.0 >> 24) & 0xFF) as u8;
129 Color::rgba(r, g, b, a)
130 }
131
132 #[inline(always)]
133 fn blend(self, fg: Color, coverage: u8) -> Self {
134 let sa = (((fg.a() as u32) * (coverage as u32) + 127) / 255) as u16;
135 if sa == 0 {
136 return self;
137 }
138 if sa >= 255 {
139 return Self::from_color(Color::rgba(fg.r(), fg.g(), fg.b(), 255));
140 }
141 let bg = self.to_color();
142 let da = bg.a() as u16;
143 let inv = 255 - sa;
144 let out_a = sa + ((da * inv + 127) / 255);
145 let r = (((fg.r() as u16) * sa + (bg.r() as u16) * inv + 127) / 255) as u8;
146 let g = (((fg.g() as u16) * sa + (bg.g() as u16) * inv + 127) / 255) as u8;
147 let b = (((fg.b() as u16) * sa + (bg.b() as u16) * inv + 127) / 255) as u8;
148 Self::from_color(Color::rgba(r, g, b, out_a.min(255) as u8))
149 }
150}