Skip to main content

style/values/computed/
color.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//! Computed color values.
6
7use crate::color::AbsoluteColor;
8use crate::typed_om::{KeywordValue, ToTyped, TypedValue};
9use crate::values::animated::ToAnimatedZero;
10use crate::values::computed::percentage::Percentage;
11use crate::values::generics::color::{
12    GenericCaretColor, GenericColor, GenericColorMix, GenericColorOrAuto,
13};
14use std::fmt::{self, Write};
15use style_traits::{CssString, CssWriter, ToCss};
16use thin_vec::ThinVec;
17
18pub use crate::values::specified::color::{ColorScheme, ForcedColorAdjust, PrintColorAdjust};
19
20/// The computed value of the `color` property.
21pub type ColorPropertyValue = AbsoluteColor;
22
23/// A computed value for `<color>`.
24pub type Color = GenericColor<Percentage>;
25
26/// A computed color-mix().
27pub type ColorMix = GenericColorMix<Color, Percentage>;
28
29impl ToCss for Color {
30    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
31    where
32        W: fmt::Write,
33    {
34        match *self {
35            Self::Absolute(ref c) => c.to_css(dest),
36            Self::ColorFunction(ref color_function) => color_function.to_css(dest),
37            Self::CurrentColor => dest.write_str("currentcolor"),
38            Self::ColorMix(ref m) => m.to_css(dest),
39            Self::ContrastColor(ref c) => {
40                dest.write_str("contrast-color(")?;
41                c.to_css(dest)?;
42                dest.write_char(')')
43            },
44        }
45    }
46}
47
48impl ToTyped for Color {
49    fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
50        match *self {
51            Self::CurrentColor => {
52                dest.push(TypedValue::Keyword(KeywordValue(CssString::from(
53                    "currentcolor",
54                ))));
55                Ok(())
56            },
57            _ => Err(()),
58        }
59    }
60}
61
62impl Color {
63    /// A fully transparent color.
64    pub const TRANSPARENT_BLACK: Self = Self::Absolute(AbsoluteColor::TRANSPARENT_BLACK);
65
66    /// An opaque black color.
67    pub const BLACK: Self = Self::Absolute(AbsoluteColor::BLACK);
68
69    /// An opaque white color.
70    pub const WHITE: Self = Self::Absolute(AbsoluteColor::WHITE);
71
72    /// Create a new computed [`Color`] from a given color-mix, simplifying it to an absolute color
73    /// if possible.
74    pub fn from_color_mix(color_mix: ColorMix) -> Self {
75        if let Some(absolute) = color_mix.mix_to_absolute() {
76            Self::Absolute(absolute)
77        } else {
78            Self::ColorMix(Box::new(color_mix))
79        }
80    }
81
82    /// Combine this complex color with the given foreground color into an absolute color.
83    pub fn resolve_to_absolute(&self, current_color: &AbsoluteColor) -> AbsoluteColor {
84        match *self {
85            Self::Absolute(c) => c,
86            Self::ColorFunction(ref color_function) => {
87                color_function.resolve_to_absolute(current_color)
88            },
89            Self::CurrentColor => *current_color,
90            Self::ColorMix(ref mix) => {
91                use crate::color::mix;
92
93                mix::mix_many(
94                    mix.interpolation,
95                    mix.items.iter().map(|item| {
96                        mix::ColorMixItem::new(
97                            item.color.resolve_to_absolute(current_color),
98                            item.percentage.0,
99                        )
100                    }),
101                    mix.flags,
102                )
103            },
104            Self::ContrastColor(ref c) => {
105                let bg_color = c.resolve_to_absolute(current_color);
106                if Self::contrast_ratio(&bg_color, &AbsoluteColor::BLACK)
107                    > Self::contrast_ratio(&bg_color, &AbsoluteColor::WHITE)
108                {
109                    AbsoluteColor::BLACK
110                } else {
111                    AbsoluteColor::WHITE
112                }
113            },
114        }
115    }
116
117    fn contrast_ratio(a: &AbsoluteColor, b: &AbsoluteColor) -> f32 {
118        // TODO: This just implements the WCAG 2.1 algorithm,
119        // https://www.w3.org/TR/WCAG21/#dfn-contrast-ratio
120        // Consider using a more sophisticated contrast algorithm, e.g. see
121        // https://apcacontrast.com
122        let compute = |c| -> f32 {
123            if c <= 0.04045 {
124                c / 12.92
125            } else {
126                f32::powf((c + 0.055) / 1.055, 2.4)
127            }
128        };
129        let luminance = |r, g, b| -> f32 { 0.2126 * r + 0.7152 * g + 0.0722 * b };
130        let a = a.into_srgb_legacy();
131        let b = b.into_srgb_legacy();
132        let a = a.raw_components();
133        let b = b.raw_components();
134        let la = luminance(compute(a[0]), compute(a[1]), compute(a[2])) + 0.05;
135        let lb = luminance(compute(b[0]), compute(b[1]), compute(b[2])) + 0.05;
136        if la > lb {
137            la / lb
138        } else {
139            lb / la
140        }
141    }
142}
143
144impl ToAnimatedZero for AbsoluteColor {
145    fn to_animated_zero(&self) -> Result<Self, ()> {
146        Ok(Self::TRANSPARENT_BLACK)
147    }
148}
149
150/// auto | <color>
151pub type ColorOrAuto = GenericColorOrAuto<Color>;
152
153/// caret-color
154pub type CaretColor = GenericCaretColor<Color>;