Skip to content

Commit

Permalink
Merge pull request #80 from disjukr/master
Browse files Browse the repository at this point in the history
Parse 8, 4 digits hex notations
  • Loading branch information
SimonSapin committed Jul 20, 2015
2 parents 6646666 + 4c9f491 commit 53a543d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ fn rgb(red: f32, green: f32, blue: f32) -> Result<Color, ()> {
}))
}

#[inline]
fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Result<Color, ()> {
Ok(Color::RGBA(RGBA {
red: red / 255.,
green: green / 255.,
blue: blue / 255.,
alpha: alpha / 255.,
}))
}


/// Return the named color with the given name.
///
Expand Down Expand Up @@ -267,11 +277,23 @@ fn from_hex(c: u8) -> Result<u8, ()> {
fn parse_color_hash(value: &str) -> Result<Color, ()> {
let value = value.as_bytes();
match value.len() {
8 => rgba(
(try!(from_hex(value[0])) * 16 + try!(from_hex(value[1]))) as f32,
(try!(from_hex(value[2])) * 16 + try!(from_hex(value[3]))) as f32,
(try!(from_hex(value[4])) * 16 + try!(from_hex(value[5]))) as f32,
(try!(from_hex(value[6])) * 16 + try!(from_hex(value[7]))) as f32,
),
6 => rgb(
(try!(from_hex(value[0])) * 16 + try!(from_hex(value[1]))) as f32,
(try!(from_hex(value[2])) * 16 + try!(from_hex(value[3]))) as f32,
(try!(from_hex(value[4])) * 16 + try!(from_hex(value[5]))) as f32,
),
4 => rgba(
(try!(from_hex(value[0])) * 17) as f32,
(try!(from_hex(value[1])) * 17) as f32,
(try!(from_hex(value[2])) * 17) as f32,
(try!(from_hex(value[3])) * 17) as f32,
),
3 => rgb(
(try!(from_hex(value[0])) * 17) as f32,
(try!(from_hex(value[1])) * 17) as f32,
Expand Down
6 changes: 4 additions & 2 deletions src/css-parsing-tests/color3.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
"#ff", null,
"#fff", [1, 1, 1, 1],
"#ffg", null,
"#ffff", null,
"#ffff", [1, 1, 1, 1],
"#fffg", null,
"#fffff", null,
"#ffffff", [1, 1, 1, 1],
"#fffffg", null,
"#fffffff", null,
"#ffffffff", null,
"#ffffffff", [1, 1, 1, 1],
"#fffffffg", null,
"#fffffffff", null,

"#FFCc99", [1, 0.8, 0.6, 1],
Expand Down

0 comments on commit 53a543d

Please sign in to comment.