Skip to content

clamp rgb values to [0,1] #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,35 +314,40 @@ fn parse_color_function(name: &str, arguments: &mut Parser) -> Result<Color, ()>
_ => return Err(())
};

fn clamp(val: f32) -> f32 {
val.max(0.).min(1.)
}

let red: f32;
let green: f32;
let blue: f32;
if is_rgb {
// Either integers or percentages, but all the same type.
match try!(arguments.next()) {
Token::Number(ref v) if v.int_value.is_some() => {
red = v.value / 255.;

red = clamp(v.value / 255.);
try!(arguments.expect_comma());
green = try!(arguments.expect_integer()) as f32 / 255.;
green = clamp(try!(arguments.expect_integer()) as f32 / 255.);
try!(arguments.expect_comma());
blue = try!(arguments.expect_integer()) as f32 / 255.;
blue = clamp(try!(arguments.expect_integer()) as f32 / 255.);
}
Token::Percentage(ref v) => {
red = v.unit_value;
red = clamp(v.unit_value);
try!(arguments.expect_comma());
green = try!(arguments.expect_percentage());
green = clamp(try!(arguments.expect_percentage()));
try!(arguments.expect_comma());
blue = try!(arguments.expect_percentage());
blue = clamp(try!(arguments.expect_percentage()));
}
_ => return Err(())
};
} else {
let hue = try!(arguments.expect_number()) / 360.;
let hue = hue - hue.floor();
try!(arguments.expect_comma());
let saturation = (try!(arguments.expect_percentage())).max(0.).min(1.);
let saturation = clamp(try!(arguments.expect_percentage()));
try!(arguments.expect_comma());
let lightness = (try!(arguments.expect_percentage())).max(0.).min(1.);
let lightness = clamp(try!(arguments.expect_percentage()));

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

let alpha = if has_alpha {
try!(arguments.expect_comma());
(try!(arguments.expect_number())).max(0.).min(1.)
clamp(try!(arguments.expect_number()))
} else {
1.
};
Expand Down
4 changes: 2 additions & 2 deletions src/css-parsing-tests/color3.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"rgb(0,51,255)", [0, 0.2, 1, 1],
"rgb(0\t, 51 ,255)", [0, 0.2, 1, 1],
"rgb(/* R */0, /* G */51, /* B */255)", [0, 0.2, 1, 1],
"rgb(-51, 306, 0)", [-0.2, 1.2, 0, 1],
"rgb(-51, 306, 0)", [0, 1, 0, 1],

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

"rgb(10%, 50%, 0)", null,
"rgb(255, 50%, 0%)", null,
Expand Down