color.rs

  1#![allow(dead_code)]
  2
  3use serde::de::{self, Deserialize, Deserializer, Visitor};
  4use std::fmt;
  5use std::num::ParseIntError;
  6
  7pub fn rgb<C: From<Rgba>>(hex: u32) -> C {
  8    let r = ((hex >> 16) & 0xFF) as f32 / 255.0;
  9    let g = ((hex >> 8) & 0xFF) as f32 / 255.0;
 10    let b = (hex & 0xFF) as f32 / 255.0;
 11    Rgba { r, g, b, a: 1.0 }.into()
 12}
 13
 14#[derive(Clone, Copy, Default, Debug)]
 15pub struct Rgba {
 16    pub r: f32,
 17    pub g: f32,
 18    pub b: f32,
 19    pub a: f32,
 20}
 21
 22impl Rgba {
 23    pub fn blend(&self, other: Rgba) -> Self {
 24        if other.a >= 1.0 {
 25            return other;
 26        } else if other.a <= 0.0 {
 27            return *self;
 28        } else {
 29            return Rgba {
 30                r: (self.r * (1.0 - other.a)) + (other.r * other.a),
 31                g: (self.g * (1.0 - other.a)) + (other.g * other.a),
 32                b: (self.b * (1.0 - other.a)) + (other.b * other.a),
 33                a: self.a,
 34            };
 35        }
 36    }
 37}
 38
 39struct RgbaVisitor;
 40
 41impl<'de> Visitor<'de> for RgbaVisitor {
 42    type Value = Rgba;
 43
 44    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
 45        formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
 46    }
 47
 48    fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
 49        if value.len() == 7 || value.len() == 9 {
 50            let r = u8::from_str_radix(&value[1..3], 16).unwrap() as f32 / 255.0;
 51            let g = u8::from_str_radix(&value[3..5], 16).unwrap() as f32 / 255.0;
 52            let b = u8::from_str_radix(&value[5..7], 16).unwrap() as f32 / 255.0;
 53            let a = if value.len() == 9 {
 54                u8::from_str_radix(&value[7..9], 16).unwrap() as f32 / 255.0
 55            } else {
 56                1.0
 57            };
 58            Ok(Rgba { r, g, b, a })
 59        } else {
 60            Err(E::custom(
 61                "Bad format for RGBA. Expected #rrggbb or #rrggbbaa.",
 62            ))
 63        }
 64    }
 65}
 66
 67impl<'de> Deserialize<'de> for Rgba {
 68    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
 69        deserializer.deserialize_str(RgbaVisitor)
 70    }
 71}
 72
 73impl From<Hsla> for Rgba {
 74    fn from(color: Hsla) -> Self {
 75        let h = color.h;
 76        let s = color.s;
 77        let l = color.l;
 78
 79        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
 80        let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
 81        let m = l - c / 2.0;
 82        let cm = c + m;
 83        let xm = x + m;
 84
 85        let (r, g, b) = match (h * 6.0).floor() as i32 {
 86            0 | 6 => (cm, xm, m),
 87            1 => (xm, cm, m),
 88            2 => (m, cm, xm),
 89            3 => (m, xm, cm),
 90            4 => (xm, m, cm),
 91            _ => (cm, m, xm),
 92        };
 93
 94        Rgba {
 95            r,
 96            g,
 97            b,
 98            a: color.a,
 99        }
100    }
101}
102
103impl TryFrom<&'_ str> for Rgba {
104    type Error = ParseIntError;
105
106    fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
107        let r = u8::from_str_radix(&value[1..3], 16)? as f32 / 255.0;
108        let g = u8::from_str_radix(&value[3..5], 16)? as f32 / 255.0;
109        let b = u8::from_str_radix(&value[5..7], 16)? as f32 / 255.0;
110        let a = if value.len() > 7 {
111            u8::from_str_radix(&value[7..9], 16)? as f32 / 255.0
112        } else {
113            1.0
114        };
115
116        Ok(Rgba { r, g, b, a })
117    }
118}
119
120#[derive(Default, Copy, Clone, Debug, PartialEq)]
121#[repr(C)]
122pub struct Hsla {
123    pub h: f32,
124    pub s: f32,
125    pub l: f32,
126    pub a: f32,
127}
128
129impl Eq for Hsla {}
130
131pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
132    Hsla {
133        h: h.clamp(0., 1.),
134        s: s.clamp(0., 1.),
135        l: l.clamp(0., 1.),
136        a: a.clamp(0., 1.),
137    }
138}
139
140pub fn black() -> Hsla {
141    Hsla {
142        h: 0.,
143        s: 0.,
144        l: 0.,
145        a: 1.,
146    }
147}
148
149pub fn white() -> Hsla {
150    Hsla {
151        h: 0.,
152        s: 0.,
153        l: 1.,
154        a: 1.,
155    }
156}
157
158pub fn red() -> Hsla {
159    Hsla {
160        h: 0.,
161        s: 1.,
162        l: 0.5,
163        a: 1.,
164    }
165}
166
167impl Hsla {
168    /// Returns true if the HSLA color is fully transparent, false otherwise.
169    pub fn is_transparent(&self) -> bool {
170        self.a == 0.0
171    }
172
173    /// Blends `other` on top of `self` based on `other`'s alpha value. The resulting color is a combination of `self`'s and `other`'s colors.
174    ///
175    /// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
176    /// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
177    /// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
178    ///
179    /// Assumptions:
180    /// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
181    /// - The relative contributions of `self` and `other` is based on `self`'s alpha value (`self.a`) and `other`'s  alpha value (`other.a`), `self` contributing `self.a * (1.0 - other.a)` and `other` contributing it's own alpha value.
182    /// - RGB color components are contained in the range [0, 1].
183    /// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
184    pub fn blend(self, other: Hsla) -> Hsla {
185        let alpha = other.a;
186
187        if alpha >= 1.0 {
188            return other;
189        } else if alpha <= 0.0 {
190            return self;
191        } else {
192            let converted_self = Rgba::from(self);
193            let converted_other = Rgba::from(other);
194            let blended_rgb = converted_self.blend(converted_other);
195            return Hsla::from(blended_rgb);
196        }
197    }
198
199    /// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
200    /// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
201    pub fn fade_out(&mut self, factor: f32) {
202        self.a *= 1.0 - factor.clamp(0., 1.);
203    }
204}
205
206impl From<Rgba> for Hsla {
207    fn from(color: Rgba) -> Self {
208        let r = color.r;
209        let g = color.g;
210        let b = color.b;
211
212        let max = r.max(g.max(b));
213        let min = r.min(g.min(b));
214        let delta = max - min;
215
216        let l = (max + min) / 2.0;
217        let s = if l == 0.0 || l == 1.0 {
218            0.0
219        } else if l < 0.5 {
220            delta / (2.0 * l)
221        } else {
222            delta / (2.0 - 2.0 * l)
223        };
224
225        let h = if delta == 0.0 {
226            0.0
227        } else if max == r {
228            ((g - b) / delta).rem_euclid(6.0) / 6.0
229        } else if max == g {
230            ((b - r) / delta + 2.0) / 6.0
231        } else {
232            ((r - g) / delta + 4.0) / 6.0
233        };
234
235        Hsla {
236            h,
237            s,
238            l,
239            a: color.a,
240        }
241    }
242}
243
244impl<'de> Deserialize<'de> for Hsla {
245    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
246    where
247        D: Deserializer<'de>,
248    {
249        // First, deserialize it into Rgba
250        let rgba = Rgba::deserialize(deserializer)?;
251
252        // Then, use the From<Rgba> for Hsla implementation to convert it
253        Ok(Hsla::from(rgba))
254    }
255}