Skip to content

Commit 9a8620d

Browse files
committed
clamp rgb values to [0,1]
1 parent 2e98c8f commit 9a8620d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/color.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,14 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
321321
// Either integers or percentages, but all the same type.
322322
match try!(arguments.next()) {
323323
Token::Number(ref v) if v.int_value.is_some() => {
324-
red = v.value / 255.;
324+
fn clamp(value: f32) -> f32 {
325+
0f32.max(1f32.min(value))
326+
}
327+
red = clamp(v.value / 255.);
325328
try!(arguments.expect_comma());
326-
green = try!(arguments.expect_integer()) as f32 / 255.;
329+
green = clamp(try!(arguments.expect_integer()) as f32 / 255.);
327330
try!(arguments.expect_comma());
328-
blue = try!(arguments.expect_integer()) as f32 / 255.;
331+
blue = clamp(try!(arguments.expect_integer()) as f32 / 255.);
329332
}
330333
Token::Percentage(ref v) => {
331334
red = v.unit_value;

src/css-parsing-tests/color3.json

+1-1
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],

0 commit comments

Comments
 (0)