summaryrefslogtreecommitdiffstats
path: root/servo/components/style/color/color_function.rs
blob: 426256a2e92b741537acb5db562e1931bf4d96d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Output of parsing a color function, e.g. rgb(..), hsl(..), color(..)

use crate::{color::ColorFlags, values::normalize};
use cssparser::color::{PredefinedColorSpace, OPAQUE};

use super::{
    component::ColorComponent,
    convert::normalize_hue,
    parsing::{NumberOrAngle, NumberOrPercentage},
    AbsoluteColor, ColorSpace,
};

/// Represents a specified color function.
#[derive(Debug)]
pub enum ColorFunction {
    /// <https://drafts.csswg.org/css-color-4/#rgb-functions>
    Rgb(
        ColorComponent<u8>,                 // red
        ColorComponent<u8>,                 // green
        ColorComponent<u8>,                 // blue
        ColorComponent<NumberOrPercentage>, // alpha
    ),
    /// <https://drafts.csswg.org/css-color-4/#the-hsl-notation>
    Hsl(
        ColorComponent<NumberOrAngle>,      // hue
        ColorComponent<NumberOrPercentage>, // saturation
        ColorComponent<NumberOrPercentage>, // lightness
        ColorComponent<NumberOrPercentage>, // alpha
        bool,                               // is_legacy_syntax
    ),
    /// <https://drafts.csswg.org/css-color-4/#the-hwb-notation>
    Hwb(
        ColorComponent<NumberOrAngle>,      // hue
        ColorComponent<NumberOrPercentage>, // whiteness
        ColorComponent<NumberOrPercentage>, // blackness
        ColorComponent<NumberOrPercentage>, // alpha
        bool,                               // is_legacy_syntax
    ),
    /// <https://drafts.csswg.org/css-color-4/#specifying-lab-lch>
    Lab(
        ColorComponent<NumberOrPercentage>, // lightness
        ColorComponent<NumberOrPercentage>, // a
        ColorComponent<NumberOrPercentage>, // b
        ColorComponent<NumberOrPercentage>, // alpha
    ),
    /// <https://drafts.csswg.org/css-color-4/#specifying-lab-lch>
    Lch(
        ColorComponent<NumberOrPercentage>, // lightness
        ColorComponent<NumberOrPercentage>, // chroma
        ColorComponent<NumberOrAngle>,      // hue
        ColorComponent<NumberOrPercentage>, // alpha
    ),
    /// <https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch>
    Oklab(
        ColorComponent<NumberOrPercentage>, // lightness
        ColorComponent<NumberOrPercentage>, // a
        ColorComponent<NumberOrPercentage>, // b
        ColorComponent<NumberOrPercentage>, // alpha
    ),
    /// <https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch>
    Oklch(
        ColorComponent<NumberOrPercentage>, // lightness
        ColorComponent<NumberOrPercentage>, // chroma
        ColorComponent<NumberOrAngle>,      // hue
        ColorComponent<NumberOrPercentage>, // alpha
    ),
    /// <https://drafts.csswg.org/css-color-4/#color-function>
    Color(
        PredefinedColorSpace,
        ColorComponent<NumberOrPercentage>, // red / x
        ColorComponent<NumberOrPercentage>, // green / y
        ColorComponent<NumberOrPercentage>, // blue / z
        ColorComponent<NumberOrPercentage>, // alpha
    ),
}

impl ColorFunction {
    /// Try to resolve the color function to an [`AbsoluteColor`] that does not
    /// contain any variables (currentcolor, color components, etc.).
    pub fn resolve_to_absolute(&self) -> AbsoluteColor {
        macro_rules! value {
            ($v:expr) => {{
                match $v {
                    ColorComponent::None => None,
                    // value should be Copy.
                    ColorComponent::Value(value) => Some(*value),
                }
            }};
        }

        macro_rules! alpha {
            ($alpha:expr) => {{
                value!($alpha).map(|value| normalize(value.to_number(1.0)).clamp(0.0, OPAQUE))
            }};
        }

        match self {
            ColorFunction::Rgb(r, g, b, alpha) => {
                let r = value!(r).unwrap_or(0);
                let g = value!(g).unwrap_or(0);
                let b = value!(b).unwrap_or(0);

                AbsoluteColor::srgb_legacy(r, g, b, alpha!(alpha).unwrap_or(0.0))
            },
            ColorFunction::Hsl(h, s, l, alpha, is_legacy_syntax) => {
                // Percent reference range for S and L: 0% = 0.0, 100% = 100.0
                const LIGHTNESS_RANGE: f32 = 100.0;
                const SATURATION_RANGE: f32 = 100.0;

                let mut result = AbsoluteColor::new(
                    ColorSpace::Hsl,
                    value!(h).map(|angle| normalize_hue(angle.degrees())),
                    value!(s).map(|s| s.to_number(SATURATION_RANGE).clamp(0.0, SATURATION_RANGE)),
                    value!(l).map(|l| l.to_number(LIGHTNESS_RANGE).clamp(0.0, LIGHTNESS_RANGE)),
                    alpha!(alpha),
                );

                if *is_legacy_syntax {
                    result.flags.insert(ColorFlags::IS_LEGACY_SRGB);
                }

                result
            },
            ColorFunction::Hwb(h, w, b, alpha, is_legacy_syntax) => {
                // Percent reference range for W and B: 0% = 0.0, 100% = 100.0
                const WHITENESS_RANGE: f32 = 100.0;
                const BLACKNESS_RANGE: f32 = 100.0;

                let mut result = AbsoluteColor::new(
                    ColorSpace::Hwb,
                    value!(h).map(|angle| normalize_hue(angle.degrees())),
                    value!(w).map(|w| w.to_number(WHITENESS_RANGE).clamp(0.0, WHITENESS_RANGE)),
                    value!(b).map(|b| b.to_number(BLACKNESS_RANGE).clamp(0.0, BLACKNESS_RANGE)),
                    alpha!(alpha),
                );

                if *is_legacy_syntax {
                    result.flags.insert(ColorFlags::IS_LEGACY_SRGB);
                }

                result
            },
            ColorFunction::Lab(l, a, b, alpha) => {
                // for L: 0% = 0.0, 100% = 100.0
                // for a and b: -100% = -125, 100% = 125
                const LIGHTNESS_RANGE: f32 = 100.0;
                const A_B_RANGE: f32 = 125.0;

                AbsoluteColor::new(
                    ColorSpace::Lab,
                    value!(l).map(|l| l.to_number(LIGHTNESS_RANGE)),
                    value!(a).map(|a| a.to_number(A_B_RANGE)),
                    value!(b).map(|b| b.to_number(A_B_RANGE)),
                    alpha!(alpha),
                )
            },
            ColorFunction::Lch(l, c, h, alpha) => {
                // for L: 0% = 0.0, 100% = 100.0
                // for C: 0% = 0, 100% = 150
                const LIGHTNESS_RANGE: f32 = 100.0;
                const CHROMA_RANGE: f32 = 150.0;

                AbsoluteColor::new(
                    ColorSpace::Lch,
                    value!(l).map(|l| l.to_number(LIGHTNESS_RANGE)),
                    value!(c).map(|c| c.to_number(CHROMA_RANGE)),
                    value!(h).map(|angle| normalize_hue(angle.degrees())),
                    alpha!(alpha),
                )
            },
            ColorFunction::Oklab(l, a, b, alpha) => {
                // for L: 0% = 0.0, 100% = 1.0
                // for a and b: -100% = -0.4, 100% = 0.4
                const LIGHTNESS_RANGE: f32 = 1.0;
                const A_B_RANGE: f32 = 0.4;

                AbsoluteColor::new(
                    ColorSpace::Oklab,
                    value!(l).map(|l| l.to_number(LIGHTNESS_RANGE)),
                    value!(a).map(|a| a.to_number(A_B_RANGE)),
                    value!(b).map(|b| b.to_number(A_B_RANGE)),
                    alpha!(alpha),
                )
            },
            ColorFunction::Oklch(l, c, h, alpha) => {
                // for L: 0% = 0.0, 100% = 1.0
                // for C: 0% = 0.0 100% = 0.4
                const LIGHTNESS_RANGE: f32 = 1.0;
                const CHROMA_RANGE: f32 = 0.4;

                AbsoluteColor::new(
                    ColorSpace::Oklch,
                    value!(l).map(|l| l.to_number(LIGHTNESS_RANGE)),
                    value!(c).map(|c| c.to_number(CHROMA_RANGE)),
                    value!(h).map(|angle| normalize_hue(angle.degrees())),
                    alpha!(alpha),
                )
            },
            ColorFunction::Color(color_space, r, g, b, alpha) => AbsoluteColor::new(
                (*color_space).into(),
                value!(r).map(|c| c.to_number(1.0)),
                value!(g).map(|c| c.to_number(1.0)),
                value!(b).map(|c| c.to_number(1.0)),
                alpha!(alpha),
            ),
        }
    }
}