pride_overlay/
colour.rs

1/// Represents a colour in RGB format.
2#[derive(Clone, Copy)]
3pub struct Colour(pub(crate) u8, pub(crate) u8, pub(crate) u8);
4
5impl Colour {
6    /// Create a [Colour] from RGB components.
7    pub const fn rgb(r: u8, g: u8, b: u8) -> Colour {
8        Colour(r, g, b)
9    }
10
11    /// Create a [Colour] from a hexadecimal RGB value (e.g., `0xRRGGBB`).
12    pub const fn hex(hex: u32) -> Colour {
13        let r = ((hex >> 16) & 0xFF) as u8;
14        let g = ((hex >> 8) & 0xFF) as u8;
15        let b = (hex & 0xFF) as u8;
16        Colour(r, g, b)
17    }
18}
19
20// deserialize hex strings
21#[cfg(target_arch = "wasm32")]
22impl<'de> serde::Deserialize<'de> for Colour {
23    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
24    where
25        D: serde::Deserializer<'de>,
26    {
27        let s = String::deserialize(deserializer)?;
28        let s = s.trim_start_matches('#');
29        let hex = u32::from_str_radix(s, 16).map_err(serde::de::Error::custom)?;
30        Ok(Self::hex(hex))
31    }
32}
33
34#[cfg(target_arch = "wasm32")]
35impl serde::Serialize for Colour {
36    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
37    where
38        S: serde::Serializer,
39    {
40        serializer.serialize_str(&format!("#{:02X}{:02X}{:02X}", self.0, self.1, self.2))
41    }
42}