From fbd4dd97f1263eb69bbb969e22d39e921aa91c20 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 27 Jan 2018 18:14:37 -0800 Subject: [PATCH] Add grayscale color matching for ncurses backend --- src/backend/curses/mod.rs | 23 ++++++++++++++++++----- src/backend/curses/n.rs | 1 + 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index 25be9a6..c4dfe42 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -34,11 +34,24 @@ fn find_closest(color: &Color) -> i16 { Color::Light(BaseColor::Cyan) => 14, Color::Light(BaseColor::White) => 15, Color::Rgb(r, g, b) => { - 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 + // If r = g = b, it may be a grayscale value! + if r == g && g == b && r != 0 && r < 250 { + // (r = g = b) = 8 + 10 * n + // (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), } } diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 66ba074..c5c322d 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -213,6 +213,7 @@ impl backend::Backend for Concrete { } fn with_color(&self, colors: ColorPair, f: F) { + eprintln!("Color used: {:?}", colors); let current = self.current_style.get(); if current != colors { self.set_colors(colors);