Skip to main content

ferroui_base/platform/
pixel_format.rs

1use std::fmt;
2
3#[doc(hidden)]
4#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
5pub enum PixelFormatEnum {
6    Rgb565,
7    Rgba8888,
8    Bgra8888,
9    BlackWhite,
10    Gray2,
11    Gray4,
12    Gray8,
13    Gray16,
14    Gray32Float,
15    Rgba64,
16    Rgb24,
17    Rgb32,
18    Bgr24,
19    Bgr32,
20    Bgr555,
21    Bgr565,
22}
23
24/// The format of the pixels of a bitmap or framebuffer.
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26pub struct PixelFormat {
27    pub(crate) format: PixelFormatEnum,
28}
29
30impl PixelFormat {
31    pub const RGB565: PixelFormat = PixelFormats::RGB565;
32    pub const RGBA8888: PixelFormat = PixelFormats::RGBA8888;
33    pub const RGB32: PixelFormat = PixelFormats::RGB32;
34    pub const BGRA8888: PixelFormat = PixelFormats::BGRA8888;
35
36    #[doc(hidden)]
37    pub const fn new(format: PixelFormatEnum) -> Self {
38        Self { format }
39    }
40
41    /// The number of bits one pixel occupies.
42    pub fn bits_per_pixel(&self) -> u32 {
43        use PixelFormatEnum::*;
44        match self.format {
45            BlackWhite => 1,
46            Gray2 => 2,
47            Gray4 => 4,
48            Gray8 => 8,
49            Rgb565 | Bgr555 | Bgr565 | Gray16 => 16,
50            Bgr24 | Rgb24 => 24,
51            Rgba64 => 64,
52            Rgba8888 | Bgra8888 | Gray32Float | Rgb32 | Bgr32 => 32,
53        }
54    }
55
56    /// Whether the format carries an alpha channel.
57    pub fn has_alpha(&self) -> bool {
58        matches!(self.format, PixelFormatEnum::Rgba8888 | PixelFormatEnum::Bgra8888 | PixelFormatEnum::Rgba64)
59    }
60}
61
62impl fmt::Display for PixelFormat {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        fmt::Debug::fmt(&self.format, f)
65    }
66}
67
68/// The well-known pixel formats.
69pub struct PixelFormats;
70
71impl PixelFormats {
72    pub const RGB565: PixelFormat = PixelFormat::new(PixelFormatEnum::Rgb565);
73    pub const RGBA8888: PixelFormat = PixelFormat::new(PixelFormatEnum::Rgba8888);
74    pub const RGBA64: PixelFormat = PixelFormat::new(PixelFormatEnum::Rgba64);
75    pub const BGRA8888: PixelFormat = PixelFormat::new(PixelFormatEnum::Bgra8888);
76    pub const BLACK_WHITE: PixelFormat = PixelFormat::new(PixelFormatEnum::BlackWhite);
77    pub const GRAY2: PixelFormat = PixelFormat::new(PixelFormatEnum::Gray2);
78    pub const GRAY4: PixelFormat = PixelFormat::new(PixelFormatEnum::Gray4);
79    pub const GRAY8: PixelFormat = PixelFormat::new(PixelFormatEnum::Gray8);
80    pub const GRAY16: PixelFormat = PixelFormat::new(PixelFormatEnum::Gray16);
81    pub const GRAY32_FLOAT: PixelFormat = PixelFormat::new(PixelFormatEnum::Gray32Float);
82    pub const RGB24: PixelFormat = PixelFormat::new(PixelFormatEnum::Rgb24);
83    pub const RGB32: PixelFormat = PixelFormat::new(PixelFormatEnum::Rgb32);
84    pub const BGR24: PixelFormat = PixelFormat::new(PixelFormatEnum::Bgr24);
85    pub const BGR32: PixelFormat = PixelFormat::new(PixelFormatEnum::Bgr32);
86    pub const BGR555: PixelFormat = PixelFormat::new(PixelFormatEnum::Bgr555);
87    pub const BGR565: PixelFormat = PixelFormat::new(PixelFormatEnum::Bgr565);
88}