diff --git a/src/value/parse.rs b/src/value/parse.rs index f8e5e66..1f18ceb 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -10,6 +10,42 @@ use crate::utils::{devour_whitespace_or_comment, parse_interpolation}; use crate::value::Value; use crate::{Token, TokenKind}; +fn parse_hex(s: String) -> Value { + match s.len() { + 3 => { + let v = u16::from_str_radix(&s, 16).unwrap(); + let red = ((v & 0xf00) >> 8) * 0x11; + let green = ((v & 0x0f0) >> 4) * 0x11; + let blue = (v & 0x00f) * 0x11; + Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))) + } + 4 => { + let v = u16::from_str_radix(&s, 16).unwrap(); + let red = ((v & 0xf000) >> 12) * 0x11; + let green = ((v & 0x0f00) >> 8) * 0x11; + let blue = ((v & 0x00f0) >> 4) * 0x11; + let alpha = (v & 0x000f) * 0x11; + Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s))) + } + 6 => { + let v = u32::from_str_radix(&s, 16).unwrap(); + let red: u16 = ((v & 0xff0000) >> 16) as u16; + let green: u16 = ((v & 0x00ff00) >> 8) as u16; + let blue: u16 = (v & 0x0000ff) as u16; + Value::Color(Color::new(red, green, blue, 1, format!("#{}", s))) + } + 8 => { + let v = u32::from_str_radix(&s, 16).unwrap(); + let red = ((v & 0xff000000) >> 24) as u16; + let green = ((v & 0x00ff0000) >> 16) as u16; + let blue = ((v & 0x0000ff00) >> 8) as u16; + let alpha = (v & 0x000000ff) as u16; + Value::Color(Color::new(red, green, blue, alpha, format!("#{}", s))) + } + _ => Value::Ident(s, QuoteKind::None), + } +} + impl Value { pub fn from_tokens>( toks: &mut Peekable, @@ -139,64 +175,7 @@ impl Value { _ => break, } } - - match s.len() { - 3 => { - let v = u16::from_str_radix(&s, 16).unwrap(); - let red = ((v & 0xf00) >> 8) * 0x11; - let green = ((v & 0x0f0) >> 4) * 0x11; - let blue = (v & 0x00f) * 0x11; - Some(Value::Color(Color::new( - red, - green, - blue, - 1, - format!("#{}", s), - ))) - } - 4 => { - let v = u16::from_str_radix(&s, 16).unwrap(); - let red = ((v & 0xf000) >> 12) * 0x11; - let green = ((v & 0x0f00) >> 8) * 0x11; - let blue = ((v & 0x00f0) >> 4) * 0x11; - let alpha = (v & 0x000f) * 0x11; - Some(Value::Color(Color::new( - red, - green, - blue, - alpha, - format!("#{}", s), - ))) - } - 6 => { - let v = u32::from_str_radix(&s, 16).unwrap(); - let red: u16 = ((v & 0xff0000) >> 16) as u16; - let green: u16 = ((v & 0x00ff00) >> 8) as u16; - let blue: u16 = (v & 0x0000ff) as u16; - Some(Value::Color(Color::new( - red, - green, - blue, - 1, - format!("#{}", s), - ))) - } - 8 => { - let v = u32::from_str_radix(&s, 16).unwrap(); - let red = ((v & 0xff000000) >> 24) as u16; - let green = ((v & 0x00ff0000) >> 16) as u16; - let blue = ((v & 0x0000ff00) >> 8) as u16; - let alpha = (v & 0x000000ff) as u16; - Some(Value::Color(Color::new( - red, - green, - blue, - alpha, - format!("#{}", s), - ))) - } - _ => Some(Value::Ident(s, QuoteKind::None)), - } + Some(parse_hex(s)) } // TokenKind::Interpolation => { // Some(Value::Ident(