From c5bc6062a03b8b053d17c09272d4ff46b770a382 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 8 Feb 2020 08:48:31 -0500 Subject: [PATCH] Implement basic color parsing --- src/color/mod.rs | 55 ++++----- src/color/name.rs | 306 +++++++++++++++++++++++----------------------- src/lexer.rs | 6 +- src/value.rs | 83 +++++++++++++ tests/color.rs | 33 +++++ 5 files changed, 298 insertions(+), 185 deletions(-) create mode 100644 tests/color.rs diff --git a/src/color/mod.rs b/src/color/mod.rs index 20a630f..3d10571 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -4,47 +4,44 @@ pub(crate) use name::ColorName; mod name; -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Eq, PartialEq)] pub(crate) struct Color { red: u16, green: u16, blue: u16, alpha: u16, - repr: ColorRepr, + repr: String, } impl Color { - pub const fn new(red: u16, green: u16, blue: u16, alpha: u16, repr: ColorRepr) -> Self { - Color { red, green, blue, alpha, repr } + pub const fn new(red: u16, green: u16, blue: u16, alpha: u16, repr: String) -> Self { + Color { + red, + green, + blue, + alpha, + repr, + } + } + + pub fn from_values(red: u16, green: u16, blue: u16, alpha: u16) -> Self { + let repr = if alpha >= 1 { + format!("#{:X}{:X}{:X}", red, green, blue) + } else { + format!("#{:X}{:X}{:X}{:X}", red, green, blue, alpha) + }; + Color { + red, + green, + blue, + alpha, + repr, + } } } impl Display for Color { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.repr { - ColorRepr::Name(n) => write!(f, "{}", n), - ColorRepr::Hex3 => todo!(), //write!(f, "#{}{}{}", self.red, self.green, self.blue), - ColorRepr::Hex6 => write!(f, "#{:X}{:X}{:X}", self.red, self.green, self.blue), - ColorRepr::Hex8 => write!( - f, - "#{:X}{:X}{:X}{:X}", - self.red, self.green, self.blue, self.alpha - ), - ColorRepr::Function if self.alpha < 1 => write!( - f, - "rgba({}, {}, {}, {})", - self.red, self.green, self.blue, self.alpha - ), - ColorRepr::Function => write!(f, "#{:X}{:X}{:X}", self.red, self.green, self.blue), - } + write!(f, "{}", self.repr) } } - -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub(crate) enum ColorRepr { - Name(ColorName), - Hex3, - Hex6, - Hex8, - Function, -} diff --git a/src/color/name.rs b/src/color/name.rs index 49c3046..80e50c4 100644 --- a/src/color/name.rs +++ b/src/color/name.rs @@ -1,7 +1,7 @@ -use std::convert::{Into, TryFrom}; +use std::convert::TryFrom; use std::fmt::{self, Display}; -use super::{Color, ColorRepr}; +use super::Color; #[derive(Debug, Clone, Copy, Eq, PartialEq)] pub(crate) enum ColorName { @@ -470,157 +470,157 @@ impl TryFrom<&str> for ColorName { } } -impl Into for ColorName { - fn into(self) -> Color { +impl ColorName { + pub fn into_color(self, repr: String) -> Color { match self { - Self::AliceBlue => Color::new(0xF0, 0xF8, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::AntiqueWhite => Color::new(0xFA, 0xEB, 0xD7, 0xFF, ColorRepr::Name(self)), - Self::Aqua => Color::new(0x00, 0xFF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::Aquamarine => Color::new(0x7F, 0xFF, 0xD4, 0xFF, ColorRepr::Name(self)), - Self::Azure => Color::new(0xF0, 0xFF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::Beige => Color::new(0xF5, 0xF5, 0xDC, 0xFF, ColorRepr::Name(self)), - Self::Bisque => Color::new(0xFF, 0xE4, 0xC4, 0xFF, ColorRepr::Name(self)), - Self::Black => Color::new(0x00, 0x00, 0x00, 0xFF, ColorRepr::Name(self)), - Self::BlanchedAlmond => Color::new(0xFF, 0xEB, 0xCD, 0xFF, ColorRepr::Name(self)), - Self::Blue => Color::new(0x00, 0x00, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::BlueViolet => Color::new(0x8A, 0x2B, 0xE2, 0xFF, ColorRepr::Name(self)), - Self::Brown => Color::new(0xA5, 0x2A, 0x2A, 0xFF, ColorRepr::Name(self)), - Self::BurlyWood => Color::new(0xDE, 0xB8, 0x87, 0xFF, ColorRepr::Name(self)), - Self::CadetBlue => Color::new(0x5F, 0x9E, 0xA0, 0xFF, ColorRepr::Name(self)), - Self::Chartreuse => Color::new(0x7F, 0xFF, 0x00, 0xFF, ColorRepr::Name(self)), - Self::Chocolate => Color::new(0xD2, 0x69, 0x1E, 0xFF, ColorRepr::Name(self)), - Self::Coral => Color::new(0xFF, 0x7F, 0x50, 0xFF, ColorRepr::Name(self)), - Self::CornflowerBlue => Color::new(0x64, 0x95, 0xED, 0xFF, ColorRepr::Name(self)), - Self::Cornsilk => Color::new(0xFF, 0xF8, 0xDC, 0xFF, ColorRepr::Name(self)), - Self::Crimson => Color::new(0xDC, 0x14, 0x3C, 0xFF, ColorRepr::Name(self)), - Self::Cyan => Color::new(0x00, 0xFF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::DarkBlue => Color::new(0x00, 0x00, 0x8B, 0xFF, ColorRepr::Name(self)), - Self::DarkCyan => Color::new(0x00, 0x8B, 0x8B, 0xFF, ColorRepr::Name(self)), - Self::DarkGoldenRod => Color::new(0xB8, 0x86, 0x0B, 0xFF, ColorRepr::Name(self)), - Self::DarkGray => Color::new(0xA9, 0xA9, 0xA9, 0xFF, ColorRepr::Name(self)), - Self::DarkGrey => Color::new(0xA9, 0xA9, 0xA9, 0xFF, ColorRepr::Name(self)), - Self::DarkGreen => Color::new(0x00, 0x64, 0x00, 0xFF, ColorRepr::Name(self)), - Self::DarkKhaki => Color::new(0xBD, 0xB7, 0x6B, 0xFF, ColorRepr::Name(self)), - Self::DarkMagenta => Color::new(0x8B, 0x00, 0x8B, 0xFF, ColorRepr::Name(self)), - Self::DarkOliveGreen => Color::new(0x55, 0x6B, 0x2F, 0xFF, ColorRepr::Name(self)), - Self::DarkOrange => Color::new(0xFF, 0x8C, 0x00, 0xFF, ColorRepr::Name(self)), - Self::DarkOrchid => Color::new(0x99, 0x32, 0xCC, 0xFF, ColorRepr::Name(self)), - Self::DarkRed => Color::new(0x8B, 0x00, 0x00, 0xFF, ColorRepr::Name(self)), - Self::DarkSalmon => Color::new(0xE9, 0x96, 0x7A, 0xFF, ColorRepr::Name(self)), - Self::DarkSeaGreen => Color::new(0x8F, 0xBC, 0x8F, 0xFF, ColorRepr::Name(self)), - Self::DarkSlateBlue => Color::new(0x48, 0x3D, 0x8B, 0xFF, ColorRepr::Name(self)), - Self::DarkSlateGray => Color::new(0x2F, 0x4F, 0x4F, 0xFF, ColorRepr::Name(self)), - Self::DarkSlateGrey => Color::new(0x2F, 0x4F, 0x4F, 0xFF, ColorRepr::Name(self)), - Self::DarkTurquoise => Color::new(0x00, 0xCE, 0xD1, 0xFF, ColorRepr::Name(self)), - Self::DarkViolet => Color::new(0x94, 0x00, 0xD3, 0xFF, ColorRepr::Name(self)), - Self::DeepPink => Color::new(0xFF, 0x14, 0x93, 0xFF, ColorRepr::Name(self)), - Self::DeepSkyBlue => Color::new(0x00, 0xBF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::DimGray => Color::new(0x69, 0x69, 0x69, 0xFF, ColorRepr::Name(self)), - Self::DimGrey => Color::new(0x69, 0x69, 0x69, 0xFF, ColorRepr::Name(self)), - Self::DodgerBlue => Color::new(0x1E, 0x90, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::FireBrick => Color::new(0xB2, 0x22, 0x22, 0xFF, ColorRepr::Name(self)), - Self::FloralWhite => Color::new(0xFF, 0xFA, 0xF0, 0xFF, ColorRepr::Name(self)), - Self::ForestGreen => Color::new(0x22, 0x8B, 0x22, 0xFF, ColorRepr::Name(self)), - Self::Fuchsia => Color::new(0xFF, 0x00, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::Gainsboro => Color::new(0xDC, 0xDC, 0xDC, 0xFF, ColorRepr::Name(self)), - Self::GhostWhite => Color::new(0xF8, 0xF8, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::Gold => Color::new(0xFF, 0xD7, 0x00, 0xFF, ColorRepr::Name(self)), - Self::GoldenRod => Color::new(0xDA, 0xA5, 0x20, 0xFF, ColorRepr::Name(self)), - Self::Gray => Color::new(0x80, 0x80, 0x80, 0xFF, ColorRepr::Name(self)), - Self::Grey => Color::new(0x80, 0x80, 0x80, 0xFF, ColorRepr::Name(self)), - Self::Green => Color::new(0x00, 0x80, 0x00, 0xFF, ColorRepr::Name(self)), - Self::GreenYellow => Color::new(0xAD, 0xFF, 0x2F, 0xFF, ColorRepr::Name(self)), - Self::HoneyDew => Color::new(0xF0, 0xFF, 0xF0, 0xFF, ColorRepr::Name(self)), - Self::HotPink => Color::new(0xFF, 0x69, 0xB4, 0xFF, ColorRepr::Name(self)), - Self::IndianRed => Color::new(0xCD, 0x5C, 0x5C, 0xFF, ColorRepr::Name(self)), - Self::Indigo => Color::new(0x4B, 0x00, 0x82, 0xFF, ColorRepr::Name(self)), - Self::Ivory => Color::new(0xFF, 0xFF, 0xF0, 0xFF, ColorRepr::Name(self)), - Self::Khaki => Color::new(0xF0, 0xE6, 0x8C, 0xFF, ColorRepr::Name(self)), - Self::Lavender => Color::new(0xE6, 0xE6, 0xFA, 0xFF, ColorRepr::Name(self)), - Self::LavenderBlush => Color::new(0xFF, 0xF0, 0xF5, 0xFF, ColorRepr::Name(self)), - Self::LawnGreen => Color::new(0x7C, 0xFC, 0x00, 0xFF, ColorRepr::Name(self)), - Self::LemonChiffon => Color::new(0xFF, 0xFA, 0xCD, 0xFF, ColorRepr::Name(self)), - Self::LightBlue => Color::new(0xAD, 0xD8, 0xE6, 0xFF, ColorRepr::Name(self)), - Self::LightCoral => Color::new(0xF0, 0x80, 0x80, 0xFF, ColorRepr::Name(self)), - Self::LightCyan => Color::new(0xE0, 0xFF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::LightGoldenRodYellow => Color::new(0xFA, 0xFA, 0xD2, 0xFF, ColorRepr::Name(self)), - Self::LightGray => Color::new(0xD3, 0xD3, 0xD3, 0xFF, ColorRepr::Name(self)), - Self::LightGrey => Color::new(0xD3, 0xD3, 0xD3, 0xFF, ColorRepr::Name(self)), - Self::LightGreen => Color::new(0x90, 0xEE, 0x90, 0xFF, ColorRepr::Name(self)), - Self::LightPink => Color::new(0xFF, 0xB6, 0xC1, 0xFF, ColorRepr::Name(self)), - Self::LightSalmon => Color::new(0xFF, 0xA0, 0x7A, 0xFF, ColorRepr::Name(self)), - Self::LightSeaGreen => Color::new(0x20, 0xB2, 0xAA, 0xFF, ColorRepr::Name(self)), - Self::LightSkyBlue => Color::new(0x87, 0xCE, 0xFA, 0xFF, ColorRepr::Name(self)), - Self::LightSlateGray => Color::new(0x77, 0x88, 0x99, 0xFF, ColorRepr::Name(self)), - Self::LightSlateGrey => Color::new(0x77, 0x88, 0x99, 0xFF, ColorRepr::Name(self)), - Self::LightSteelBlue => Color::new(0xB0, 0xC4, 0xDE, 0xFF, ColorRepr::Name(self)), - Self::LightYellow => Color::new(0xFF, 0xFF, 0xE0, 0xFF, ColorRepr::Name(self)), - Self::Lime => Color::new(0x00, 0xFF, 0x00, 0xFF, ColorRepr::Name(self)), - Self::LimeGreen => Color::new(0x32, 0xCD, 0x32, 0xFF, ColorRepr::Name(self)), - Self::Linen => Color::new(0xFA, 0xF0, 0xE6, 0xFF, ColorRepr::Name(self)), - Self::Magenta => Color::new(0xFF, 0x00, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::Maroon => Color::new(0x80, 0x00, 0x00, 0xFF, ColorRepr::Name(self)), - Self::MediumAquaMarine => Color::new(0x66, 0xCD, 0xAA, 0xFF, ColorRepr::Name(self)), - Self::MediumBlue => Color::new(0x00, 0x00, 0xCD, 0xFF, ColorRepr::Name(self)), - Self::MediumOrchid => Color::new(0xBA, 0x55, 0xD3, 0xFF, ColorRepr::Name(self)), - Self::MediumPurple => Color::new(0x93, 0x70, 0xDB, 0xFF, ColorRepr::Name(self)), - Self::MediumSeaGreen => Color::new(0x3C, 0xB3, 0x71, 0xFF, ColorRepr::Name(self)), - Self::MediumSlateBlue => Color::new(0x7B, 0x68, 0xEE, 0xFF, ColorRepr::Name(self)), - Self::MediumSpringGreen => Color::new(0x00, 0xFA, 0x9A, 0xFF, ColorRepr::Name(self)), - Self::MediumTurquoise => Color::new(0x48, 0xD1, 0xCC, 0xFF, ColorRepr::Name(self)), - Self::MediumVioletRed => Color::new(0xC7, 0x15, 0x85, 0xFF, ColorRepr::Name(self)), - Self::MidnightBlue => Color::new(0x19, 0x19, 0x70, 0xFF, ColorRepr::Name(self)), - Self::MintCream => Color::new(0xF5, 0xFF, 0xFA, 0xFF, ColorRepr::Name(self)), - Self::MistyRose => Color::new(0xFF, 0xE4, 0xE1, 0xFF, ColorRepr::Name(self)), - Self::Moccasin => Color::new(0xFF, 0xE4, 0xB5, 0xFF, ColorRepr::Name(self)), - Self::NavajoWhite => Color::new(0xFF, 0xDE, 0xAD, 0xFF, ColorRepr::Name(self)), - Self::Navy => Color::new(0x00, 0x00, 0x80, 0xFF, ColorRepr::Name(self)), - Self::OldLace => Color::new(0xFD, 0xF5, 0xE6, 0xFF, ColorRepr::Name(self)), - Self::Olive => Color::new(0x80, 0x80, 0x00, 0xFF, ColorRepr::Name(self)), - Self::OliveDrab => Color::new(0x6B, 0x8E, 0x23, 0xFF, ColorRepr::Name(self)), - Self::Orange => Color::new(0xFF, 0xA5, 0x00, 0xFF, ColorRepr::Name(self)), - Self::OrangeRed => Color::new(0xFF, 0x45, 0x00, 0xFF, ColorRepr::Name(self)), - Self::Orchid => Color::new(0xDA, 0x70, 0xD6, 0xFF, ColorRepr::Name(self)), - Self::PaleGoldenRod => Color::new(0xEE, 0xE8, 0xAA, 0xFF, ColorRepr::Name(self)), - Self::PaleGreen => Color::new(0x98, 0xFB, 0x98, 0xFF, ColorRepr::Name(self)), - Self::PaleTurquoise => Color::new(0xAF, 0xEE, 0xEE, 0xFF, ColorRepr::Name(self)), - Self::PaleVioletRed => Color::new(0xDB, 0x70, 0x93, 0xFF, ColorRepr::Name(self)), - Self::PapayaWhip => Color::new(0xFF, 0xEF, 0xD5, 0xFF, ColorRepr::Name(self)), - Self::PeachPuff => Color::new(0xFF, 0xDA, 0xB9, 0xFF, ColorRepr::Name(self)), - Self::Peru => Color::new(0xCD, 0x85, 0x3F, 0xFF, ColorRepr::Name(self)), - Self::Pink => Color::new(0xFF, 0xC0, 0xCB, 0xFF, ColorRepr::Name(self)), - Self::Plum => Color::new(0xDD, 0xA0, 0xDD, 0xFF, ColorRepr::Name(self)), - Self::PowderBlue => Color::new(0xB0, 0xE0, 0xE6, 0xFF, ColorRepr::Name(self)), - Self::Purple => Color::new(0x80, 0x00, 0x80, 0xFF, ColorRepr::Name(self)), - Self::RebeccaPurple => Color::new(0x66, 0x33, 0x99, 0xFF, ColorRepr::Name(self)), - Self::Red => Color::new(0xFF, 0x00, 0x00, 0xFF, ColorRepr::Name(self)), - Self::RosyBrown => Color::new(0xBC, 0x8F, 0x8F, 0xFF, ColorRepr::Name(self)), - Self::RoyalBlue => Color::new(0x41, 0x69, 0xE1, 0xFF, ColorRepr::Name(self)), - Self::SaddleBrown => Color::new(0x8B, 0x45, 0x13, 0xFF, ColorRepr::Name(self)), - Self::Salmon => Color::new(0xFA, 0x80, 0x72, 0xFF, ColorRepr::Name(self)), - Self::SandyBrown => Color::new(0xF4, 0xA4, 0x60, 0xFF, ColorRepr::Name(self)), - Self::SeaGreen => Color::new(0x2E, 0x8B, 0x57, 0xFF, ColorRepr::Name(self)), - Self::SeaShell => Color::new(0xFF, 0xF5, 0xEE, 0xFF, ColorRepr::Name(self)), - Self::Sienna => Color::new(0xA0, 0x52, 0x2D, 0xFF, ColorRepr::Name(self)), - Self::Silver => Color::new(0xC0, 0xC0, 0xC0, 0xFF, ColorRepr::Name(self)), - Self::SkyBlue => Color::new(0x87, 0xCE, 0xEB, 0xFF, ColorRepr::Name(self)), - Self::SlateBlue => Color::new(0x6A, 0x5A, 0xCD, 0xFF, ColorRepr::Name(self)), - Self::SlateGray => Color::new(0x70, 0x80, 0x90, 0xFF, ColorRepr::Name(self)), - Self::SlateGrey => Color::new(0x70, 0x80, 0x90, 0xFF, ColorRepr::Name(self)), - Self::Snow => Color::new(0xFF, 0xFA, 0xFA, 0xFF, ColorRepr::Name(self)), - Self::SpringGreen => Color::new(0x00, 0xFF, 0x7F, 0xFF, ColorRepr::Name(self)), - Self::SteelBlue => Color::new(0x46, 0x82, 0xB4, 0xFF, ColorRepr::Name(self)), - Self::Tan => Color::new(0xD2, 0xB4, 0x8C, 0xFF, ColorRepr::Name(self)), - Self::Teal => Color::new(0x00, 0x80, 0x80, 0xFF, ColorRepr::Name(self)), - Self::Thistle => Color::new(0xD8, 0xBF, 0xD8, 0xFF, ColorRepr::Name(self)), - Self::Tomato => Color::new(0xFF, 0x63, 0x47, 0xFF, ColorRepr::Name(self)), - Self::Turquoise => Color::new(0x40, 0xE0, 0xD0, 0xFF, ColorRepr::Name(self)), - Self::Violet => Color::new(0xEE, 0x82, 0xEE, 0xFF, ColorRepr::Name(self)), - Self::Wheat => Color::new(0xF5, 0xDE, 0xB3, 0xFF, ColorRepr::Name(self)), - Self::White => Color::new(0xFF, 0xFF, 0xFF, 0xFF, ColorRepr::Name(self)), - Self::WhiteSmoke => Color::new(0xF5, 0xF5, 0xF5, 0xFF, ColorRepr::Name(self)), - Self::Yellow => Color::new(0xFF, 0xFF, 0x00, 0xFF, ColorRepr::Name(self)), - Self::YellowGreen => Color::new(0x9A, 0xCD, 0x32, 0xFF, ColorRepr::Name(self)), + Self::AliceBlue => Color::new(0xF0, 0xF8, 0xFF, 0xFF, repr), + Self::AntiqueWhite => Color::new(0xFA, 0xEB, 0xD7, 0xFF, repr), + Self::Aqua => Color::new(0x00, 0xFF, 0xFF, 0xFF, repr), + Self::Aquamarine => Color::new(0x7F, 0xFF, 0xD4, 0xFF, repr), + Self::Azure => Color::new(0xF0, 0xFF, 0xFF, 0xFF, repr), + Self::Beige => Color::new(0xF5, 0xF5, 0xDC, 0xFF, repr), + Self::Bisque => Color::new(0xFF, 0xE4, 0xC4, 0xFF, repr), + Self::Black => Color::new(0x00, 0x00, 0x00, 0xFF, repr), + Self::BlanchedAlmond => Color::new(0xFF, 0xEB, 0xCD, 0xFF, repr), + Self::Blue => Color::new(0x00, 0x00, 0xFF, 0xFF, repr), + Self::BlueViolet => Color::new(0x8A, 0x2B, 0xE2, 0xFF, repr), + Self::Brown => Color::new(0xA5, 0x2A, 0x2A, 0xFF, repr), + Self::BurlyWood => Color::new(0xDE, 0xB8, 0x87, 0xFF, repr), + Self::CadetBlue => Color::new(0x5F, 0x9E, 0xA0, 0xFF, repr), + Self::Chartreuse => Color::new(0x7F, 0xFF, 0x00, 0xFF, repr), + Self::Chocolate => Color::new(0xD2, 0x69, 0x1E, 0xFF, repr), + Self::Coral => Color::new(0xFF, 0x7F, 0x50, 0xFF, repr), + Self::CornflowerBlue => Color::new(0x64, 0x95, 0xED, 0xFF, repr), + Self::Cornsilk => Color::new(0xFF, 0xF8, 0xDC, 0xFF, repr), + Self::Crimson => Color::new(0xDC, 0x14, 0x3C, 0xFF, repr), + Self::Cyan => Color::new(0x00, 0xFF, 0xFF, 0xFF, repr), + Self::DarkBlue => Color::new(0x00, 0x00, 0x8B, 0xFF, repr), + Self::DarkCyan => Color::new(0x00, 0x8B, 0x8B, 0xFF, repr), + Self::DarkGoldenRod => Color::new(0xB8, 0x86, 0x0B, 0xFF, repr), + Self::DarkGray => Color::new(0xA9, 0xA9, 0xA9, 0xFF, repr), + Self::DarkGrey => Color::new(0xA9, 0xA9, 0xA9, 0xFF, repr), + Self::DarkGreen => Color::new(0x00, 0x64, 0x00, 0xFF, repr), + Self::DarkKhaki => Color::new(0xBD, 0xB7, 0x6B, 0xFF, repr), + Self::DarkMagenta => Color::new(0x8B, 0x00, 0x8B, 0xFF, repr), + Self::DarkOliveGreen => Color::new(0x55, 0x6B, 0x2F, 0xFF, repr), + Self::DarkOrange => Color::new(0xFF, 0x8C, 0x00, 0xFF, repr), + Self::DarkOrchid => Color::new(0x99, 0x32, 0xCC, 0xFF, repr), + Self::DarkRed => Color::new(0x8B, 0x00, 0x00, 0xFF, repr), + Self::DarkSalmon => Color::new(0xE9, 0x96, 0x7A, 0xFF, repr), + Self::DarkSeaGreen => Color::new(0x8F, 0xBC, 0x8F, 0xFF, repr), + Self::DarkSlateBlue => Color::new(0x48, 0x3D, 0x8B, 0xFF, repr), + Self::DarkSlateGray => Color::new(0x2F, 0x4F, 0x4F, 0xFF, repr), + Self::DarkSlateGrey => Color::new(0x2F, 0x4F, 0x4F, 0xFF, repr), + Self::DarkTurquoise => Color::new(0x00, 0xCE, 0xD1, 0xFF, repr), + Self::DarkViolet => Color::new(0x94, 0x00, 0xD3, 0xFF, repr), + Self::DeepPink => Color::new(0xFF, 0x14, 0x93, 0xFF, repr), + Self::DeepSkyBlue => Color::new(0x00, 0xBF, 0xFF, 0xFF, repr), + Self::DimGray => Color::new(0x69, 0x69, 0x69, 0xFF, repr), + Self::DimGrey => Color::new(0x69, 0x69, 0x69, 0xFF, repr), + Self::DodgerBlue => Color::new(0x1E, 0x90, 0xFF, 0xFF, repr), + Self::FireBrick => Color::new(0xB2, 0x22, 0x22, 0xFF, repr), + Self::FloralWhite => Color::new(0xFF, 0xFA, 0xF0, 0xFF, repr), + Self::ForestGreen => Color::new(0x22, 0x8B, 0x22, 0xFF, repr), + Self::Fuchsia => Color::new(0xFF, 0x00, 0xFF, 0xFF, repr), + Self::Gainsboro => Color::new(0xDC, 0xDC, 0xDC, 0xFF, repr), + Self::GhostWhite => Color::new(0xF8, 0xF8, 0xFF, 0xFF, repr), + Self::Gold => Color::new(0xFF, 0xD7, 0x00, 0xFF, repr), + Self::GoldenRod => Color::new(0xDA, 0xA5, 0x20, 0xFF, repr), + Self::Gray => Color::new(0x80, 0x80, 0x80, 0xFF, repr), + Self::Grey => Color::new(0x80, 0x80, 0x80, 0xFF, repr), + Self::Green => Color::new(0x00, 0x80, 0x00, 0xFF, repr), + Self::GreenYellow => Color::new(0xAD, 0xFF, 0x2F, 0xFF, repr), + Self::HoneyDew => Color::new(0xF0, 0xFF, 0xF0, 0xFF, repr), + Self::HotPink => Color::new(0xFF, 0x69, 0xB4, 0xFF, repr), + Self::IndianRed => Color::new(0xCD, 0x5C, 0x5C, 0xFF, repr), + Self::Indigo => Color::new(0x4B, 0x00, 0x82, 0xFF, repr), + Self::Ivory => Color::new(0xFF, 0xFF, 0xF0, 0xFF, repr), + Self::Khaki => Color::new(0xF0, 0xE6, 0x8C, 0xFF, repr), + Self::Lavender => Color::new(0xE6, 0xE6, 0xFA, 0xFF, repr), + Self::LavenderBlush => Color::new(0xFF, 0xF0, 0xF5, 0xFF, repr), + Self::LawnGreen => Color::new(0x7C, 0xFC, 0x00, 0xFF, repr), + Self::LemonChiffon => Color::new(0xFF, 0xFA, 0xCD, 0xFF, repr), + Self::LightBlue => Color::new(0xAD, 0xD8, 0xE6, 0xFF, repr), + Self::LightCoral => Color::new(0xF0, 0x80, 0x80, 0xFF, repr), + Self::LightCyan => Color::new(0xE0, 0xFF, 0xFF, 0xFF, repr), + Self::LightGoldenRodYellow => Color::new(0xFA, 0xFA, 0xD2, 0xFF, repr), + Self::LightGray => Color::new(0xD3, 0xD3, 0xD3, 0xFF, repr), + Self::LightGrey => Color::new(0xD3, 0xD3, 0xD3, 0xFF, repr), + Self::LightGreen => Color::new(0x90, 0xEE, 0x90, 0xFF, repr), + Self::LightPink => Color::new(0xFF, 0xB6, 0xC1, 0xFF, repr), + Self::LightSalmon => Color::new(0xFF, 0xA0, 0x7A, 0xFF, repr), + Self::LightSeaGreen => Color::new(0x20, 0xB2, 0xAA, 0xFF, repr), + Self::LightSkyBlue => Color::new(0x87, 0xCE, 0xFA, 0xFF, repr), + Self::LightSlateGray => Color::new(0x77, 0x88, 0x99, 0xFF, repr), + Self::LightSlateGrey => Color::new(0x77, 0x88, 0x99, 0xFF, repr), + Self::LightSteelBlue => Color::new(0xB0, 0xC4, 0xDE, 0xFF, repr), + Self::LightYellow => Color::new(0xFF, 0xFF, 0xE0, 0xFF, repr), + Self::Lime => Color::new(0x00, 0xFF, 0x00, 0xFF, repr), + Self::LimeGreen => Color::new(0x32, 0xCD, 0x32, 0xFF, repr), + Self::Linen => Color::new(0xFA, 0xF0, 0xE6, 0xFF, repr), + Self::Magenta => Color::new(0xFF, 0x00, 0xFF, 0xFF, repr), + Self::Maroon => Color::new(0x80, 0x00, 0x00, 0xFF, repr), + Self::MediumAquaMarine => Color::new(0x66, 0xCD, 0xAA, 0xFF, repr), + Self::MediumBlue => Color::new(0x00, 0x00, 0xCD, 0xFF, repr), + Self::MediumOrchid => Color::new(0xBA, 0x55, 0xD3, 0xFF, repr), + Self::MediumPurple => Color::new(0x93, 0x70, 0xDB, 0xFF, repr), + Self::MediumSeaGreen => Color::new(0x3C, 0xB3, 0x71, 0xFF, repr), + Self::MediumSlateBlue => Color::new(0x7B, 0x68, 0xEE, 0xFF, repr), + Self::MediumSpringGreen => Color::new(0x00, 0xFA, 0x9A, 0xFF, repr), + Self::MediumTurquoise => Color::new(0x48, 0xD1, 0xCC, 0xFF, repr), + Self::MediumVioletRed => Color::new(0xC7, 0x15, 0x85, 0xFF, repr), + Self::MidnightBlue => Color::new(0x19, 0x19, 0x70, 0xFF, repr), + Self::MintCream => Color::new(0xF5, 0xFF, 0xFA, 0xFF, repr), + Self::MistyRose => Color::new(0xFF, 0xE4, 0xE1, 0xFF, repr), + Self::Moccasin => Color::new(0xFF, 0xE4, 0xB5, 0xFF, repr), + Self::NavajoWhite => Color::new(0xFF, 0xDE, 0xAD, 0xFF, repr), + Self::Navy => Color::new(0x00, 0x00, 0x80, 0xFF, repr), + Self::OldLace => Color::new(0xFD, 0xF5, 0xE6, 0xFF, repr), + Self::Olive => Color::new(0x80, 0x80, 0x00, 0xFF, repr), + Self::OliveDrab => Color::new(0x6B, 0x8E, 0x23, 0xFF, repr), + Self::Orange => Color::new(0xFF, 0xA5, 0x00, 0xFF, repr), + Self::OrangeRed => Color::new(0xFF, 0x45, 0x00, 0xFF, repr), + Self::Orchid => Color::new(0xDA, 0x70, 0xD6, 0xFF, repr), + Self::PaleGoldenRod => Color::new(0xEE, 0xE8, 0xAA, 0xFF, repr), + Self::PaleGreen => Color::new(0x98, 0xFB, 0x98, 0xFF, repr), + Self::PaleTurquoise => Color::new(0xAF, 0xEE, 0xEE, 0xFF, repr), + Self::PaleVioletRed => Color::new(0xDB, 0x70, 0x93, 0xFF, repr), + Self::PapayaWhip => Color::new(0xFF, 0xEF, 0xD5, 0xFF, repr), + Self::PeachPuff => Color::new(0xFF, 0xDA, 0xB9, 0xFF, repr), + Self::Peru => Color::new(0xCD, 0x85, 0x3F, 0xFF, repr), + Self::Pink => Color::new(0xFF, 0xC0, 0xCB, 0xFF, repr), + Self::Plum => Color::new(0xDD, 0xA0, 0xDD, 0xFF, repr), + Self::PowderBlue => Color::new(0xB0, 0xE0, 0xE6, 0xFF, repr), + Self::Purple => Color::new(0x80, 0x00, 0x80, 0xFF, repr), + Self::RebeccaPurple => Color::new(0x66, 0x33, 0x99, 0xFF, repr), + Self::Red => Color::new(0xFF, 0x00, 0x00, 0xFF, repr), + Self::RosyBrown => Color::new(0xBC, 0x8F, 0x8F, 0xFF, repr), + Self::RoyalBlue => Color::new(0x41, 0x69, 0xE1, 0xFF, repr), + Self::SaddleBrown => Color::new(0x8B, 0x45, 0x13, 0xFF, repr), + Self::Salmon => Color::new(0xFA, 0x80, 0x72, 0xFF, repr), + Self::SandyBrown => Color::new(0xF4, 0xA4, 0x60, 0xFF, repr), + Self::SeaGreen => Color::new(0x2E, 0x8B, 0x57, 0xFF, repr), + Self::SeaShell => Color::new(0xFF, 0xF5, 0xEE, 0xFF, repr), + Self::Sienna => Color::new(0xA0, 0x52, 0x2D, 0xFF, repr), + Self::Silver => Color::new(0xC0, 0xC0, 0xC0, 0xFF, repr), + Self::SkyBlue => Color::new(0x87, 0xCE, 0xEB, 0xFF, repr), + Self::SlateBlue => Color::new(0x6A, 0x5A, 0xCD, 0xFF, repr), + Self::SlateGray => Color::new(0x70, 0x80, 0x90, 0xFF, repr), + Self::SlateGrey => Color::new(0x70, 0x80, 0x90, 0xFF, repr), + Self::Snow => Color::new(0xFF, 0xFA, 0xFA, 0xFF, repr), + Self::SpringGreen => Color::new(0x00, 0xFF, 0x7F, 0xFF, repr), + Self::SteelBlue => Color::new(0x46, 0x82, 0xB4, 0xFF, repr), + Self::Tan => Color::new(0xD2, 0xB4, 0x8C, 0xFF, repr), + Self::Teal => Color::new(0x00, 0x80, 0x80, 0xFF, repr), + Self::Thistle => Color::new(0xD8, 0xBF, 0xD8, 0xFF, repr), + Self::Tomato => Color::new(0xFF, 0x63, 0x47, 0xFF, repr), + Self::Turquoise => Color::new(0x40, 0xE0, 0xD0, 0xFF, repr), + Self::Violet => Color::new(0xEE, 0x82, 0xEE, 0xFF, repr), + Self::Wheat => Color::new(0xF5, 0xDE, 0xB3, 0xFF, repr), + Self::White => Color::new(0xFF, 0xFF, 0xFF, 0xFF, repr), + Self::WhiteSmoke => Color::new(0xF5, 0xF5, 0xF5, 0xFF, repr), + Self::Yellow => Color::new(0xFF, 0xFF, 0x00, 0xFF, repr), + Self::YellowGreen => Color::new(0x9A, 0xCD, 0x32, 0xFF, repr), } } -} \ No newline at end of file +} diff --git a/src/lexer.rs b/src/lexer.rs index 50361d3..a513c05 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -3,7 +3,7 @@ use std::iter::Peekable; use std::str::Chars; use crate::atrule::AtRuleKind; -use crate::color::{ColorName}; +use crate::color::ColorName; use crate::common::{Keyword, Op, Pos, Symbol}; use crate::selector::{Attribute, AttributeKind, CaseKind}; use crate::{Token, TokenKind, Whitespace}; @@ -413,9 +413,9 @@ impl<'a> Lexer<'a> { if let Ok(kw) = Keyword::try_from(string.as_ref()) { return TokenKind::Keyword(kw); } - + if let Ok(c) = ColorName::try_from(string.as_ref()) { - return TokenKind::Color(c.into()); + return TokenKind::Color(c.into_color(string)); } if string == "-" { diff --git a/src/value.rs b/src/value.rs index adb9439..cdea1a5 100644 --- a/src/value.rs +++ b/src/value.rs @@ -361,6 +361,89 @@ impl Value { TokenKind::Symbol(Symbol::BitAnd) => { Some(Value::Ident(String::from("&"), QuoteKind::None)) } + TokenKind::Symbol(Symbol::Hash) => { + let mut s = String::new(); + while let Some(tok) = toks.peek() { + match tok.kind.clone() { + TokenKind::Interpolation => { + toks.next(); + s.push_str( + &parse_interpolation(toks, scope) + .iter() + .map(|x| x.kind.to_string()) + .collect::(), + ) + } + TokenKind::Ident(ref i) => { + toks.next(); + s.push_str(i) + } + TokenKind::Number(ref n) => { + toks.next(); + s.push_str(n) + } + _ => 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)), + } + } TokenKind::Ident(mut s) => { while let Some(tok) = toks.peek() { match tok.kind.clone() { diff --git a/tests/color.rs b/tests/color.rs new file mode 100644 index 0000000..5faae05 --- /dev/null +++ b/tests/color.rs @@ -0,0 +1,33 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +test!(preserves_named_color_case, "a {\n color: OrAnGe;\n}\n"); +test!(preserves_hex_color_case, "a {\n color: #FfFfFf;\n}\n"); +test!( + preserves_hex_8_val_00000000, + "a {\n color: #00000000;\n}\n" +); +test!( + preserves_hex_8_val_12312312, + "a {\n color: #12312312;\n}\n" +); +test!( + preserves_hex_8_val_ab234cff, + "a {\n color: #ab234cff;\n}\n" +); +test!(preserves_hex_6_val_000000, "a {\n color: #000000;\n}\n"); +test!(preserves_hex_6_val_123123, "a {\n color: #123123;\n}\n"); +test!(preserves_hex_6_val_ab234c, "a {\n color: #ab234c;\n}\n"); +test!(preserves_hex_4_val_0000, "a {\n color: #0000;\n}\n"); +test!(preserves_hex_4_val_123a, "a {\n color: #123a;\n}\n"); +test!(preserves_hex_4_val_ab2f, "a {\n color: #ab2f;\n}\n"); +test!(preserves_hex_3_val_000, "a {\n color: #000;\n}\n"); +test!(preserves_hex_3_val_123, "a {\n color: #123;\n}\n"); +test!(preserves_hex_3_val_ab2, "a {\n color: #ab2;\n}\n"); +// test!( +// , +// "a {\n color: OrAnGe;\n}\n", +// "a {\n color: OrAnGe;\n}\n" +// );