Skip to content

Commit 1c0b3ff

Browse files
author
bors-servo
authored
Auto merge of #146 - nox:parse-hash, r=SimonSapin
Introduce Color::parse_hash Useful to parse quirky colors. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/146) <!-- Reviewable:end -->
2 parents 1ea913d + e557dd7 commit 1c0b3ff

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "cssparser"
4-
version = "0.13.3"
4+
version = "0.13.4"
55
authors = [ "Simon Sapin <simon.sapin@exyr.org>" ]
66

77
description = "Rust implementation of CSS Syntax Level 3"

src/color.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ impl Color {
143143
/// FIXME(#2) Deprecated CSS2 System Colors are not supported yet.
144144
pub fn parse(input: &mut Parser) -> Result<Color, ()> {
145145
match try!(input.next()) {
146-
Token::Hash(value) | Token::IDHash(value) => parse_color_hash(&*value),
146+
Token::Hash(value) | Token::IDHash(value) => {
147+
Color::parse_hash(value.as_bytes())
148+
},
147149
Token::Ident(value) => parse_color_keyword(&*value),
148150
Token::Function(name) => {
149151
input.parse_nested_block(|arguments| {
@@ -153,6 +155,37 @@ impl Color {
153155
_ => Err(())
154156
}
155157
}
158+
159+
/// Parse a color hash, without the leading '#' character.
160+
#[inline]
161+
fn parse_hash(value: &[u8]) -> Result<Self, ()> {
162+
match value.len() {
163+
8 => rgba(
164+
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
165+
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
166+
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
167+
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7])),
168+
),
169+
6 => rgb(
170+
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
171+
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
172+
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
173+
),
174+
4 => rgba(
175+
try!(from_hex(value[0])) * 17,
176+
try!(from_hex(value[1])) * 17,
177+
try!(from_hex(value[2])) * 17,
178+
try!(from_hex(value[3])) * 17,
179+
),
180+
3 => rgb(
181+
try!(from_hex(value[0])) * 17,
182+
try!(from_hex(value[1])) * 17,
183+
try!(from_hex(value[2])) * 17,
184+
),
185+
_ => Err(())
186+
}
187+
}
188+
156189
}
157190

158191

@@ -354,37 +387,6 @@ fn from_hex(c: u8) -> Result<u8, ()> {
354387
}
355388
}
356389

357-
358-
#[inline]
359-
fn parse_color_hash(value: &str) -> Result<Color, ()> {
360-
let value = value.as_bytes();
361-
match value.len() {
362-
8 => rgba(
363-
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
364-
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
365-
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
366-
try!(from_hex(value[6])) * 16 + try!(from_hex(value[7])),
367-
),
368-
6 => rgb(
369-
try!(from_hex(value[0])) * 16 + try!(from_hex(value[1])),
370-
try!(from_hex(value[2])) * 16 + try!(from_hex(value[3])),
371-
try!(from_hex(value[4])) * 16 + try!(from_hex(value[5])),
372-
),
373-
4 => rgba(
374-
try!(from_hex(value[0])) * 17,
375-
try!(from_hex(value[1])) * 17,
376-
try!(from_hex(value[2])) * 17,
377-
try!(from_hex(value[3])) * 17,
378-
),
379-
3 => rgb(
380-
try!(from_hex(value[0])) * 17,
381-
try!(from_hex(value[1])) * 17,
382-
try!(from_hex(value[2])) * 17,
383-
),
384-
_ => Err(())
385-
}
386-
}
387-
388390
fn clamp_unit_f32(val: f32) -> u8 {
389391
// Whilst scaling by 256 and flooring would provide
390392
// an equal distribution of integers to percentage inputs,

0 commit comments

Comments
 (0)