From 9ab51edf9f3d98fcdc754a87ac34703c4d2367ef Mon Sep 17 00:00:00 2001 From: bi4k8 Date: Sun, 5 Feb 2023 05:47:06 +0000 Subject: [PATCH] ncurses: allocate custom colors color0-9 will now use their configured RGB values exactly --- src/colours.cc | 25 -------------------- src/colours.h | 37 ++---------------------------- src/display-ncurses.cc | 52 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 62 deletions(-) diff --git a/src/colours.cc b/src/colours.cc index bda40d84..a77d6d70 100644 --- a/src/colours.cc +++ b/src/colours.cc @@ -113,31 +113,6 @@ Colour Colour::from_argb32(uint32_t argb) { Colour error_colour { 0xff, 0x00, 0x00, 0xff }; -#ifdef BUILD_NCURSES -Colour Colour::from_ncurses(int nccolor) { - switch(nccolor) { - case COLOR_WHITE: - return {0xff, 0xff, 0xff, 0xff}; - case COLOR_RED: - return {0xff, 0x00, 0x00, 0xff}; - case COLOR_GREEN: - return {0x00, 0xff, 0x00, 0xff}; - case COLOR_YELLOW: - return {0xff, 0xff, 0x00, 0xff}; - case COLOR_BLUE: - return {0x00, 0x00, 0xff, 0xff}; - case COLOR_MAGENTA: - return {0xff, 0x00, 0xff, 0xff}; - case COLOR_CYAN: - return {0x00, 0xff, 0xff, 0xff}; - case COLOR_BLACK: - return {0x00, 0x00, 0x00, 0xff}; - default: - return error_colour; - } -} -#endif /*BUILD_NCURSES*/ - Colour parse_color(const char *name) { unsigned short r, g, b; size_t len = strlen(name); diff --git a/src/colours.h b/src/colours.h index 6a32a95e..11df01ad 100644 --- a/src/colours.h +++ b/src/colours.h @@ -36,9 +36,6 @@ #ifdef BUILD_X11 #include "x11.h" #endif /* BUILD_X11 */ -#ifdef BUILD_NCURSES -#include -#endif unsigned int adjust_colours(unsigned int); @@ -79,41 +76,11 @@ public: return static_cast(xcolor.pixel); } #endif /* BUILD_X11 */ - -#ifdef BUILD_NCURSES - static Colour from_ncurses(int nccolor); - - // Find the nearest ncurses color. - int to_ncurses() { - int nccolors[] = { - COLOR_WHITE, - COLOR_RED, - COLOR_GREEN, - COLOR_YELLOW, - COLOR_BLUE, - COLOR_MAGENTA, - COLOR_CYAN, - COLOR_BLACK, - }; - int mindiff = INT_MAX; - int best_nccolor = nccolors[0]; - for (int nccolor : nccolors) { - Colour other = Colour::from_ncurses(nccolor); - int diff = abs(red - other.red) + - abs(green - other.green) + - abs(blue - other.blue); - - if (diff < mindiff) { - mindiff = diff; - best_nccolor = nccolor; - } - } - return best_nccolor; - } -#endif /* BUILD_NCURSES */ }; +extern Colour error_colour; + Colour parse_color(const std::string &colour); // XXX: when everyone uses C++ strings, remove this C version Colour parse_color(const char *); diff --git a/src/display-ncurses.cc b/src/display-ncurses.cc index 42516e25..3b47fbbf 100644 --- a/src/display-ncurses.cc +++ b/src/display-ncurses.cc @@ -27,7 +27,9 @@ #include #include "conky.h" +#include "colours.h" #include "display-ncurses.hh" +#include "gui.h" #include "nc.h" #include @@ -60,6 +62,45 @@ extern void init_ncurses_output() {} #ifdef BUILD_NCURSES +#define COLORS_BUILTIN 8 +#define COLORS_CUSTOM 10 + +Colour ncurses_colors[COLORS_BUILTIN + COLORS_CUSTOM] = { + {0x00, 0x00, 0x00, 0xff}, // BLACK + {0xff, 0x00, 0x00, 0xff}, // RED + {0x00, 0xff, 0x00, 0xff}, // GREEN + {0xff, 0xff, 0x00, 0xff}, // YELLOW + {0x00, 0x00, 0xff, 0xff}, // BLUE + {0xff, 0x00, 0xff, 0xff}, // MAGENTA + {0x00, 0xff, 0xff, 0xff}, // CYAN + {0xff, 0xff, 0xff, 0xff}, // WHITE +}; + + // Find the nearest ncurses color. + int to_ncurses(const Colour& c) { + int mindiff = INT_MAX; + int best_nccolor = 0; + for (int nccolor = 0; nccolor < COLORS_BUILTIN + COLORS_CUSTOM; nccolor++) { + const Colour& other = ncurses_colors[nccolor]; + int diff = abs(c.red - other.red) + + abs(c.green - other.green) + + abs(c.blue - other.blue); + + if (diff < mindiff) { + mindiff = diff; + best_nccolor = nccolor; + } + } + return best_nccolor; + } + +Colour from_ncurses(int nccolor) { + if (nccolor >= 0 && nccolor < COLORS_BUILTIN + COLORS_CUSTOM) { + return ncurses_colors[nccolor]; + } + return error_colour; +} + display_output_ncurses::display_output_ncurses() : display_output_console("ncurses") { priority = 1; @@ -74,6 +115,12 @@ bool display_output_ncurses::detect() { } bool display_output_ncurses::initialize() { + for(int i = 0; i < COLORS_CUSTOM; i++) { + Colour c = color[i].get(*state); + init_color(COLORS_BUILTIN + i, (1000*c.red)/255, (1000*c.green)/255, (1000*c.blue)/255); + ncurses_colors[COLORS_BUILTIN + i] = c; + } + is_active = ncurses_window != nullptr; return is_active; } @@ -81,8 +128,9 @@ bool display_output_ncurses::initialize() { bool display_output_ncurses::shutdown() { return false; } void display_output_ncurses::set_foreground_color(Colour c) { - init_pair(c.to_ncurses(), c.to_ncurses(), COLOR_BLACK); - attron(COLOR_PAIR(c.to_ncurses())); + int nccolor = to_ncurses(c); + init_pair(nccolor, nccolor, COLOR_BLACK); + attron(COLOR_PAIR(nccolor)); } void display_output_ncurses::begin_draw_text() {