remove bimap in favor of ad hoc structure

This commit is contained in:
ConnorSkees 2020-05-19 19:13:49 -04:00
parent eb7fe52074
commit cf987abb53
4 changed files with 32 additions and 6 deletions

View File

@ -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"]

View File

@ -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)

View File

@ -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<BiMap<&'static str, [u8; 4]>> = 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<NamedColorMap> = 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]);

View File

@ -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,