color.rs

  1use anyhow::{Context as _, bail};
  2use schemars::{JsonSchema, json_schema};
  3use serde::{
  4    Deserialize, Deserializer, Serialize, Serializer,
  5    de::{self, Visitor},
  6};
  7use std::borrow::Cow;
  8use std::{
  9    fmt::{self, Display, Formatter},
 10    hash::{Hash, Hasher},
 11};
 12
 13/// Convert an RGB hex color code number to a color type
 14pub fn rgb(hex: u32) -> Rgba {
 15    let [_, r, g, b] = hex.to_be_bytes().map(|b| (b as f32) / 255.0);
 16    Rgba { r, g, b, a: 1.0 }
 17}
 18
 19/// Convert an RGBA hex color code number to [`Rgba`]
 20pub fn rgba(hex: u32) -> Rgba {
 21    let [r, g, b, a] = hex.to_be_bytes().map(|b| (b as f32) / 255.0);
 22    Rgba { r, g, b, a }
 23}
 24
 25/// Swap from RGBA with premultiplied alpha to BGRA
 26pub(crate) fn swap_rgba_pa_to_bgra(color: &mut [u8]) {
 27    color.swap(0, 2);
 28    if color[3] > 0 {
 29        let a = color[3] as f32 / 255.;
 30        color[0] = (color[0] as f32 / a) as u8;
 31        color[1] = (color[1] as f32 / a) as u8;
 32        color[2] = (color[2] as f32 / a) as u8;
 33    }
 34}
 35
 36/// An RGBA color
 37#[derive(PartialEq, Clone, Copy, Default)]
 38pub struct Rgba {
 39    /// The red component of the color, in the range 0.0 to 1.0
 40    pub r: f32,
 41    /// The green component of the color, in the range 0.0 to 1.0
 42    pub g: f32,
 43    /// The blue component of the color, in the range 0.0 to 1.0
 44    pub b: f32,
 45    /// The alpha component of the color, in the range 0.0 to 1.0
 46    pub a: f32,
 47}
 48
 49impl fmt::Debug for Rgba {
 50    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 51        write!(f, "rgba({:#010x})", u32::from(*self))
 52    }
 53}
 54
 55impl Rgba {
 56    /// Create a new [`Rgba`] color by blending this and another color together
 57    pub fn blend(&self, other: Rgba) -> Self {
 58        if other.a >= 1.0 {
 59            other
 60        } else if other.a <= 0.0 {
 61            *self
 62        } else {
 63            Rgba {
 64                r: (self.r * (1.0 - other.a)) + (other.r * other.a),
 65                g: (self.g * (1.0 - other.a)) + (other.g * other.a),
 66                b: (self.b * (1.0 - other.a)) + (other.b * other.a),
 67                a: self.a,
 68            }
 69        }
 70    }
 71}
 72
 73impl From<Rgba> for u32 {
 74    fn from(rgba: Rgba) -> Self {
 75        let r = (rgba.r * 255.0) as u32;
 76        let g = (rgba.g * 255.0) as u32;
 77        let b = (rgba.b * 255.0) as u32;
 78        let a = (rgba.a * 255.0) as u32;
 79        (r << 24) | (g << 16) | (b << 8) | a
 80    }
 81}
 82
 83struct RgbaVisitor;
 84
 85impl Visitor<'_> for RgbaVisitor {
 86    type Value = Rgba;
 87
 88    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
 89        formatter.write_str("a string in the format #rrggbb or #rrggbbaa")
 90    }
 91
 92    fn visit_str<E: de::Error>(self, value: &str) -> Result<Rgba, E> {
 93        Rgba::try_from(value).map_err(E::custom)
 94    }
 95}
 96
 97impl JsonSchema for Rgba {
 98    fn schema_name() -> Cow<'static, str> {
 99        "Rgba".into()
100    }
101
102    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
103        json_schema!({
104            "type": "string",
105            "pattern": "^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$"
106        })
107    }
108}
109
110impl<'de> Deserialize<'de> for Rgba {
111    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
112        deserializer.deserialize_str(RgbaVisitor)
113    }
114}
115
116impl Serialize for Rgba {
117    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
118    where
119        S: Serializer,
120    {
121        let r = (self.r * 255.0).round() as u8;
122        let g = (self.g * 255.0).round() as u8;
123        let b = (self.b * 255.0).round() as u8;
124        let a = (self.a * 255.0).round() as u8;
125
126        let s = format!("#{r:02x}{g:02x}{b:02x}{a:02x}");
127        serializer.serialize_str(&s)
128    }
129}
130
131impl From<Hsla> for Rgba {
132    fn from(color: Hsla) -> Self {
133        let h = color.h;
134        let s = color.s;
135        let l = color.l;
136
137        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
138        let x = c * (1.0 - ((h * 6.0) % 2.0 - 1.0).abs());
139        let m = l - c / 2.0;
140        let cm = c + m;
141        let xm = x + m;
142
143        let (r, g, b) = match (h * 6.0).floor() as i32 {
144            0 | 6 => (cm, xm, m),
145            1 => (xm, cm, m),
146            2 => (m, cm, xm),
147            3 => (m, xm, cm),
148            4 => (xm, m, cm),
149            _ => (cm, m, xm),
150        };
151
152        Rgba {
153            r,
154            g,
155            b,
156            a: color.a,
157        }
158    }
159}
160
161impl TryFrom<&'_ str> for Rgba {
162    type Error = anyhow::Error;
163
164    fn try_from(value: &'_ str) -> Result<Self, Self::Error> {
165        const RGB: usize = "rgb".len();
166        const RGBA: usize = "rgba".len();
167        const RRGGBB: usize = "rrggbb".len();
168        const RRGGBBAA: usize = "rrggbbaa".len();
169
170        const EXPECTED_FORMATS: &str = "Expected #rgb, #rgba, #rrggbb, or #rrggbbaa";
171        const INVALID_UNICODE: &str = "invalid unicode characters in color";
172
173        let Some(("", hex)) = value.trim().split_once('#') else {
174            bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}");
175        };
176
177        let (r, g, b, a) = match hex.len() {
178            RGB | RGBA => {
179                let r = u8::from_str_radix(
180                    hex.get(0..1).with_context(|| {
181                        format!("{INVALID_UNICODE}: r component of #rgb/#rgba for value: '{value}'")
182                    })?,
183                    16,
184                )?;
185                let g = u8::from_str_radix(
186                    hex.get(1..2).with_context(|| {
187                        format!("{INVALID_UNICODE}: g component of #rgb/#rgba for value: '{value}'")
188                    })?,
189                    16,
190                )?;
191                let b = u8::from_str_radix(
192                    hex.get(2..3).with_context(|| {
193                        format!("{INVALID_UNICODE}: b component of #rgb/#rgba for value: '{value}'")
194                    })?,
195                    16,
196                )?;
197                let a = if hex.len() == RGBA {
198                    u8::from_str_radix(
199                        hex.get(3..4).with_context(|| {
200                            format!("{INVALID_UNICODE}: a component of #rgba for value: '{value}'")
201                        })?,
202                        16,
203                    )?
204                } else {
205                    0xf
206                };
207
208                /// Duplicates a given hex digit.
209                /// E.g., `0xf` -> `0xff`.
210                const fn duplicate(value: u8) -> u8 {
211                    (value << 4) | value
212                }
213
214                (duplicate(r), duplicate(g), duplicate(b), duplicate(a))
215            }
216            RRGGBB | RRGGBBAA => {
217                let r = u8::from_str_radix(
218                    hex.get(0..2).with_context(|| {
219                        format!(
220                            "{}: r component of #rrggbb/#rrggbbaa for value: '{}'",
221                            INVALID_UNICODE, value
222                        )
223                    })?,
224                    16,
225                )?;
226                let g = u8::from_str_radix(
227                    hex.get(2..4).with_context(|| {
228                        format!(
229                            "{INVALID_UNICODE}: g component of #rrggbb/#rrggbbaa for value: '{value}'"
230                        )
231                    })?,
232                    16,
233                )?;
234                let b = u8::from_str_radix(
235                    hex.get(4..6).with_context(|| {
236                        format!(
237                            "{INVALID_UNICODE}: b component of #rrggbb/#rrggbbaa for value: '{value}'"
238                        )
239                    })?,
240                    16,
241                )?;
242                let a = if hex.len() == RRGGBBAA {
243                    u8::from_str_radix(
244                        hex.get(6..8).with_context(|| {
245                            format!(
246                                "{INVALID_UNICODE}: a component of #rrggbbaa for value: '{value}'"
247                            )
248                        })?,
249                        16,
250                    )?
251                } else {
252                    0xff
253                };
254                (r, g, b, a)
255            }
256            _ => bail!("invalid RGBA hex color: '{value}'. {EXPECTED_FORMATS}"),
257        };
258
259        Ok(Rgba {
260            r: r as f32 / 255.,
261            g: g as f32 / 255.,
262            b: b as f32 / 255.,
263            a: a as f32 / 255.,
264        })
265    }
266}
267
268/// An HSLA color
269#[derive(Default, Copy, Clone, Debug)]
270#[repr(C)]
271pub struct Hsla {
272    /// Hue, in a range from 0 to 1
273    pub h: f32,
274
275    /// Saturation, in a range from 0 to 1
276    pub s: f32,
277
278    /// Lightness, in a range from 0 to 1
279    pub l: f32,
280
281    /// Alpha, in a range from 0 to 1
282    pub a: f32,
283}
284
285impl PartialEq for Hsla {
286    fn eq(&self, other: &Self) -> bool {
287        self.h
288            .total_cmp(&other.h)
289            .then(self.s.total_cmp(&other.s))
290            .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a)))
291            .is_eq()
292    }
293}
294
295impl PartialOrd for Hsla {
296    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
297        Some(self.cmp(other))
298    }
299}
300
301impl Ord for Hsla {
302    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
303        self.h
304            .total_cmp(&other.h)
305            .then(self.s.total_cmp(&other.s))
306            .then(self.l.total_cmp(&other.l).then(self.a.total_cmp(&other.a)))
307    }
308}
309
310impl Eq for Hsla {}
311
312impl Hash for Hsla {
313    fn hash<H: Hasher>(&self, state: &mut H) {
314        state.write_u32(u32::from_be_bytes(self.h.to_be_bytes()));
315        state.write_u32(u32::from_be_bytes(self.s.to_be_bytes()));
316        state.write_u32(u32::from_be_bytes(self.l.to_be_bytes()));
317        state.write_u32(u32::from_be_bytes(self.a.to_be_bytes()));
318    }
319}
320
321impl Display for Hsla {
322    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
323        write!(
324            f,
325            "hsla({:.2}, {:.2}%, {:.2}%, {:.2})",
326            self.h * 360.,
327            self.s * 100.,
328            self.l * 100.,
329            self.a
330        )
331    }
332}
333
334/// Construct an [`Hsla`] object from plain values
335pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Hsla {
336    Hsla {
337        h: h.clamp(0., 1.),
338        s: s.clamp(0., 1.),
339        l: l.clamp(0., 1.),
340        a: a.clamp(0., 1.),
341    }
342}
343
344/// Pure black in [`Hsla`]
345pub const fn black() -> Hsla {
346    Hsla {
347        h: 0.,
348        s: 0.,
349        l: 0.,
350        a: 1.,
351    }
352}
353
354/// Transparent black in [`Hsla`]
355pub const fn transparent_black() -> Hsla {
356    Hsla {
357        h: 0.,
358        s: 0.,
359        l: 0.,
360        a: 0.,
361    }
362}
363
364/// Transparent black in [`Hsla`]
365pub const fn transparent_white() -> Hsla {
366    Hsla {
367        h: 0.,
368        s: 0.,
369        l: 1.,
370        a: 0.,
371    }
372}
373
374/// Opaque grey in [`Hsla`], values will be clamped to the range [0, 1]
375pub fn opaque_grey(lightness: f32, opacity: f32) -> Hsla {
376    Hsla {
377        h: 0.,
378        s: 0.,
379        l: lightness.clamp(0., 1.),
380        a: opacity.clamp(0., 1.),
381    }
382}
383
384/// Pure white in [`Hsla`]
385pub const fn white() -> Hsla {
386    Hsla {
387        h: 0.,
388        s: 0.,
389        l: 1.,
390        a: 1.,
391    }
392}
393
394/// The color red in [`Hsla`]
395pub const fn red() -> Hsla {
396    Hsla {
397        h: 0.,
398        s: 1.,
399        l: 0.5,
400        a: 1.,
401    }
402}
403
404/// The color blue in [`Hsla`]
405pub const fn blue() -> Hsla {
406    Hsla {
407        h: 0.6666666667,
408        s: 1.,
409        l: 0.5,
410        a: 1.,
411    }
412}
413
414/// The color green in [`Hsla`]
415pub const fn green() -> Hsla {
416    Hsla {
417        h: 0.3333333333,
418        s: 1.,
419        l: 0.25,
420        a: 1.,
421    }
422}
423
424/// The color yellow in [`Hsla`]
425pub const fn yellow() -> Hsla {
426    Hsla {
427        h: 0.1666666667,
428        s: 1.,
429        l: 0.5,
430        a: 1.,
431    }
432}
433
434impl Hsla {
435    /// Converts this HSLA color to an RGBA color.
436    pub fn to_rgb(self) -> Rgba {
437        self.into()
438    }
439
440    /// The color red
441    pub const fn red() -> Self {
442        red()
443    }
444
445    /// The color green
446    pub const fn green() -> Self {
447        green()
448    }
449
450    /// The color blue
451    pub const fn blue() -> Self {
452        blue()
453    }
454
455    /// The color black
456    pub const fn black() -> Self {
457        black()
458    }
459
460    /// The color white
461    pub const fn white() -> Self {
462        white()
463    }
464
465    /// The color transparent black
466    pub const fn transparent_black() -> Self {
467        transparent_black()
468    }
469
470    /// Returns true if the HSLA color is fully transparent, false otherwise.
471    pub fn is_transparent(&self) -> bool {
472        self.a == 0.0
473    }
474
475    /// 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.
476    ///
477    /// If `other`'s alpha value is 1.0 or greater, `other` color is fully opaque, thus `other` is returned as the output color.
478    /// If `other`'s alpha value is 0.0 or less, `other` color is fully transparent, thus `self` is returned as the output color.
479    /// Else, the output color is calculated as a blend of `self` and `other` based on their weighted alpha values.
480    ///
481    /// Assumptions:
482    /// - Alpha values are contained in the range [0, 1], with 1 as fully opaque and 0 as fully transparent.
483    /// - 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.
484    /// - RGB color components are contained in the range [0, 1].
485    /// - If `self` and `other` colors are out of the valid range, the blend operation's output and behavior is undefined.
486    pub fn blend(self, other: Hsla) -> Hsla {
487        let alpha = other.a;
488
489        if alpha >= 1.0 {
490            other
491        } else if alpha <= 0.0 {
492            self
493        } else {
494            let converted_self = Rgba::from(self);
495            let converted_other = Rgba::from(other);
496            let blended_rgb = converted_self.blend(converted_other);
497            Hsla::from(blended_rgb)
498        }
499    }
500
501    /// Returns a new HSLA color with the same hue, and lightness, but with no saturation.
502    pub fn grayscale(&self) -> Self {
503        Hsla {
504            h: self.h,
505            s: 0.,
506            l: self.l,
507            a: self.a,
508        }
509    }
510
511    /// Fade out the color by a given factor. This factor should be between 0.0 and 1.0.
512    /// Where 0.0 will leave the color unchanged, and 1.0 will completely fade out the color.
513    pub fn fade_out(&mut self, factor: f32) {
514        self.a *= 1.0 - factor.clamp(0., 1.);
515    }
516
517    /// Multiplies the alpha value of the color by a given factor
518    /// and returns a new HSLA color.
519    ///
520    /// Useful for transforming colors with dynamic opacity,
521    /// like a color from an external source.
522    ///
523    /// Example:
524    /// ```
525    /// let color = gpui::red();
526    /// let faded_color = color.opacity(0.5);
527    /// assert_eq!(faded_color.a, 0.5);
528    /// ```
529    ///
530    /// This will return a red color with half the opacity.
531    ///
532    /// Example:
533    /// ```
534    /// let color = hlsa(0.7, 1.0, 0.5, 0.7); // A saturated blue
535    /// let faded_color = color.opacity(0.16);
536    /// assert_eq!(faded_color.a, 0.112);
537    /// ```
538    ///
539    /// This will return a blue color with around ~10% opacity,
540    /// suitable for an element's hover or selected state.
541    ///
542    pub fn opacity(&self, factor: f32) -> Self {
543        Hsla {
544            h: self.h,
545            s: self.s,
546            l: self.l,
547            a: self.a * factor.clamp(0., 1.),
548        }
549    }
550
551    /// Returns a new HSLA color with the same hue, saturation,
552    /// and lightness, but with a new alpha value.
553    ///
554    /// Example:
555    /// ```
556    /// let color = gpui::red();
557    /// let red_color = color.alpha(0.25);
558    /// assert_eq!(red_color.a, 0.25);
559    /// ```
560    ///
561    /// This will return a red color with half the opacity.
562    ///
563    /// Example:
564    /// ```
565    /// let color = hsla(0.7, 1.0, 0.5, 0.7); // A saturated blue
566    /// let faded_color = color.alpha(0.25);
567    /// assert_eq!(faded_color.a, 0.25);
568    /// ```
569    ///
570    /// This will return a blue color with 25% opacity.
571    pub fn alpha(&self, a: f32) -> Self {
572        Hsla {
573            h: self.h,
574            s: self.s,
575            l: self.l,
576            a: a.clamp(0., 1.),
577        }
578    }
579}
580
581impl From<Rgba> for Hsla {
582    fn from(color: Rgba) -> Self {
583        let r = color.r;
584        let g = color.g;
585        let b = color.b;
586
587        let max = r.max(g.max(b));
588        let min = r.min(g.min(b));
589        let delta = max - min;
590
591        let l = (max + min) / 2.0;
592        let s = if l == 0.0 || l == 1.0 {
593            0.0
594        } else if l < 0.5 {
595            delta / (2.0 * l)
596        } else {
597            delta / (2.0 - 2.0 * l)
598        };
599
600        let h = if delta == 0.0 {
601            0.0
602        } else if max == r {
603            ((g - b) / delta).rem_euclid(6.0) / 6.0
604        } else if max == g {
605            ((b - r) / delta + 2.0) / 6.0
606        } else {
607            ((r - g) / delta + 4.0) / 6.0
608        };
609
610        Hsla {
611            h,
612            s,
613            l,
614            a: color.a,
615        }
616    }
617}
618
619impl JsonSchema for Hsla {
620    fn schema_name() -> Cow<'static, str> {
621        Rgba::schema_name()
622    }
623
624    fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
625        Rgba::json_schema(generator)
626    }
627}
628
629impl Serialize for Hsla {
630    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
631    where
632        S: Serializer,
633    {
634        Rgba::from(*self).serialize(serializer)
635    }
636}
637
638impl<'de> Deserialize<'de> for Hsla {
639    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
640    where
641        D: Deserializer<'de>,
642    {
643        Ok(Rgba::deserialize(deserializer)?.into())
644    }
645}
646
647#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
648#[repr(C)]
649pub(crate) enum BackgroundTag {
650    Solid = 0,
651    LinearGradient = 1,
652    PatternSlash = 2,
653}
654
655/// A color space for color interpolation.
656///
657/// References:
658/// - <https://developer.mozilla.org/en-US/docs/Web/CSS/color-interpolation-method>
659/// - <https://www.w3.org/TR/css-color-4/#typedef-color-space>
660#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize, JsonSchema)]
661#[repr(C)]
662pub enum ColorSpace {
663    #[default]
664    /// The sRGB color space.
665    Srgb = 0,
666    /// The Oklab color space.
667    Oklab = 1,
668}
669
670impl Display for ColorSpace {
671    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
672        match self {
673            ColorSpace::Srgb => write!(f, "sRGB"),
674            ColorSpace::Oklab => write!(f, "Oklab"),
675        }
676    }
677}
678
679/// A background color, which can be either a solid color or a linear gradient.
680#[derive(Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
681#[repr(C)]
682pub struct Background {
683    pub(crate) tag: BackgroundTag,
684    pub(crate) color_space: ColorSpace,
685    pub(crate) solid: Hsla,
686    pub(crate) gradient_angle_or_pattern_height: f32,
687    pub(crate) colors: [LinearColorStop; 2],
688    /// Padding for alignment for repr(C) layout.
689    pad: u32,
690}
691
692impl std::fmt::Debug for Background {
693    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
694        match self.tag {
695            BackgroundTag::Solid => write!(f, "Solid({:?})", self.solid),
696            BackgroundTag::LinearGradient => {
697                write!(
698                    f,
699                    "LinearGradient({}, {:?}, {:?})",
700                    self.gradient_angle_or_pattern_height, self.colors[0], self.colors[1]
701                )
702            }
703            BackgroundTag::PatternSlash => {
704                write!(
705                    f,
706                    "PatternSlash({:?}, {})",
707                    self.solid, self.gradient_angle_or_pattern_height
708                )
709            }
710        }
711    }
712}
713
714impl Eq for Background {}
715impl Default for Background {
716    fn default() -> Self {
717        Self {
718            tag: BackgroundTag::Solid,
719            solid: Hsla::default(),
720            color_space: ColorSpace::default(),
721            gradient_angle_or_pattern_height: 0.0,
722            colors: [LinearColorStop::default(), LinearColorStop::default()],
723            pad: 0,
724        }
725    }
726}
727
728/// Creates a hash pattern background
729pub fn pattern_slash(color: Hsla, width: f32, interval: f32) -> Background {
730    let width_scaled = (width * 255.0) as u32;
731    let interval_scaled = (interval * 255.0) as u32;
732    let height = ((width_scaled * 0xFFFF) + interval_scaled) as f32;
733
734    Background {
735        tag: BackgroundTag::PatternSlash,
736        solid: color,
737        gradient_angle_or_pattern_height: height,
738        ..Default::default()
739    }
740}
741
742/// Creates a solid background color.
743pub fn solid_background(color: impl Into<Hsla>) -> Background {
744    Background {
745        solid: color.into(),
746        ..Default::default()
747    }
748}
749
750/// Creates a LinearGradient background color.
751///
752/// The gradient line's angle of direction. A value of `0.` is equivalent to top; increasing values rotate clockwise from there.
753///
754/// The `angle` is in degrees value in the range 0.0 to 360.0.
755///
756/// <https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient>
757pub fn linear_gradient(
758    angle: f32,
759    from: impl Into<LinearColorStop>,
760    to: impl Into<LinearColorStop>,
761) -> Background {
762    Background {
763        tag: BackgroundTag::LinearGradient,
764        gradient_angle_or_pattern_height: angle,
765        colors: [from.into(), to.into()],
766        ..Default::default()
767    }
768}
769
770/// A color stop in a linear gradient.
771///
772/// <https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient#linear-color-stop>
773#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
774#[repr(C)]
775pub struct LinearColorStop {
776    /// The color of the color stop.
777    pub color: Hsla,
778    /// The percentage of the gradient, in the range 0.0 to 1.0.
779    pub percentage: f32,
780}
781
782/// Creates a new linear color stop.
783///
784/// The percentage of the gradient, in the range 0.0 to 1.0.
785pub fn linear_color_stop(color: impl Into<Hsla>, percentage: f32) -> LinearColorStop {
786    LinearColorStop {
787        color: color.into(),
788        percentage,
789    }
790}
791
792impl LinearColorStop {
793    /// Returns a new color stop with the same color, but with a modified alpha value.
794    pub fn opacity(&self, factor: f32) -> Self {
795        Self {
796            percentage: self.percentage,
797            color: self.color.opacity(factor),
798        }
799    }
800}
801
802impl Background {
803    /// Use specified color space for color interpolation.
804    ///
805    /// <https://developer.mozilla.org/en-US/docs/Web/CSS/color-interpolation-method>
806    pub fn color_space(mut self, color_space: ColorSpace) -> Self {
807        self.color_space = color_space;
808        self
809    }
810
811    /// Returns a new background color with the same hue, saturation, and lightness, but with a modified alpha value.
812    pub fn opacity(&self, factor: f32) -> Self {
813        let mut background = *self;
814        background.solid = background.solid.opacity(factor);
815        background.colors = [
816            self.colors[0].opacity(factor),
817            self.colors[1].opacity(factor),
818        ];
819        background
820    }
821
822    /// Returns whether the background color is transparent.
823    pub fn is_transparent(&self) -> bool {
824        match self.tag {
825            BackgroundTag::Solid => self.solid.is_transparent(),
826            BackgroundTag::LinearGradient => self.colors.iter().all(|c| c.color.is_transparent()),
827            BackgroundTag::PatternSlash => self.solid.is_transparent(),
828        }
829    }
830}
831
832impl From<Hsla> for Background {
833    fn from(value: Hsla) -> Self {
834        Background {
835            tag: BackgroundTag::Solid,
836            solid: value,
837            ..Default::default()
838        }
839    }
840}
841impl From<Rgba> for Background {
842    fn from(value: Rgba) -> Self {
843        Background {
844            tag: BackgroundTag::Solid,
845            solid: Hsla::from(value),
846            ..Default::default()
847        }
848    }
849}
850
851#[cfg(test)]
852mod tests {
853    use serde_json::json;
854
855    use super::*;
856
857    #[test]
858    fn test_deserialize_three_value_hex_to_rgba() {
859        let actual: Rgba = serde_json::from_value(json!("#f09")).unwrap();
860
861        assert_eq!(actual, rgba(0xff0099ff))
862    }
863
864    #[test]
865    fn test_deserialize_four_value_hex_to_rgba() {
866        let actual: Rgba = serde_json::from_value(json!("#f09f")).unwrap();
867
868        assert_eq!(actual, rgba(0xff0099ff))
869    }
870
871    #[test]
872    fn test_deserialize_six_value_hex_to_rgba() {
873        let actual: Rgba = serde_json::from_value(json!("#ff0099")).unwrap();
874
875        assert_eq!(actual, rgba(0xff0099ff))
876    }
877
878    #[test]
879    fn test_deserialize_eight_value_hex_to_rgba() {
880        let actual: Rgba = serde_json::from_value(json!("#ff0099ff")).unwrap();
881
882        assert_eq!(actual, rgba(0xff0099ff))
883    }
884
885    #[test]
886    fn test_deserialize_eight_value_hex_with_padding_to_rgba() {
887        let actual: Rgba = serde_json::from_value(json!(" #f5f5f5ff   ")).unwrap();
888
889        assert_eq!(actual, rgba(0xf5f5f5ff))
890    }
891
892    #[test]
893    fn test_deserialize_eight_value_hex_with_mixed_case_to_rgba() {
894        let actual: Rgba = serde_json::from_value(json!("#DeAdbEeF")).unwrap();
895
896        assert_eq!(actual, rgba(0xdeadbeef))
897    }
898
899    #[test]
900    fn test_background_solid() {
901        let color = Hsla::from(rgba(0xff0099ff));
902        let mut background = Background::from(color);
903        assert_eq!(background.tag, BackgroundTag::Solid);
904        assert_eq!(background.solid, color);
905
906        assert_eq!(background.opacity(0.5).solid, color.opacity(0.5));
907        assert_eq!(background.is_transparent(), false);
908        background.solid = hsla(0.0, 0.0, 0.0, 0.0);
909        assert_eq!(background.is_transparent(), true);
910    }
911
912    #[test]
913    fn test_background_linear_gradient() {
914        let from = linear_color_stop(rgba(0xff0099ff), 0.0);
915        let to = linear_color_stop(rgba(0x00ff99ff), 1.0);
916        let background = linear_gradient(90.0, from, to);
917        assert_eq!(background.tag, BackgroundTag::LinearGradient);
918        assert_eq!(background.colors[0], from);
919        assert_eq!(background.colors[1], to);
920
921        assert_eq!(background.opacity(0.5).colors[0], from.opacity(0.5));
922        assert_eq!(background.opacity(0.5).colors[1], to.opacity(0.5));
923        assert_eq!(background.is_transparent(), false);
924        assert_eq!(background.opacity(0.0).is_transparent(), true);
925    }
926}