mirror of
https://github.com/Llewellynvdm/conky.git
synced 2025-02-04 21:18:33 +00:00
ncurses: allocate custom colors
color0-9 will now use their configured RGB values exactly
This commit is contained in:
parent
d51bf688ba
commit
9ab51edf9f
@ -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);
|
||||
|
@ -36,9 +36,6 @@
|
||||
#ifdef BUILD_X11
|
||||
#include "x11.h"
|
||||
#endif /* BUILD_X11 */
|
||||
#ifdef BUILD_NCURSES
|
||||
#include <ncurses.h>
|
||||
#endif
|
||||
|
||||
unsigned int adjust_colours(unsigned int);
|
||||
|
||||
@ -79,41 +76,11 @@ public:
|
||||
return static_cast<unsigned long>(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 *);
|
||||
|
@ -27,7 +27,9 @@
|
||||
#include <config.h>
|
||||
|
||||
#include "conky.h"
|
||||
#include "colours.h"
|
||||
#include "display-ncurses.hh"
|
||||
#include "gui.h"
|
||||
#include "nc.h"
|
||||
|
||||
#include <iostream>
|
||||
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user