re_types/datatypes/
rgba32_ext.rs

1use super::Rgba32;
2
3impl Rgba32 {
4    /// Black and opaque.
5    pub const BLACK: Self = Self::from_rgb(0, 0, 0);
6
7    /// White and opaque.
8    pub const WHITE: Self = Self::from_rgb(255, 255, 255);
9
10    /// Fully transparent (invisible).
11    pub const TRANSPARENT: Self = Self::from_unmultiplied_rgba(0, 0, 0, 0);
12
13    /// From gamma-space sRGB values.
14    #[inline]
15    pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
16        Self::from_unmultiplied_rgba(r, g, b, 255)
17    }
18
19    /// From gamma-space sRGB values, with a separate/unmultiplied alpha in linear-space.
20    #[inline]
21    pub const fn from_unmultiplied_rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
22        let [r, g, b, a] = [r as u32, g as u32, b as u32, a as u32];
23        Self((r << 24) | (g << 16) | (b << 8) | a)
24    }
25
26    /// From linear-space sRGB values in 0-1 range, with a separate/unmultiplied alpha.
27    ///
28    /// This is a lossy conversion.
29    #[cfg(feature = "ecolor")]
30    pub fn from_linear_unmultiplied_rgba_f32(r: f32, g: f32, b: f32, a: f32) -> Self {
31        #[expect(clippy::disallowed_methods)] // This is not a hard-coded color.
32        ecolor::Rgba::from_rgba_unmultiplied(r, g, b, a).into()
33    }
34
35    /// Most significant byte is `r`, least significant byte is `a`.
36    #[inline]
37    pub const fn from_u32(rgba: u32) -> Self {
38        Self(rgba)
39    }
40
41    /// `[r, g, b, a]`
42    #[inline]
43    pub const fn to_array(self) -> [u8; 4] {
44        [
45            (self.0 >> 24) as u8,
46            (self.0 >> 16) as u8,
47            (self.0 >> 8) as u8,
48            self.0 as u8,
49        ]
50    }
51
52    /// Most significant byte is `r`, least significant byte is `a`.
53    #[inline]
54    pub const fn to_u32(self) -> u32 {
55        self.0
56    }
57}
58
59impl From<(u8, u8, u8)> for Rgba32 {
60    #[inline]
61    fn from((r, g, b): (u8, u8, u8)) -> Self {
62        Self::from_rgb(r, g, b)
63    }
64}
65
66impl From<[u8; 3]> for Rgba32 {
67    #[inline]
68    fn from([r, g, b]: [u8; 3]) -> Self {
69        Self::from_rgb(r, g, b)
70    }
71}
72
73impl From<[u8; 4]> for Rgba32 {
74    #[inline]
75    fn from([r, g, b, a]: [u8; 4]) -> Self {
76        Self::from_unmultiplied_rgba(r, g, b, a)
77    }
78}
79
80impl From<(u8, u8, u8, u8)> for Rgba32 {
81    #[inline]
82    fn from((r, g, b, a): (u8, u8, u8, u8)) -> Self {
83        Self::from_unmultiplied_rgba(r, g, b, a)
84    }
85}
86
87#[cfg(feature = "ecolor")]
88impl From<Rgba32> for ecolor::Color32 {
89    fn from(color: Rgba32) -> Self {
90        let [r, g, b, a] = color.to_array();
91        #[expect(clippy::disallowed_methods)] // This is not a hard-coded color.
92        Self::from_rgba_unmultiplied(r, g, b, a)
93    }
94}
95
96#[cfg(feature = "ecolor")]
97impl From<Rgba32> for ecolor::Rgba {
98    fn from(color: Rgba32) -> Self {
99        let color: ecolor::Color32 = color.into();
100        color.into()
101    }
102}
103
104#[cfg(feature = "ecolor")]
105impl From<ecolor::Rgba> for Rgba32 {
106    fn from(val: ecolor::Rgba) -> Self {
107        val.to_srgba_unmultiplied().into()
108    }
109}
110
111#[cfg(feature = "ecolor")]
112impl From<ecolor::Color32> for Rgba32 {
113    fn from(val: ecolor::Color32) -> Self {
114        val.to_srgba_unmultiplied().into()
115    }
116}