From 4c9f491fa36a0893e2e0c09ac06ef112b34dfc96 Mon Sep 17 00:00:00 2001 From: JongChan Choi Date: Sun, 19 Jul 2015 17:25:45 +0900 Subject: [PATCH] Parse 8, 4 digits hex notations --- src/color.rs | 22 ++++++++++++++++++++++ src/css-parsing-tests/color3.json | 6 ++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/color.rs b/src/color.rs index 47101c8b..be52885d 100644 --- a/src/color.rs +++ b/src/color.rs @@ -86,6 +86,16 @@ fn rgb(red: f32, green: f32, blue: f32) -> Result { })) } +#[inline] +fn rgba(red: f32, green: f32, blue: f32, alpha: f32) -> Result { + Ok(Color::RGBA(RGBA { + red: red / 255., + green: green / 255., + blue: blue / 255., + alpha: alpha / 255., + })) +} + /// Return the named color with the given name. /// @@ -267,11 +277,23 @@ fn from_hex(c: u8) -> Result { fn parse_color_hash(value: &str) -> Result { 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, diff --git a/src/css-parsing-tests/color3.json b/src/css-parsing-tests/color3.json index 04978564..bbc05a4e 100644 --- a/src/css-parsing-tests/color3.json +++ b/src/css-parsing-tests/color3.json @@ -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],