Trying to define color pairs, I was getting strange results. All 256 colors are already defined, and attempt to change any color with init_color() has no affect.
I'm using Putty with 256-colors enabled and TERM=xterm-256color (also putty-256color), ncurses 6.0 compiled with --enable-widec and --enable-ext-colors. This shows all colors are defined and the init_color() doesn't change anything even though it succeeds:
init_scr();
start_color();
if (has_colors() && COLORS == 256 && can_change_color()) {
NCURSES_COLOR_T f;
for (f = 1; f < 256; f++) {
if (init_pair(f, f, COLOR_BLACK) == ERR) break;
attron(COLOR_PAIR(f));
printw("(%d)", f);
attroff(COLOR_PAIR(f));
refresh();
}
getch();
clear();
for (f = 1; f < 256; f++) {
if (init_color(f, 0, 0, f*3) == ERR) break;
if (init_pair(f, f, COLOR_BLACK) == ERR) break;
attron(COLOR_PAIR(f));
printw("(%d)", f);
attroff(COLOR_PAIR(f));
refresh();
}
getch();
clear();
}
I've read that the default colors can't be changed, but only refers to COLOR_BLACK, etc (0-7).
Where are these 256 default colors defined and why can't I change them? If they can't be changed, I could make use of the colors defined, but only if I can rely on them being the same on any 256-color capable terminal.
Couple of things I discovered. First, yes I was apparently referencing an old putty-256color terminfo that had "ccc", allowing can_change_color() to succeed, but then init_color() would fail. But the same Putty window using "xterm-256color" would init_color() OK and color_content() even shows the new values, but nothing changed on the screen. What was really confusing is sometimes the colors I set would appear and other times seemingly random colors appeared instead.
Here's what I found:
So there's basically no way to determine whether colors can be changed or not. But I did find that every terminal had already defined the standard 256 xterm colors, whether they could be changed or not. So, now, I just define the colors I want to use using the same color numbers as in the xterm palette. That way, the colors I expect will appear whether I needed to define them or not. So, to use "PaleGreen3", I just use:
If it works, it works, and if not, its probably already defined. For reference, I converting all the Xwindow/xterm colors from GUI hex notation to the ncurses (0-1000) values:
short: PuTTY doesn't do that, ncurses can't tell if PuTTY can...
long:
In ncurses, the
init_color
function checks its parameters (in the example given, those appear okay if your$TERM
is "xterm-256color"), as well as checking if the terminal description has theinitc
(initialize_color) capability. If that is missing or cancelled, ncurses returns an error.However, that's only the terminal description. ncurses cannot tell if you have chosen an incorrect or inappropriate terminal description.
In a quick check, PuTTY doesn't respond to the control sequence which is used in
initc
. This is a known limitation, as indicated in the (more appropriate) terminal descriptionputty-256color
provided by ncurses:That
xterm+256setaf
is used for terminals whose palette is hard-coded. PuTTY is not the only terminal which both setsTERM
=xterm` and lacks the ability to change its palette. If you happen to be using an old version of the terminal database, you may be misled, since that error was fixed in 2014:Like the other terminals whose developers set
TERM=xterm
(orTERM=xterm-256color
), there are differences between those andxterm
.Further reading: