diff --git a/Cargo.toml b/Cargo.toml index c7b08f4..1cdc1e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ rand = { version = "0.7.3", optional = true } codemap = "0.1.3" peekmore = "0.4.0" wasm-bindgen = { version = "0.2.60", optional = true } -bimap = "0.4.0" [features] default = ["commandline", "random"] diff --git a/src/color/mod.rs b/src/color/mod.rs index 57f82d5..fb7aef4 100644 --- a/src/color/mod.rs +++ b/src/color/mod.rs @@ -498,7 +498,7 @@ fn repr(red: &Number, green: &Number, blue: &Number, alpha: &Number) -> String { if alpha < &Number::one() { format!("rgba({}, {}, {}, {})", red_u8, green_u8, blue_u8, alpha) - } else if let Some(c) = NAMED_COLORS.get_by_right(&[red_u8, green_u8, blue_u8, 0xFF]) { + } else if let Some(c) = NAMED_COLORS.get_by_rgba(&[red_u8, green_u8, blue_u8, 0xFF]) { (*c).to_string() } else { format!("#{:0>2x}{:0>2x}{:0>2x}", red_u8, green_u8, blue_u8) diff --git a/src/color/name.rs b/src/color/name.rs index c01e4f3..d613224 100644 --- a/src/color/name.rs +++ b/src/color/name.rs @@ -1,11 +1,38 @@ //! A big dictionary of named colors and their //! corresponding RGBA values -use bimap::BiMap; use once_cell::sync::Lazy; +use std::collections::HashMap; -pub(crate) static NAMED_COLORS: Lazy> = Lazy::new(|| { - let mut m = BiMap::with_capacity(150); +pub(crate) struct NamedColorMap { + name_to_rgba: HashMap<&'static str, [u8; 4]>, + rgba_to_name: HashMap<[u8; 4], &'static str>, +} + +impl NamedColorMap { + pub fn with_capacity(capacity: usize) -> Self { + Self { + name_to_rgba: HashMap::with_capacity(capacity), + rgba_to_name: HashMap::with_capacity(capacity), + } + } + + pub fn insert(&mut self, name: &'static str, rgba: [u8; 4]) { + self.name_to_rgba.insert(name, rgba); + self.rgba_to_name.insert(rgba, name); + } + + pub fn get_by_name(&self, name: &str) -> Option<&[u8; 4]> { + self.name_to_rgba.get(name) + } + + pub fn get_by_rgba(&self, rgba: &[u8; 4]) -> Option<&&str> { + self.rgba_to_name.get(rgba) + } +} + +pub(crate) static NAMED_COLORS: Lazy = Lazy::new(|| { + let mut m = NamedColorMap::with_capacity(150); m.insert("aliceblue", [0xF0, 0xF8, 0xFF, 0xFF]); m.insert("antiquewhite", [0xFA, 0xEB, 0xD7, 0xFF]); m.insert("aqua", [0x00, 0xFF, 0xFF, 0xFF]); diff --git a/src/value/parse.rs b/src/value/parse.rs index e205395..04c2602 100644 --- a/src/value/parse.rs +++ b/src/value/parse.rs @@ -586,7 +586,7 @@ impl Value { )); } - if let Some(c) = NAMED_COLORS.get_by_left(&lower.as_str()) { + if let Some(c) = NAMED_COLORS.get_by_name(&lower.as_str()) { return Ok(IntermediateValue::Value(Spanned { node: Value::Color(Box::new(Color::new(c[0], c[1], c[2], c[3], s))), span,