Refactor hex parsing
This commit is contained in:
parent
a139e07189
commit
3404073ed8
@ -10,6 +10,42 @@ use crate::utils::{devour_whitespace_or_comment, parse_interpolation};
|
|||||||
use crate::value::Value;
|
use crate::value::Value;
|
||||||
use crate::{Token, TokenKind};
|
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 {
|
impl Value {
|
||||||
pub fn from_tokens<I: Iterator<Item = Token>>(
|
pub fn from_tokens<I: Iterator<Item = Token>>(
|
||||||
toks: &mut Peekable<I>,
|
toks: &mut Peekable<I>,
|
||||||
@ -139,64 +175,7 @@ impl Value {
|
|||||||
_ => break,
|
_ => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(parse_hex(s))
|
||||||
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)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// TokenKind::Interpolation => {
|
// TokenKind::Interpolation => {
|
||||||
// Some(Value::Ident(
|
// Some(Value::Ident(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user