Add a cache to find_nearest_color.
use bracket_color::prelude::*;
+use std::collections::HashMap;
+use parking_lot::Mutex;
pub struct CursesColor {
rf: f32,
@@ -16,7 +18,23 @@ impl CursesColor {
}
}
+lazy_static! {
+ static ref COLOR_CACHE: Mutex<HashMap<u32, i16>> = Mutex::new(HashMap::new());
+}
+
pub fn find_nearest_color(color: RGBA, map: &[CursesColor]) -> i16 {
+ let key = (color.r * 255.0) as u32 +
+ (((color.g * 255.0) as u32) << 8u32) +
+ (((color.b * 255.0) as u32) << 16u32) +
+ (((color.a * 255.0) as u32) << 24u32)
+ ;
+ {
+ let cache = COLOR_CACHE.lock();
+ if let Some(col) = cache.get(&key) {
+ return *col;
+ }
+ }
+
let mut result = -1;
let mut best_diff = std::f32::MAX;
@@ -32,5 +50,7 @@ pub fn find_nearest_color(color: RGBA, map: &[CursesColor]) -> i16 {
}
}
+ COLOR_CACHE.lock().insert(key, result);
+
result
}
