style/color/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Color support functions.
6
7/// cbindgen:ignore
8pub mod convert;
9
10mod color_function;
11pub mod component;
12pub mod mix;
13pub mod parsing;
14mod to_css;
15
16use self::parsing::ChannelKeyword;
17use crate::derives::*;
18pub use color_function::*;
19use component::ColorComponent;
20use cssparser::color::PredefinedColorSpace;
21
22/// Number of color-mix items to reserve on the stack to avoid heap allocations.
23pub const PRE_ALLOCATED_COLOR_MIX_ITEMS: usize = 3;
24
25/// Conveniece type to use for collecting color mix items.
26pub type ColorMixItemList<T> = smallvec::SmallVec<[T; PRE_ALLOCATED_COLOR_MIX_ITEMS]>;
27
28/// The 3 components that make up a color.  (Does not include the alpha component)
29#[derive(Copy, Clone, Debug, MallocSizeOf, PartialEq, ToShmem)]
30#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
31#[repr(C)]
32pub struct ColorComponents(pub f32, pub f32, pub f32);
33
34impl ColorComponents {
35    /// Apply a function to each of the 3 components of the color.
36    #[must_use]
37    pub fn map(self, f: impl Fn(f32) -> f32) -> Self {
38        Self(f(self.0), f(self.1), f(self.2))
39    }
40}
41
42impl std::ops::Mul for ColorComponents {
43    type Output = Self;
44
45    fn mul(self, rhs: Self) -> Self::Output {
46        Self(self.0 * rhs.0, self.1 * rhs.1, self.2 * rhs.2)
47    }
48}
49
50impl std::ops::Div for ColorComponents {
51    type Output = Self;
52
53    fn div(self, rhs: Self) -> Self::Output {
54        Self(self.0 / rhs.0, self.1 / rhs.1, self.2 / rhs.2)
55    }
56}
57
58/// A color space representation in the CSS specification.
59///
60/// https://drafts.csswg.org/css-color-4/#typedef-color-space
61#[derive(
62    Clone,
63    Copy,
64    Debug,
65    Eq,
66    MallocSizeOf,
67    Parse,
68    PartialEq,
69    ToAnimatedValue,
70    ToComputedValue,
71    ToCss,
72    ToResolvedValue,
73    ToShmem,
74)]
75#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
76#[repr(u8)]
77pub enum ColorSpace {
78    /// A color specified in the sRGB color space with either the rgb/rgba(..)
79    /// functions or the newer color(srgb ..) function. If the color(..)
80    /// function is used, the AS_COLOR_FUNCTION flag will be set. Examples:
81    /// "color(srgb 0.691 0.139 0.259)", "rgb(176, 35, 66)"
82    Srgb = 0,
83    /// A color specified in the Hsl notation in the sRGB color space, e.g.
84    /// "hsl(289.18 93.136% 65.531%)"
85    /// https://drafts.csswg.org/css-color-4/#the-hsl-notation
86    Hsl,
87    /// A color specified in the Hwb notation in the sRGB color space, e.g.
88    /// "hwb(740deg 20% 30%)"
89    /// https://drafts.csswg.org/css-color-4/#the-hwb-notation
90    Hwb,
91    /// A color specified in the Lab color format, e.g.
92    /// "lab(29.2345% 39.3825 20.0664)".
93    /// https://w3c.github.io/csswg-drafts/css-color-4/#lab-colors
94    Lab,
95    /// A color specified in the Lch color format, e.g.
96    /// "lch(29.2345% 44.2 27)".
97    /// https://w3c.github.io/csswg-drafts/css-color-4/#lch-colors
98    Lch,
99    /// A color specified in the Oklab color format, e.g.
100    /// "oklab(40.101% 0.1147 0.0453)".
101    /// https://w3c.github.io/csswg-drafts/css-color-4/#lab-colors
102    Oklab,
103    /// A color specified in the Oklch color format, e.g.
104    /// "oklch(40.101% 0.12332 21.555)".
105    /// https://w3c.github.io/csswg-drafts/css-color-4/#lch-colors
106    Oklch,
107    /// A color specified with the color(..) function and the "srgb-linear"
108    /// color space, e.g. "color(srgb-linear 0.435 0.017 0.055)".
109    SrgbLinear,
110    /// A color specified with the color(..) function and the "display-p3"
111    /// color space, e.g. "color(display-p3 0.84 0.19 0.72)".
112    DisplayP3,
113    /// A color specified with the color(..) function and the "display-p3-linear"
114    /// color space.
115    DisplayP3Linear,
116    /// A color specified with the color(..) function and the "a98-rgb" color
117    /// space, e.g. "color(a98-rgb 0.44091 0.49971 0.37408)".
118    A98Rgb,
119    /// A color specified with the color(..) function and the "prophoto-rgb"
120    /// color space, e.g. "color(prophoto-rgb 0.36589 0.41717 0.31333)".
121    ProphotoRgb,
122    /// A color specified with the color(..) function and the "rec2020" color
123    /// space, e.g. "color(rec2020 0.42210 0.47580 0.35605)".
124    Rec2020,
125    /// A color specified with the color(..) function and the "xyz-d50" color
126    /// space, e.g. "color(xyz-d50 0.2005 0.14089 0.4472)".
127    XyzD50,
128    /// A color specified with the color(..) function and the "xyz-d65" or "xyz"
129    /// color space, e.g. "color(xyz-d65 0.21661 0.14602 0.59452)".
130    /// NOTE: https://drafts.csswg.org/css-color-4/#resolving-color-function-values
131    ///       specifies that `xyz` is an alias for the `xyz-d65` color space.
132    #[parse(aliases = "xyz")]
133    XyzD65,
134}
135
136impl ColorSpace {
137    /// Returns whether this is a `<rectangular-color-space>`.
138    #[inline]
139    pub fn is_rectangular(&self) -> bool {
140        !self.is_polar()
141    }
142
143    /// Returns whether this is a `<polar-color-space>`.
144    #[inline]
145    pub fn is_polar(&self) -> bool {
146        matches!(self, Self::Hsl | Self::Hwb | Self::Lch | Self::Oklch)
147    }
148
149    /// Returns true if the color has RGB or XYZ components.
150    #[inline]
151    pub fn is_rgb_or_xyz_like(&self) -> bool {
152        match self {
153            Self::Srgb
154            | Self::SrgbLinear
155            | Self::DisplayP3
156            | Self::DisplayP3Linear
157            | Self::A98Rgb
158            | Self::ProphotoRgb
159            | Self::Rec2020
160            | Self::XyzD50
161            | Self::XyzD65 => true,
162            _ => false,
163        }
164    }
165
166    /// Returns an index of the hue component in the color space, otherwise
167    /// `None`.
168    #[inline]
169    pub fn hue_index(&self) -> Option<usize> {
170        match self {
171            Self::Hsl | Self::Hwb => Some(0),
172            Self::Lch | Self::Oklch => Some(2),
173
174            _ => {
175                debug_assert!(!self.is_polar());
176                None
177            },
178        }
179    }
180}
181
182/// Flags used when serializing colors.
183#[derive(Clone, Copy, Debug, Default, MallocSizeOf, PartialEq, ToShmem)]
184#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
185#[repr(C)]
186pub struct ColorFlags(u8);
187bitflags! {
188    impl ColorFlags : u8 {
189        /// Whether the 1st color component is `none`.
190        const C0_IS_NONE = 1 << 0;
191        /// Whether the 2nd color component is `none`.
192        const C1_IS_NONE = 1 << 1;
193        /// Whether the 3rd color component is `none`.
194        const C2_IS_NONE = 1 << 2;
195        /// Whether the alpha component is `none`.
196        const ALPHA_IS_NONE = 1 << 3;
197        /// Marks that this color is in the legacy color format. This flag is
198        /// only valid for the `Srgb` color space.
199        const IS_LEGACY_SRGB = 1 << 4;
200    }
201}
202
203/// An absolutely specified color, using either rgb(), rgba(), lab(), lch(),
204/// oklab(), oklch() or color().
205#[derive(Copy, Clone, Debug, MallocSizeOf, ToShmem, ToTyped)]
206#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
207#[repr(C)]
208pub struct AbsoluteColor {
209    /// The 3 components that make up colors in any color space.
210    pub components: ColorComponents,
211    /// The alpha component of the color.
212    pub alpha: f32,
213    /// The current color space that the components represent.
214    pub color_space: ColorSpace,
215    /// Extra flags used durring serialization of this color.
216    pub flags: ColorFlags,
217}
218
219impl PartialEq for AbsoluteColor {
220    // Convert both colors to Oklab for comparison; allow EPSILON difference
221    // in component values.
222    // TODO: update this once https://github.com/w3c/csswg-drafts/issues/13157
223    // is resolved to specify a precise comparison algorithm.
224    fn eq(&self, other: &Self) -> bool {
225        const EPSILON: f32 = 0.0001;
226        let a = self.to_color_space(ColorSpace::Oklab);
227        let b = other.to_color_space(ColorSpace::Oklab);
228        (a.components.0 - b.components.0).abs() <= EPSILON
229            && (a.components.1 - b.components.1).abs() <= EPSILON
230            && (a.components.2 - b.components.2).abs() <= EPSILON
231            && (a.alpha - b.alpha).abs() <= EPSILON
232    }
233}
234
235/// Given an [`AbsoluteColor`], return the 4 float components as the type given,
236/// e.g.:
237///
238/// ```rust
239/// let srgb = AbsoluteColor::new(ColorSpace::Srgb, 1.0, 0.0, 0.0, 0.0);
240/// let floats = color_components_as!(&srgb, [f32; 4]); // [1.0, 0.0, 0.0, 0.0]
241/// ```
242macro_rules! color_components_as {
243    ($c:expr, $t:ty) => {{
244        // This macro is not an inline function, because we can't use the
245        // generic  type ($t) in a constant expression as per:
246        // https://github.com/rust-lang/rust/issues/76560
247        const_assert_eq!(std::mem::size_of::<$t>(), std::mem::size_of::<[f32; 4]>());
248        const_assert_eq!(std::mem::align_of::<$t>(), std::mem::align_of::<[f32; 4]>());
249        const_assert!(std::mem::size_of::<AbsoluteColor>() >= std::mem::size_of::<$t>());
250        const_assert_eq!(
251            std::mem::align_of::<AbsoluteColor>(),
252            std::mem::align_of::<$t>()
253        );
254
255        std::mem::transmute::<&ColorComponents, &$t>(&$c.components)
256    }};
257}
258
259/// Holds details about each component passed into creating a new [`AbsoluteColor`].
260pub struct ComponentDetails {
261    value: f32,
262    is_none: bool,
263}
264
265impl From<f32> for ComponentDetails {
266    fn from(value: f32) -> Self {
267        Self {
268            value,
269            is_none: false,
270        }
271    }
272}
273
274impl From<u8> for ComponentDetails {
275    fn from(value: u8) -> Self {
276        Self {
277            value: value as f32 / 255.0,
278            is_none: false,
279        }
280    }
281}
282
283impl From<Option<f32>> for ComponentDetails {
284    fn from(value: Option<f32>) -> Self {
285        if let Some(value) = value {
286            Self {
287                value,
288                is_none: false,
289            }
290        } else {
291            Self {
292                value: 0.0,
293                is_none: true,
294            }
295        }
296    }
297}
298
299impl From<ColorComponent<f32>> for ComponentDetails {
300    fn from(value: ColorComponent<f32>) -> Self {
301        if let ColorComponent::Value(value) = value {
302            Self {
303                value,
304                is_none: false,
305            }
306        } else {
307            Self {
308                value: 0.0,
309                is_none: true,
310            }
311        }
312    }
313}
314
315impl AbsoluteColor {
316    /// A fully transparent color in the legacy syntax.
317    pub const TRANSPARENT_BLACK: Self = Self {
318        components: ColorComponents(0.0, 0.0, 0.0),
319        alpha: 0.0,
320        color_space: ColorSpace::Srgb,
321        flags: ColorFlags::IS_LEGACY_SRGB,
322    };
323
324    /// An opaque black color in the legacy syntax.
325    pub const BLACK: Self = Self {
326        components: ColorComponents(0.0, 0.0, 0.0),
327        alpha: 1.0,
328        color_space: ColorSpace::Srgb,
329        flags: ColorFlags::IS_LEGACY_SRGB,
330    };
331
332    /// An opaque white color in the legacy syntax.
333    pub const WHITE: Self = Self {
334        components: ColorComponents(1.0, 1.0, 1.0),
335        alpha: 1.0,
336        color_space: ColorSpace::Srgb,
337        flags: ColorFlags::IS_LEGACY_SRGB,
338    };
339
340    /// Create a new [`AbsoluteColor`] with the given [`ColorSpace`] and
341    /// components.
342    pub fn new(
343        color_space: ColorSpace,
344        c1: impl Into<ComponentDetails>,
345        c2: impl Into<ComponentDetails>,
346        c3: impl Into<ComponentDetails>,
347        alpha: impl Into<ComponentDetails>,
348    ) -> Self {
349        let mut flags = ColorFlags::empty();
350
351        macro_rules! cd {
352            ($c:expr,$flag:expr) => {{
353                let component_details = $c.into();
354                if component_details.is_none {
355                    flags |= $flag;
356                }
357                component_details.value
358            }};
359        }
360
361        let mut components = ColorComponents(
362            cd!(c1, ColorFlags::C0_IS_NONE),
363            cd!(c2, ColorFlags::C1_IS_NONE),
364            cd!(c3, ColorFlags::C2_IS_NONE),
365        );
366
367        let alpha = cd!(alpha, ColorFlags::ALPHA_IS_NONE);
368
369        // Lightness for Lab and Lch is clamped to [0..100].
370        if matches!(color_space, ColorSpace::Lab | ColorSpace::Lch) {
371            components.0 = components.0.clamp(0.0, 100.0);
372        }
373
374        // Lightness for Oklab and Oklch is clamped to [0..1].
375        if matches!(color_space, ColorSpace::Oklab | ColorSpace::Oklch) {
376            components.0 = components.0.clamp(0.0, 1.0);
377        }
378
379        // Chroma must not be less than 0.
380        if matches!(color_space, ColorSpace::Lch | ColorSpace::Oklch) {
381            components.1 = components.1.max(0.0);
382        }
383
384        // Alpha is always clamped to [0..1].
385        let alpha = alpha.clamp(0.0, 1.0);
386
387        Self {
388            components,
389            alpha,
390            color_space,
391            flags,
392        }
393    }
394
395    /// Convert this color into the sRGB color space and set it to the legacy
396    /// syntax.
397    #[inline]
398    #[must_use]
399    pub fn into_srgb_legacy(self) -> Self {
400        let mut result = if !matches!(self.color_space, ColorSpace::Srgb) {
401            self.to_color_space(ColorSpace::Srgb)
402        } else {
403            self
404        };
405
406        // Explicitly set the flags to IS_LEGACY_SRGB only to clear out the
407        // *_IS_NONE flags, because the legacy syntax doesn't allow "none".
408        result.flags = ColorFlags::IS_LEGACY_SRGB;
409
410        result
411    }
412
413    /// Create a new [`AbsoluteColor`] from rgba legacy syntax values in the sRGB color space.
414    pub fn srgb_legacy(red: u8, green: u8, blue: u8, alpha: f32) -> Self {
415        let mut result = Self::new(ColorSpace::Srgb, red, green, blue, alpha);
416        result.flags = ColorFlags::IS_LEGACY_SRGB;
417        result
418    }
419
420    /// Return all the components of the color in an array.  (Includes alpha)
421    #[inline]
422    pub fn raw_components(&self) -> &[f32; 4] {
423        unsafe { color_components_as!(self, [f32; 4]) }
424    }
425
426    /// Returns true if this color is in the legacy color syntax.
427    #[inline]
428    pub fn is_legacy_syntax(&self) -> bool {
429        // rgb(), rgba(), hsl(), hsla(), hwb(), hwba()
430        match self.color_space {
431            ColorSpace::Srgb => self.flags.contains(ColorFlags::IS_LEGACY_SRGB),
432            ColorSpace::Hsl | ColorSpace::Hwb => true,
433            _ => false,
434        }
435    }
436
437    /// Returns true if this color is fully transparent.
438    #[inline]
439    pub fn is_transparent(&self) -> bool {
440        self.flags.contains(ColorFlags::ALPHA_IS_NONE) || self.alpha == 0.0
441    }
442
443    /// Return an optional first component.
444    #[inline]
445    pub fn c0(&self) -> Option<f32> {
446        if self.flags.contains(ColorFlags::C0_IS_NONE) {
447            None
448        } else {
449            Some(self.components.0)
450        }
451    }
452
453    /// Return an optional second component.
454    #[inline]
455    pub fn c1(&self) -> Option<f32> {
456        if self.flags.contains(ColorFlags::C1_IS_NONE) {
457            None
458        } else {
459            Some(self.components.1)
460        }
461    }
462
463    /// Return an optional second component.
464    #[inline]
465    pub fn c2(&self) -> Option<f32> {
466        if self.flags.contains(ColorFlags::C2_IS_NONE) {
467            None
468        } else {
469            Some(self.components.2)
470        }
471    }
472
473    /// Return an optional alpha component.
474    #[inline]
475    pub fn alpha(&self) -> Option<f32> {
476        if self.flags.contains(ColorFlags::ALPHA_IS_NONE) {
477            None
478        } else {
479            Some(self.alpha)
480        }
481    }
482
483    /// Return the value of a component by its channel keyword.
484    pub fn get_component_by_channel_keyword(
485        &self,
486        channel_keyword: ChannelKeyword,
487    ) -> Result<Option<f32>, ()> {
488        if channel_keyword == ChannelKeyword::Alpha {
489            return Ok(self.alpha());
490        }
491
492        Ok(match self.color_space {
493            ColorSpace::Srgb => {
494                if self.flags.contains(ColorFlags::IS_LEGACY_SRGB) {
495                    match channel_keyword {
496                        ChannelKeyword::R => self.c0().map(|v| v * 255.0),
497                        ChannelKeyword::G => self.c1().map(|v| v * 255.0),
498                        ChannelKeyword::B => self.c2().map(|v| v * 255.0),
499                        _ => return Err(()),
500                    }
501                } else {
502                    match channel_keyword {
503                        ChannelKeyword::R => self.c0(),
504                        ChannelKeyword::G => self.c1(),
505                        ChannelKeyword::B => self.c2(),
506                        _ => return Err(()),
507                    }
508                }
509            },
510            ColorSpace::Hsl => match channel_keyword {
511                ChannelKeyword::H => self.c0(),
512                ChannelKeyword::S => self.c1(),
513                ChannelKeyword::L => self.c2(),
514                _ => return Err(()),
515            },
516            ColorSpace::Hwb => match channel_keyword {
517                ChannelKeyword::H => self.c0(),
518                ChannelKeyword::W => self.c1(),
519                ChannelKeyword::B => self.c2(),
520                _ => return Err(()),
521            },
522            ColorSpace::Lab | ColorSpace::Oklab => match channel_keyword {
523                ChannelKeyword::L => self.c0(),
524                ChannelKeyword::A => self.c1(),
525                ChannelKeyword::B => self.c2(),
526                _ => return Err(()),
527            },
528            ColorSpace::Lch | ColorSpace::Oklch => match channel_keyword {
529                ChannelKeyword::L => self.c0(),
530                ChannelKeyword::C => self.c1(),
531                ChannelKeyword::H => self.c2(),
532                _ => return Err(()),
533            },
534            ColorSpace::SrgbLinear
535            | ColorSpace::DisplayP3
536            | ColorSpace::DisplayP3Linear
537            | ColorSpace::A98Rgb
538            | ColorSpace::ProphotoRgb
539            | ColorSpace::Rec2020 => match channel_keyword {
540                ChannelKeyword::R => self.c0(),
541                ChannelKeyword::G => self.c1(),
542                ChannelKeyword::B => self.c2(),
543                _ => return Err(()),
544            },
545            ColorSpace::XyzD50 | ColorSpace::XyzD65 => match channel_keyword {
546                ChannelKeyword::X => self.c0(),
547                ChannelKeyword::Y => self.c1(),
548                ChannelKeyword::Z => self.c2(),
549                _ => return Err(()),
550            },
551        })
552    }
553
554    /// Convert this color to the specified color space.
555    pub fn to_color_space(&self, color_space: ColorSpace) -> Self {
556        use ColorSpace::*;
557
558        if self.color_space == color_space {
559            return self.clone();
560        }
561
562        // Conversion functions doesn't handle NAN component values, so they are
563        // converted to 0.0. They do however need to know if a component is
564        // missing, so we use NAN as the marker for that.
565        macro_rules! missing_to_nan {
566            ($c:expr) => {{
567                if let Some(v) = $c {
568                    crate::values::normalize(v)
569                } else {
570                    f32::NAN
571                }
572            }};
573        }
574
575        let components = ColorComponents(
576            missing_to_nan!(self.c0()),
577            missing_to_nan!(self.c1()),
578            missing_to_nan!(self.c2()),
579        );
580
581        let result = match (self.color_space, color_space) {
582            // We have simplified conversions that do not need to convert to XYZ
583            // first. This improves performance, because it skips at least 2
584            // matrix multiplications and reduces float rounding errors.
585            (Srgb, Hsl) => convert::rgb_to_hsl(&components),
586            (Srgb, Hwb) => convert::rgb_to_hwb(&components),
587            (Hsl, Srgb) => convert::hsl_to_rgb(&components),
588            (Hwb, Srgb) => convert::hwb_to_rgb(&components),
589            (Lab, Lch) | (Oklab, Oklch) => convert::orthogonal_to_polar(
590                &components,
591                convert::epsilon_for_range(0.0, if color_space == Lch { 100.0 } else { 1.0 }),
592            ),
593            (Lch, Lab) | (Oklch, Oklab) => convert::polar_to_orthogonal(&components),
594
595            // All other conversions need to convert to XYZ first.
596            _ => {
597                let (xyz, white_point) = match self.color_space {
598                    Lab => convert::to_xyz::<convert::Lab>(&components),
599                    Lch => convert::to_xyz::<convert::Lch>(&components),
600                    Oklab => convert::to_xyz::<convert::Oklab>(&components),
601                    Oklch => convert::to_xyz::<convert::Oklch>(&components),
602                    Srgb => convert::to_xyz::<convert::Srgb>(&components),
603                    Hsl => convert::to_xyz::<convert::Hsl>(&components),
604                    Hwb => convert::to_xyz::<convert::Hwb>(&components),
605                    SrgbLinear => convert::to_xyz::<convert::SrgbLinear>(&components),
606                    DisplayP3 => convert::to_xyz::<convert::DisplayP3>(&components),
607                    DisplayP3Linear => convert::to_xyz::<convert::DisplayP3Linear>(&components),
608                    A98Rgb => convert::to_xyz::<convert::A98Rgb>(&components),
609                    ProphotoRgb => convert::to_xyz::<convert::ProphotoRgb>(&components),
610                    Rec2020 => convert::to_xyz::<convert::Rec2020>(&components),
611                    XyzD50 => convert::to_xyz::<convert::XyzD50>(&components),
612                    XyzD65 => convert::to_xyz::<convert::XyzD65>(&components),
613                };
614
615                match color_space {
616                    Lab => convert::from_xyz::<convert::Lab>(&xyz, white_point),
617                    Lch => convert::from_xyz::<convert::Lch>(&xyz, white_point),
618                    Oklab => convert::from_xyz::<convert::Oklab>(&xyz, white_point),
619                    Oklch => convert::from_xyz::<convert::Oklch>(&xyz, white_point),
620                    Srgb => convert::from_xyz::<convert::Srgb>(&xyz, white_point),
621                    Hsl => convert::from_xyz::<convert::Hsl>(&xyz, white_point),
622                    Hwb => convert::from_xyz::<convert::Hwb>(&xyz, white_point),
623                    SrgbLinear => convert::from_xyz::<convert::SrgbLinear>(&xyz, white_point),
624                    DisplayP3 => convert::from_xyz::<convert::DisplayP3>(&xyz, white_point),
625                    DisplayP3Linear => {
626                        convert::from_xyz::<convert::DisplayP3Linear>(&xyz, white_point)
627                    },
628                    A98Rgb => convert::from_xyz::<convert::A98Rgb>(&xyz, white_point),
629                    ProphotoRgb => convert::from_xyz::<convert::ProphotoRgb>(&xyz, white_point),
630                    Rec2020 => convert::from_xyz::<convert::Rec2020>(&xyz, white_point),
631                    XyzD50 => convert::from_xyz::<convert::XyzD50>(&xyz, white_point),
632                    XyzD65 => convert::from_xyz::<convert::XyzD65>(&xyz, white_point),
633                }
634            },
635        };
636
637        // A NAN value coming from a conversion function means the the component
638        // is missing, so we convert it to None.
639        macro_rules! nan_to_missing {
640            ($v:expr) => {{
641                if $v.is_nan() {
642                    None
643                } else {
644                    Some($v)
645                }
646            }};
647        }
648
649        Self::new(
650            color_space,
651            nan_to_missing!(result.0),
652            nan_to_missing!(result.1),
653            nan_to_missing!(result.2),
654            self.alpha(),
655        )
656    }
657
658    /// Convert a color value to `nscolor`.
659    pub fn to_nscolor(&self) -> u32 {
660        let srgb = self.to_color_space(ColorSpace::Srgb);
661        u32::from_le_bytes([
662            (srgb.components.0 * 255.0).round() as u8,
663            (srgb.components.1 * 255.0).round() as u8,
664            (srgb.components.2 * 255.0).round() as u8,
665            (srgb.alpha * 255.0).round() as u8,
666        ])
667    }
668
669    /// Convert a given `nscolor` to a Servo AbsoluteColor value.
670    pub fn from_nscolor(color: u32) -> Self {
671        let [r, g, b, a] = color.to_le_bytes();
672        Self::srgb_legacy(r, g, b, a as f32 / 255.0)
673    }
674}
675
676#[test]
677fn from_nscolor_should_be_in_legacy_syntax() {
678    let result = AbsoluteColor::from_nscolor(0x336699CC);
679    assert!(result.flags.contains(ColorFlags::IS_LEGACY_SRGB));
680    assert!(result.is_legacy_syntax());
681}
682
683impl From<PredefinedColorSpace> for ColorSpace {
684    fn from(value: PredefinedColorSpace) -> Self {
685        match value {
686            PredefinedColorSpace::Srgb => ColorSpace::Srgb,
687            PredefinedColorSpace::SrgbLinear => ColorSpace::SrgbLinear,
688            PredefinedColorSpace::DisplayP3 => ColorSpace::DisplayP3,
689            PredefinedColorSpace::DisplayP3Linear => ColorSpace::DisplayP3Linear,
690            PredefinedColorSpace::A98Rgb => ColorSpace::A98Rgb,
691            PredefinedColorSpace::ProphotoRgb => ColorSpace::ProphotoRgb,
692            PredefinedColorSpace::Rec2020 => ColorSpace::Rec2020,
693            PredefinedColorSpace::XyzD50 => ColorSpace::XyzD50,
694            PredefinedColorSpace::XyzD65 => ColorSpace::XyzD65,
695        }
696    }
697}