Skip to content

Commit 1d9144a

Browse files
committed
Add a comment on clamping rgb() and rgba() colors.
1 parent 3233bbb commit 1d9144a

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/color.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,32 +318,41 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
318318
val.max(0.).min(1.)
319319
}
320320

321-
let red: f32;
322-
let green: f32;
323-
let blue: f32;
321+
let mut red: f32;
322+
let mut green: f32;
323+
let mut blue: f32;
324324
if is_rgb {
325325
// Either integers or percentages, but all the same type.
326326
match try!(arguments.next()) {
327327
Token::Number(ref v) if v.int_value.is_some() => {
328-
329-
red = clamp(v.value / 255.);
328+
red = v.value / 255.;
330329
try!(arguments.expect_comma());
331-
green = clamp(try!(arguments.expect_integer()) as f32 / 255.);
330+
green = try!(arguments.expect_integer()) as f32 / 255.;
332331
try!(arguments.expect_comma());
333-
blue = clamp(try!(arguments.expect_integer()) as f32 / 255.);
332+
blue = try!(arguments.expect_integer()) as f32 / 255.;
334333
}
335334
Token::Percentage(ref v) => {
336-
red = clamp(v.unit_value);
335+
red = v.unit_value;
337336
try!(arguments.expect_comma());
338-
green = clamp(try!(arguments.expect_percentage()));
337+
green = try!(arguments.expect_percentage());
339338
try!(arguments.expect_comma());
340-
blue = clamp(try!(arguments.expect_percentage()));
339+
blue = try!(arguments.expect_percentage());
341340
}
342341
_ => return Err(())
343342
};
343+
// The spec says to clamp to the device gamut which may be wider than 0% ... 100%,
344+
// but moz2d doesn’t seem to have any support for this, so let’s not bother.
345+
// https://drafts.csswg.org/css-color/#rgb-functions
346+
// https://github.com/servo/rust-cssparser/issues/76
347+
red = clamp(red);
348+
green = clamp(green);
349+
blue = clamp(blue);
344350
} else {
345351
let hue = try!(arguments.expect_number()) / 360.;
346352
let hue = hue - hue.floor();
353+
// Saturation and lightness are clamped to 0% ... 100%
354+
// regardless of device gamut:
355+
// https://drafts.csswg.org/css-color/#the-hsl-notation
347356
try!(arguments.expect_comma());
348357
let saturation = clamp(try!(arguments.expect_percentage()));
349358
try!(arguments.expect_comma());

0 commit comments

Comments
 (0)