gpu/data/textures/
color_format.rs

1// ref https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
2
3/// Kinds of `ColorFormat`s.
4#[derive(Clone)]
5pub enum ColorFormat {
6    /// Red only `ColorFormat`.
7    R,
8    /// Red and green `ColorFormat`.
9    RG,
10    /// Red, green and blue `ColorFormat`.
11    RGB,
12    /// Red, green, blue and alpha `ColorFormat`.
13    RGBA
14}
15
16impl ColorFormat {
17    /// Creates a `ColorFormat` with `n_components`, where `n_components` can be anything from 0 to
18    /// 3. Any value above it will be clipped to 3.
19    pub fn components(n_components: usize) -> ColorFormat {
20        match n_components {
21            0..=1 => ColorFormat::R,
22            2     => ColorFormat::RG,
23            3     => ColorFormat::RGB,
24            _     => ColorFormat::RGBA
25        }
26    }
27
28    /// Gets the size of the `ColorFormat` in bytes.
29    pub fn size(&self) -> usize {
30        match self {
31            ColorFormat::R    => 1,
32            ColorFormat::RG   => 2,
33            ColorFormat::RGB  => 3,
34            ColorFormat::RGBA => 4
35        }
36    }
37
38    /// Gets `OpenGL` internal enumeration.
39    pub fn get_format(&self) -> u32 {
40        match self {
41            ColorFormat::R    => glow::RED,
42            ColorFormat::RG   => glow::RG,
43            ColorFormat::RGB  => glow::RGB,
44            ColorFormat::RGBA => glow::RGBA
45        }
46    }
47}