color.rs

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