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.33,
297        s: 1.,
298        l: 0.5,
299        a: 1.,
300    }
301}
302
303pub fn yellow() -> Hsla {
304    Hsla {
305        h: 0.16,
306        s: 1.,
307        l: 0.5,
308        a: 1.,
309    }
310}
311
312impl Hsla {
313    /// Returns true if the HSLA color is fully transparent, false otherwise.
314    pub fn is_transparent(&self) -> bool {
315        self.a == 0.0
316    }
317
318    /// 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.
319    ///
320    /// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
321    /// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
322    /// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
323    ///
324    /// Assumptions:
325    /// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
326    /// - 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.
327    /// - RGB color components are contained in the range [0, 1].
328    /// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
329    pub fn blend(self, other: Hsla) -> Hsla {
330        let alpha = other.a;
331
332        if alpha >= 1.0 {
333            return other;
334        } else if alpha <= 0.0 {
335            return self;
336        } else {
337            let converted_self = Rgba::from(self);
338            let converted_other = Rgba::from(other);
339            let blended_rgb = converted_self.blend(converted_other);
340            return Hsla::from(blended_rgb);
341        }
342    }
343
344    /// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
345    /// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
346    pub fn fade_out(&mut self, factor: f32) {
347        self.a *= 1.0 - factor.clamp(0., 1.);
348    }
349}
350
351// impl From<Hsla> for Rgba {
352//     fn from(value: Hsla) -> Self {
353//         let h = value.h;
354//         let s = value.s;
355//         let l = value.l;
356
357//         let c = (1 - |2L - 1|) X s
358//     }
359// }
360
361impl From<Rgba> for Hsla {
362    fn from(color: Rgba) -> Self {
363        let r = color.r;
364        let g = color.g;
365        let b = color.b;
366
367        let max = r.max(g.max(b));
368        let min = r.min(g.min(b));
369        let delta = max - min;
370
371        let l = (max + min) / 2.0;
372        let s = if l == 0.0 || l == 1.0 {
373            0.0
374        } else if l < 0.5 {
375            delta / (2.0 * l)
376        } else {
377            delta / (2.0 - 2.0 * l)
378        };
379
380        let h = if delta == 0.0 {
381            0.0
382        } else if max == r {
383            ((g - b) / delta).rem_euclid(6.0) / 6.0
384        } else if max == g {
385            ((b - r) / delta + 2.0) / 6.0
386        } else {
387            ((r - g) / delta + 4.0) / 6.0
388        };
389
390        Hsla {
391            h,
392            s,
393            l,
394            a: color.a,
395        }
396    }
397}
398
399impl<'de> Deserialize<'de> for Hsla {
400    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
401    where
402        D: Deserializer<'de>,
403    {
404        // First, deserialize it into Rgba
405        let rgba = Rgba::deserialize(deserializer)?;
406
407        // Then, use the From<Rgba> for Hsla implementation to convert it
408        Ok(Hsla::from(rgba))
409    }
410}
411
412#[cfg(test)]
413mod tests {
414    use serde_json::json;
415
416    use super::*;
417
418    #[test]
419    fn test_deserialize_three_value_hex_to_rgba() {
420        let actual: Rgba = serde_json::from_value(json!("#f09")).unwrap();
421
422        assert_eq!(actual, rgba(0xff0099ff))
423    }
424
425    #[test]
426    fn test_deserialize_four_value_hex_to_rgba() {
427        let actual: Rgba = serde_json::from_value(json!("#f09f")).unwrap();
428
429        assert_eq!(actual, rgba(0xff0099ff))
430    }
431
432    #[test]
433    fn test_deserialize_six_value_hex_to_rgba() {
434        let actual: Rgba = serde_json::from_value(json!("#ff0099")).unwrap();
435
436        assert_eq!(actual, rgba(0xff0099ff))
437    }
438
439    #[test]
440    fn test_deserialize_eight_value_hex_to_rgba() {
441        let actual: Rgba = serde_json::from_value(json!("#ff0099ff")).unwrap();
442
443        assert_eq!(actual, rgba(0xff0099ff))
444    }
445
446    #[test]
447    fn test_deserialize_eight_value_hex_with_padding_to_rgba() {
448        let actual: Rgba = serde_json::from_value(json!(" #f5f5f5ff   ")).unwrap();
449
450        assert_eq!(actual, rgba(0xf5f5f5ff))
451    }
452
453    #[test]
454    fn test_deserialize_eight_value_hex_with_mixed_case_to_rgba() {
455        let actual: Rgba = serde_json::from_value(json!("#DeAdbEeF")).unwrap();
456
457        assert_eq!(actual, rgba(0xdeadbeef))
458    }
459}