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