color.rs

  1#![allow(dead_code)]
  2
  3use anyhow::bail;
  4use serde::de::{self, Deserialize, Deserializer, Visitor};
  5use std::fmt;
  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(PartialEq, 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
 63struct RgbaVisitor;
 64
 65impl<'de> Visitor<'de> for RgbaVisitor {
 66    type Value = Rgba;
 67
 68    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
 69        formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
 70    }
 71
 72    fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
 73        Rgba::try_from(value).map_err(E::custom)
 74    }
 75}
 76
 77impl<'de> Deserialize<'de> for Rgba {
 78    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
 79        deserializer.deserialize_str(RgbaVisitor)
 80    }
 81}
 82
 83impl From<Hsla> for Rgba {
 84    fn from(color: Hsla) -> Self {
 85        let h = color.h;
 86        let s = color.s;
 87        let l = color.l;
 88
 89        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
 90        let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
 91        let m = l - c / 2.0;
 92        let cm = c + m;
 93        let xm = x + m;
 94
 95        let (r, g, b) = match (h * 6.0).floor() as i32 {
 96            0 | 6 => (cm, xm, m),
 97            1 => (xm, cm, m),
 98            2 => (m, cm, xm),
 99            3 => (m, xm, cm),
100            4 => (xm, m, cm),
101            _ => (cm, m, xm),
102        };
103
104        Rgba {
105            r,
106            g,
107            b,
108            a: color.a,
109        }
110    }
111}
112
113impl TryFrom<&'_ str> for Rgba {
114    type Error = anyhow::Error;
115
116    fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
117        const RGB: usize = "rgb".len();
118        const RGBA: usize = "rgba".len();
119        const RRGGBB: usize = "rrggbb".len();
120        const RRGGBBAA: usize = "rrggbbaa".len();
121
122        const EXPECTED_FORMATS: &'static str = "Expected #rgb, #rgba, #rrggbb, or #rrggbbaa";
123
124        let Some(("", hex)) = value.trim().split_once('#') else {
125            bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}");
126        };
127
128        let (r, g, b, a) = match hex.len() {
129            RGB | RGBA => {
130                let r = u8::from_str_radix(&hex[0..1], 16)?;
131                let g = u8::from_str_radix(&hex[1..2], 16)?;
132                let b = u8::from_str_radix(&hex[2..3], 16)?;
133                let a = if hex.len() == RGBA {
134                    u8::from_str_radix(&hex[3..4], 16)?
135                } else {
136                    0xf
137                };
138
139                /// Duplicates a given hex digit.
140                /// E.g., `0xf` -> `0xff`.
141                const fn duplicate(value: u8) -> u8 {
142                    value << 4 | value
143                }
144
145                (duplicate(r), duplicate(g), duplicate(b), duplicate(a))
146            }
147            RRGGBB | RRGGBBAA => {
148                let r = u8::from_str_radix(&hex[0..2], 16)?;
149                let g = u8::from_str_radix(&hex[2..4], 16)?;
150                let b = u8::from_str_radix(&hex[4..6], 16)?;
151                let a = if hex.len() == RRGGBBAA {
152                    u8::from_str_radix(&hex[6..8], 16)?
153                } else {
154                    0xff
155                };
156                (r, g, b, a)
157            }
158            _ => bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}"),
159        };
160
161        Ok(Rgba {
162            r: r as f32 / 255.,
163            g: g as f32 / 255.,
164            b: b as f32 / 255.,
165            a: a as f32 / 255.,
166        })
167    }
168}
169
170#[derive(Default, Copy, Clone, Debug)]
171#[repr(C)]
172pub struct Hsla {
173    pub h: f32,
174    pub s: f32,
175    pub l: f32,
176    pub a: f32,
177}
178
179impl PartialEq for Hsla {
180    fn eq(&self, other: &Self) -> bool {
181        self.h
182            .total_cmp(&other.h)
183            .then(self.s.total_cmp(&other.s))
184            .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a)))
185            .is_eq()
186    }
187}
188
189impl PartialOrd for Hsla {
190    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
191        // SAFETY: The total ordering relies on this always being Some()
192        Some(
193            self.h
194                .total_cmp(&other.h)
195                .then(self.s.total_cmp(&other.s))
196                .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a))),
197        )
198    }
199}
200
201impl Ord for Hsla {
202    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
203        // SAFETY: The partial comparison is a total comparison
204        unsafe { self.partial_cmp(other).unwrap_unchecked() }
205    }
206}
207
208impl Hsla {
209    pub fn to_rgb(self) -> Rgba {
210        self.into()
211    }
212
213    pub fn red() -> Self {
214        red()
215    }
216
217    pub fn green() -> Self {
218        green()
219    }
220
221    pub fn blue() -> Self {
222        blue()
223    }
224
225    pub fn black() -> Self {
226        black()
227    }
228
229    pub fn white() -> Self {
230        white()
231    }
232
233    pub fn transparent_black() -> Self {
234        transparent_black()
235    }
236}
237
238impl Eq for Hsla {}
239
240pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
241    Hsla {
242        h: h.clamp(0., 1.),
243        s: s.clamp(0., 1.),
244        l: l.clamp(0., 1.),
245        a: a.clamp(0., 1.),
246    }
247}
248
249pub fn black() -> Hsla {
250    Hsla {
251        h: 0.,
252        s: 0.,
253        l: 0.,
254        a: 1.,
255    }
256}
257
258pub fn transparent_black() -> Hsla {
259    Hsla {
260        h: 0.,
261        s: 0.,
262        l: 0.,
263        a: 0.,
264    }
265}
266
267pub fn white() -> Hsla {
268    Hsla {
269        h: 0.,
270        s: 0.,
271        l: 1.,
272        a: 1.,
273    }
274}
275
276pub fn red() -> Hsla {
277    Hsla {
278        h: 0.,
279        s: 1.,
280        l: 0.5,
281        a: 1.,
282    }
283}
284
285pub fn blue() -> Hsla {
286    Hsla {
287        h: 0.6,
288        s: 1.,
289        l: 0.5,
290        a: 1.,
291    }
292}
293
294pub fn green() -> Hsla {
295    Hsla {
296        h: 0.3,
297        s: 1.,
298        l: 0.5,
299        a: 1.,
300    }
301}
302
303impl Hsla {
304    /// Returns true if the HSLA color is fully transparent, false otherwise.
305    pub fn is_transparent(&self) -> bool {
306        self.a == 0.0
307    }
308
309    /// 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.
310    ///
311    /// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
312    /// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
313    /// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
314    ///
315    /// Assumptions:
316    /// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
317    /// - 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.
318    /// - RGB color components are contained in the range [0, 1].
319    /// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
320    pub fn blend(self, other: Hsla) -> Hsla {
321        let alpha = other.a;
322
323        if alpha >= 1.0 {
324            return other;
325        } else if alpha <= 0.0 {
326            return self;
327        } else {
328            let converted_self = Rgba::from(self);
329            let converted_other = Rgba::from(other);
330            let blended_rgb = converted_self.blend(converted_other);
331            return Hsla::from(blended_rgb);
332        }
333    }
334
335    /// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
336    /// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
337    pub fn fade_out(&mut self, factor: f32) {
338        self.a *= 1.0 - factor.clamp(0., 1.);
339    }
340}
341
342// impl From<Hsla> for Rgba {
343//     fn from(value: Hsla) -> Self {
344//         let h = value.h;
345//         let s = value.s;
346//         let l = value.l;
347
348//         let c = (1 - |2L - 1|) X s
349//     }
350// }
351
352impl From<Rgba> for Hsla {
353    fn from(color: Rgba) -> Self {
354        let r = color.r;
355        let g = color.g;
356        let b = color.b;
357
358        let max = r.max(g.max(b));
359        let min = r.min(g.min(b));
360        let delta = max - min;
361
362        let l = (max + min) / 2.0;
363        let s = if l == 0.0 || l == 1.0 {
364            0.0
365        } else if l < 0.5 {
366            delta / (2.0 * l)
367        } else {
368            delta / (2.0 - 2.0 * l)
369        };
370
371        let h = if delta == 0.0 {
372            0.0
373        } else if max == r {
374            ((g - b) / delta).rem_euclid(6.0) / 6.0
375        } else if max == g {
376            ((b - r) / delta + 2.0) / 6.0
377        } else {
378            ((r - g) / delta + 4.0) / 6.0
379        };
380
381        Hsla {
382            h,
383            s,
384            l,
385            a: color.a,
386        }
387    }
388}
389
390impl<'de> Deserialize<'de> for Hsla {
391    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
392    where
393        D: Deserializer<'de>,
394    {
395        // First, deserialize it into Rgba
396        let rgba = Rgba::deserialize(deserializer)?;
397
398        // Then, use the From<Rgba> for Hsla implementation to convert it
399        Ok(Hsla::from(rgba))
400    }
401}
402
403#[cfg(test)]
404mod tests {
405    use serde_json::json;
406
407    use super::*;
408
409    #[test]
410    fn test_deserialize_three_value_hex_to_rgba() {
411        let actual: Rgba = serde_json::from_value(json!("#f09")).unwrap();
412
413        assert_eq!(actual, rgba(0xff0099ff))
414    }
415
416    #[test]
417    fn test_deserialize_four_value_hex_to_rgba() {
418        let actual: Rgba = serde_json::from_value(json!("#f09f")).unwrap();
419
420        assert_eq!(actual, rgba(0xff0099ff))
421    }
422
423    #[test]
424    fn test_deserialize_six_value_hex_to_rgba() {
425        let actual: Rgba = serde_json::from_value(json!("#ff0099")).unwrap();
426
427        assert_eq!(actual, rgba(0xff0099ff))
428    }
429
430    #[test]
431    fn test_deserialize_eight_value_hex_to_rgba() {
432        let actual: Rgba = serde_json::from_value(json!("#ff0099ff")).unwrap();
433
434        assert_eq!(actual, rgba(0xff0099ff))
435    }
436
437    #[test]
438    fn test_deserialize_eight_value_hex_with_padding_to_rgba() {
439        let actual: Rgba = serde_json::from_value(json!(" #f5f5f5ff   ")).unwrap();
440
441        assert_eq!(actual, rgba(0xf5f5f5ff))
442    }
443
444    #[test]
445    fn test_deserialize_eight_value_hex_with_mixed_case_to_rgba() {
446        let actual: Rgba = serde_json::from_value(json!("#DeAdbEeF")).unwrap();
447
448        assert_eq!(actual, rgba(0xdeadbeef))
449    }
450}