Skip to content

Commit fd45d73

Browse files
author
bors-servo
committed
Auto merge of #84 - servo:clamp_, r=SimonSapin
Clamp rgb values to [0,1] #83 plus a code comment linking to discussion. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/84) <!-- Reviewable:end -->
2 parents 2e98c8f + 1d9144a commit fd45d73

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/color.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,13 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
314314
_ => return Err(())
315315
};
316316

317-
let red: f32;
318-
let green: f32;
319-
let blue: f32;
317+
fn clamp(val: f32) -> f32 {
318+
val.max(0.).min(1.)
319+
}
320+
321+
let mut red: f32;
322+
let mut green: f32;
323+
let mut blue: f32;
320324
if is_rgb {
321325
// Either integers or percentages, but all the same type.
322326
match try!(arguments.next()) {
@@ -336,13 +340,23 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
336340
}
337341
_ => return Err(())
338342
};
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);
339350
} else {
340351
let hue = try!(arguments.expect_number()) / 360.;
341352
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
342356
try!(arguments.expect_comma());
343-
let saturation = (try!(arguments.expect_percentage())).max(0.).min(1.);
357+
let saturation = clamp(try!(arguments.expect_percentage()));
344358
try!(arguments.expect_comma());
345-
let lightness = (try!(arguments.expect_percentage())).max(0.).min(1.);
359+
let lightness = clamp(try!(arguments.expect_percentage()));
346360

347361
// https://drafts.csswg.org/css-color/#hsl-color
348362
fn hue_to_rgb(m1: f32, m2: f32, mut h: f32) -> f32 {
@@ -364,7 +378,7 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
364378

365379
let alpha = if has_alpha {
366380
try!(arguments.expect_comma());
367-
(try!(arguments.expect_number())).max(0.).min(1.)
381+
clamp(try!(arguments.expect_number()))
368382
} else {
369383
1.
370384
};

src/css-parsing-tests/color3.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"rgb(0,51,255)", [0, 0.2, 1, 1],
4747
"rgb(0\t, 51 ,255)", [0, 0.2, 1, 1],
4848
"rgb(/* R */0, /* G */51, /* B */255)", [0, 0.2, 1, 1],
49-
"rgb(-51, 306, 0)", [-0.2, 1.2, 0, 1],
49+
"rgb(-51, 306, 0)", [0, 1, 0, 1],
5050

5151
"rgb(42%, 3%, 50%)", [0.42, 0.03, 0.5, 1],
5252
"RGB(100%, 100%, 100%)", [1, 1, 1, 1],
@@ -55,7 +55,7 @@
5555
"rgb(10%,20%,30%)", [0.1, 0.2, 0.3, 1],
5656
"rgb(10%\t, 20% ,30%)", [0.1, 0.2, 0.3, 1],
5757
"rgb(/* R */ 10%, /* G */ 20%, /* B */ 30%)", [0.1, 0.2, 0.3, 1],
58-
"rgb(-12%, 110%, 1400%)", [-0.12, 1.1, 14, 1],
58+
"rgb(-12%, 110%, 1400%)", [0, 1, 1, 1],
5959

6060
"rgb(10%, 50%, 0)", null,
6161
"rgb(255, 50%, 0%)", null,

0 commit comments

Comments
 (0)