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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! CSS color values.
use cssparser::{ParseErrorKind, Parser};
use cssparser_color as cssc;
use cssparser_color::{hsl_to_rgb, hwb_to_rgb};
use crate::error::*;
use crate::parsers::Parse;
use crate::unit_interval::UnitInterval;
use crate::util;
/// Subset of <https://drafts.csswg.org/css-color-4/#color-type>
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
/// The 'currentcolor' keyword.
CurrentColor,
/// Specify sRGB colors directly by their red/green/blue/alpha chanels.
Rgba(RGBA),
/// Specifies a color in sRGB using hue, saturation and lightness components.
Hsl(Hsl),
/// Specifies a color in sRGB using hue, whiteness and blackness components.
Hwb(Hwb),
}
/// A color with red, green, blue, and alpha components, in a byte each.
#[allow(clippy::upper_case_acronyms)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RGBA {
/// The red component.
pub red: u8,
/// The green component.
pub green: u8,
/// The blue component.
pub blue: u8,
/// The alpha component.
pub alpha: f32,
}
/// Color specified by hue, saturation and lightness components.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Hsl {
/// The hue component.
pub hue: Option<f32>,
/// The saturation component.
pub saturation: Option<f32>,
/// The lightness component.
pub lightness: Option<f32>,
/// The alpha component.
pub alpha: Option<f32>,
}
/// Color specified by hue, whiteness and blackness components.
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Hwb {
/// The hue component.
pub hue: Option<f32>,
/// The whiteness component.
pub whiteness: Option<f32>,
/// The blackness component.
pub blackness: Option<f32>,
/// The alpha component.
pub alpha: Option<f32>,
}
const OPAQUE: f32 = 1.0;
impl RGBA {
/// Constructs a new RGBA value from float components. It expects the red,
/// green, blue and alpha channels in that order, and all values will be
/// clamped to the 0.0 ... 1.0 range.
#[inline]
fn from_floats(red: f32, green: f32, blue: f32, alpha: f32) -> Self {
Self::new(
clamp_unit_f32(red),
clamp_unit_f32(green),
clamp_unit_f32(blue),
alpha.clamp(0.0, OPAQUE),
)
}
/// Same thing, but with `u8` values instead of floats in the 0 to 1 range.
#[inline]
pub const fn new(red: u8, green: u8, blue: u8, alpha: f32) -> Self {
Self {
red,
green,
blue,
alpha,
}
}
}
impl From<cssc::RgbaLegacy> for RGBA {
fn from(c: cssc::RgbaLegacy) -> RGBA {
RGBA {
red: c.red,
green: c.green,
blue: c.blue,
alpha: c.alpha,
}
}
}
impl From<cssc::Hsl> for Hsl {
fn from(c: cssc::Hsl) -> Hsl {
Hsl {
hue: c.hue,
saturation: c.saturation,
lightness: c.lightness,
alpha: c.alpha,
}
}
}
impl From<cssc::Hwb> for Hwb {
fn from(c: cssc::Hwb) -> Hwb {
Hwb {
hue: c.hue,
whiteness: c.whiteness,
blackness: c.blackness,
alpha: c.alpha,
}
}
}
fn clamp_unit_f32(val: f32) -> u8 {
// Whilst scaling by 256 and flooring would provide
// an equal distribution of integers to percentage inputs,
// this is not what Gecko does so we instead multiply by 255
// and round (adding 0.5 and flooring is equivalent to rounding)
//
// Chrome does something similar for the alpha value, but not
// the rgb values.
//
// See <https://bugzilla.mozilla.org/show_bug.cgi?id=1340484>
//
// Clamping to 256 and rounding after would let 1.0 map to 256, and
// `256.0_f32 as u8` is undefined behavior:
//
// <https://github.com/rust-lang/rust/issues/10184>
clamp_floor_256_f32(val * 255.)
}
fn clamp_floor_256_f32(val: f32) -> u8 {
val.round().clamp(0., 255.) as u8
}
/// Turn a short-lived [`cssparser::ParseError`] into a long-lived [`ParseError`].
///
/// cssparser's error type has a lifetime equal to the string being parsed. We want
/// a long-lived error so we can store it away if needed. Basically, here we turn
/// a `&str` into a `String`.
fn map_color_parse_error(err: cssparser::ParseError<'_, ()>) -> ParseError<'_> {
let string_err = match err.kind {
ParseErrorKind::Basic(ref e) => format!("{}", e),
ParseErrorKind::Custom(()) => {
// In cssparser 0.31, the error type for Color::parse is defined like this:
//
// pub fn parse<'i>(input: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i, ()>> {
//
// The ParseError<'i, ()> means that the ParseErrorKind::Custom(T) variant will have
// T be the () type.
//
// So, here we match for () inside the Custom variant. If cssparser
// changes its error API, this match will hopefully catch errors.
//
// Implementation detail: Color::parse() does not ever return Custom errors, only
// Basic ones. So the match for Basic above handles everything, and this one
// for () is a dummy case.
"could not parse color".to_string()
}
};
ParseError {
kind: ParseErrorKind::Custom(ValueErrorKind::Parse(string_err)),
location: err.location,
}
}
fn parse_plain_color<'i>(parser: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i>> {
let loc = parser.current_source_location();
let csscolor = cssc::Color::parse(parser).map_err(map_color_parse_error)?;
// Return only supported color types, and mark the others as errors.
match csscolor {
cssc::Color::CurrentColor => Ok(Color::CurrentColor),
cssc::Color::Rgba(rgba) => Ok(Color::Rgba(rgba.into())),
cssc::Color::Hsl(hsl) => Ok(Color::Hsl(hsl.into())),
cssc::Color::Hwb(hwb) => Ok(Color::Hwb(hwb.into())),
_ => Err(ParseError {
kind: ParseErrorKind::Custom(ValueErrorKind::parse_error("unsupported color syntax")),
location: loc,
}),
}
}
/// Parse a custom property name.
///
/// <https://drafts.csswg.org/css-variables/#typedef-custom-property-name>
fn parse_name(s: &str) -> Result<&str, ()> {
if s.starts_with("--") && s.len() > 2 {
Ok(&s[2..])
} else {
Err(())
}
}
fn parse_var_with_fallback<'i>(parser: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i>> {
let name = parser.expect_ident_cloned()?;
// ignore the name for now; we'll use it later when we actually
// process the names of custom variables
let _name = parse_name(&name).map_err(|()| {
parser.new_custom_error(ValueErrorKind::parse_error(&format!(
"unexpected identifier {}",
name
)))
})?;
parser.expect_comma()?;
// FIXME: when fixing #459 (full support for var()), note that
// https://drafts.csswg.org/css-variables/#using-variables indicates that var(--a,) is
// a valid function, which means that the fallback value is an empty set of tokens.
//
// Also, see Servo's extra code to handle semicolons and stuff in toplevel rules.
//
// Also, tweak the tests tagged with "FIXME: var()" below.
parse_plain_color(parser)
}
impl Parse for Color {
fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Color, ParseError<'i>> {
if let Ok(c) = parser.try_parse(|p| {
p.expect_function_matching("var")?;
p.parse_nested_block(parse_var_with_fallback)
}) {
Ok(c)
} else {
parse_plain_color(parser)
}
}
}
/// Normalizes `h` (a hue value in degrees) to be in the interval `[0.0, 1.0]`.
///
/// Rust-cssparser (the cssparser-color crate) provides
/// [`hsl_to_rgb()`], but it assumes that the hue is between 0 and 1.
/// `normalize_hue()` takes a value with respect to a scale of 0 to
/// 360 degrees and converts it to that different scale.
fn normalize_hue(h: f32) -> f32 {
h.rem_euclid(360.0) / 360.0
}
pub fn color_to_rgba(color: &Color) -> RGBA {
match color {
Color::Rgba(rgba) => *rgba,
Color::Hsl(hsl) => {
let hue = normalize_hue(hsl.hue.unwrap_or(0.0));
let (red, green, blue) = hsl_to_rgb(
hue,
hsl.saturation.unwrap_or(0.0),
hsl.lightness.unwrap_or(0.0),
);
RGBA::from_floats(red, green, blue, hsl.alpha.unwrap_or(OPAQUE))
}
Color::Hwb(hwb) => {
let hue = normalize_hue(hwb.hue.unwrap_or(0.0));
let (red, green, blue) = hwb_to_rgb(
hue,
hwb.whiteness.unwrap_or(0.0),
hwb.blackness.unwrap_or(0.0),
);
RGBA::from_floats(red, green, blue, hwb.alpha.unwrap_or(OPAQUE))
}
_ => unimplemented!(),
}
}
/// Takes the `opacity` property and an alpha value from a CSS `<color>` and returns a resulting
/// alpha for a computed value.
///
/// `alpha` is `Option<f32>` because that is what cssparser uses everywhere.
fn resolve_alpha(opacity: UnitInterval, alpha: Option<f32>) -> f32 {
let UnitInterval(o) = opacity;
let alpha = f64::from(alpha.unwrap_or(0.0)) * o;
let alpha = util::clamp(alpha, 0.0, 1.0);
cast::f32(alpha).unwrap()
}
fn black() -> Color {
Color::Rgba(RGBA::new(0, 0, 0, 1.0))
}
/// Resolves a CSS color from itself, an `opacity` property, and a `color` property (to resolve `currentColor`).
///
/// A CSS color can be `currentColor`, in which case the computed value comes from
/// the `color` property. You should pass the `color` property's value for `current_color`.
///
/// Note that `currrent_color` can itself have a value of `currentColor`. In that case, we
/// consider it to be opaque black.
pub fn resolve_color(color: &Color, opacity: UnitInterval, current_color: &Color) -> Color {
let without_opacity_applied = match color {
Color::CurrentColor => {
if let Color::CurrentColor = current_color {
black()
} else {
*current_color
}
}
_ => *color,
};
match without_opacity_applied {
Color::CurrentColor => unreachable!(),
Color::Rgba(rgba) => Color::Rgba(RGBA {
alpha: resolve_alpha(opacity, Some(rgba.alpha)),
..rgba
}),
Color::Hsl(hsl) => Color::Hsl(Hsl {
alpha: Some(resolve_alpha(opacity, hsl.alpha)),
..hsl
}),
Color::Hwb(hwb) => Color::Hwb(Hwb {
alpha: Some(resolve_alpha(opacity, hwb.alpha)),
..hwb
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_plain_color() {
assert_eq!(
Color::parse_str("#112233").unwrap(),
Color::Rgba(RGBA::new(0x11, 0x22, 0x33, 1.0))
);
}
#[test]
fn var_with_fallback_parses_as_color() {
assert_eq!(
Color::parse_str("var(--foo, #112233)").unwrap(),
Color::Rgba(RGBA::new(0x11, 0x22, 0x33, 1.0))
);
assert_eq!(
Color::parse_str("var(--foo, rgb(100% 50% 25%)").unwrap(),
Color::Rgba(RGBA::new(0xff, 0x80, 0x40, 1.0))
);
}
// FIXME: var() - when fixing #459, see the note in the code above. All the syntaxes
// in this test function will become valid once we have full support for var().
#[test]
fn var_without_fallback_yields_error() {
assert!(Color::parse_str("var(--foo)").is_err());
assert!(Color::parse_str("var(--foo,)").is_err());
assert!(Color::parse_str("var(--foo, )").is_err());
assert!(Color::parse_str("var(--foo, this is not a color)").is_err());
assert!(Color::parse_str("var(--foo, #112233, blah)").is_err());
}
#[test]
fn normalizes_hue() {
assert_eq!(normalize_hue(0.0), 0.0);
assert_eq!(normalize_hue(360.0), 0.0);
assert_eq!(normalize_hue(90.0), 0.25);
assert_eq!(normalize_hue(-90.0), 0.75);
assert_eq!(normalize_hue(450.0), 0.25); // 360 + 90 degrees
assert_eq!(normalize_hue(-450.0), 0.75);
}
// Bug #1117
#[test]
fn large_hue_value() {
let _ = color_to_rgba(&Color::parse_str("hsla(70000000000000,4%,10%,.2)").unwrap());
}
}