Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "cssparser"
version = "0.13.3"
version = "0.13.4"
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
66 changes: 34 additions & 32 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ impl Color {
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
pub fn parse(input: &mut Parser) -> Result<Color, ()> {
match try!(input.next()) {
Token::Hash(value) | Token::IDHash(value) => parse_color_hash(&*value),
Token::Hash(value) | Token::IDHash(value) => {
Color::parse_hash(value.as_bytes())
},
Token::Ident(value) => parse_color_keyword(&*value),
Token::Function(name) => {
input.parse_nested_block(|arguments| {
Expand All @@ -153,6 +155,37 @@ impl Color {
_ => Err(())
}
}

/// Parse a color hash, without the leading '#' character.
#[inline]
fn parse_hash(value: &[u8]) -> Result<Self, ()> {
match value.len() {
8 => rgba(
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7])),
),
6 => rgb(
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
),
4 => rgba(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17,
try!(from_hex(value[3])) * 17,
),
3 => rgb(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17,
),
_ => Err(())
}
}

}


Expand Down Expand Up @@ -354,37 +387,6 @@ fn from_hex(c: u8) -> Result<u8, ()> {
}
}


#[inline]
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])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7])),
),
6 => rgb(
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
),
4 => rgba(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17,
try!(from_hex(value[3])) * 17,
),
3 => rgb(
try!(from_hex(value[0])) * 17,
try!(from_hex(value[1])) * 17,
try!(from_hex(value[2])) * 17,
),
_ => Err(())
}
}

fn clamp_unit_f32(val: f32) -> u8 {
// Whilst scaling by 256 and flooring would provide
// an equal distribution of integers to percentage inputs,
Expand Down