Add grayscale color matching for ncurses backend

This commit is contained in:
Alexandre Bury 2018-01-27 18:14:37 -08:00
parent 800470e22c
commit fbd4dd97f1
2 changed files with 19 additions and 5 deletions

View File

@ -34,11 +34,24 @@ fn find_closest(color: &Color) -> i16 {
Color::Light(BaseColor::Cyan) => 14, Color::Light(BaseColor::Cyan) => 14,
Color::Light(BaseColor::White) => 15, Color::Light(BaseColor::White) => 15,
Color::Rgb(r, g, b) => { Color::Rgb(r, g, b) => {
let r = 6 * u16::from(r) / 256; // If r = g = b, it may be a grayscale value!
let g = 6 * u16::from(g) / 256; if r == g && g == b && r != 0 && r < 250 {
let b = 6 * u16::from(b) / 256; // (r = g = b) = 8 + 10 * n
(16 + 36 * r + 6 * g + b) as i16 // (r - 8) / 10 = n
//
let n = (r - 8) / 10;
(232 + n) as i16
} else {
let r = 6 * u16::from(r) / 256;
let g = 6 * u16::from(g) / 256;
let b = 6 * u16::from(b) / 256;
(16 + 36 * r + 6 * g + b) as i16
}
}
Color::RgbLowRes(r, g, b) => {
let a = i16::from(16 + 36 * r + 6 * g + b);
eprintln!("{:?} => {}", color, a);
a
} }
Color::RgbLowRes(r, g, b) => i16::from(16 + 36 * r + 6 * g + b),
} }
} }

View File

@ -213,6 +213,7 @@ impl backend::Backend for Concrete {
} }
fn with_color<F: FnOnce()>(&self, colors: ColorPair, f: F) { fn with_color<F: FnOnce()>(&self, colors: ColorPair, f: F) {
eprintln!("Color used: {:?}", colors);
let current = self.current_style.get(); let current = self.current_style.get();
if current != colors { if current != colors {
self.set_colors(colors); self.set_colors(colors);