问题
I've read that ncurses can support up to 256 colors and up to 32k color pairs. While I managed to set up 256 colors by myself, I can't find any information on how to set up 32k color pairs.
The result of
printf("%d - %d\n", COLORS, COLOR_PAIRS);
is
256 - 256
and while 2 colors and 2 color pairs may be enough for hardcore terminal fans, I'd like to know how to get the most colors out of the library.
回答1:
By default, ncurses6 configures with --enable-ext-colors
enabled. You need also --enable-widec
(otherwise, the cchar_t
type which stores extended colors is not used). The configure script warns about this:
checking if you want to use extended colors... yes
configure: WARNING: This option applies only to wide-character library
Assuming that you built the library with extended colors (and wide characters), it is capable of displaying up to 256 colors and up to 32767 color pairs (the maximum value in a signed 16-bit number). After that, it depends on the terminal description that you are using (and the terminal emulator). A majority of the terminal emulators running in X Windows can display 256 colors. Outside of X, it's not clear that there is a majority.
ncurses has reasonably accurate terminal descriptions for each of these (and no, using TERM=xterm-256color
is not the answer for each, since special keys and other characteristics generally differ from xterm: the FAQ Why not just use TERM set to "xterm"? also applies to xterm-256color
).
Here is a screenshot showing xterm
running the ncurses
test program (from ncurses-examples) for wide colors:
回答2:
A colour pair in ncurses
is actually a combination of a foreground colour and a background colour. You can manipulate them with color_pair
(manual page here). There are 32,768 combinations as there are 256 combinations for foreground and the same for background, but counting red+blue (for instance) as blue+red, there are 256x256/2 = 32,768 combinations.
You can then use foreground+background pairs with a checkerboard pattern (e.g. ░░░░░░░░░░░░
) to produce more colours. See e.g. here.
I think there may be a misunderstanding inherent in your question though. From the manual page:
This [linux] implementation will return
ERR
on attempts to use color values outside the range0
toCOLORS-1
(except for the default colors extension), or use color pairs outside the range0
toCOLOR_PAIR-1.
What that's saying is that you can only use COLOR_PAIR
colour pairs at once (i.e. available to use via init_pair()
) - on your implementation that's 256. But there are still 32,768 colour pairs available - just not all at once via init_pair()
. That's the way I understand it anyway.
来源:https://stackoverflow.com/questions/33985896/how-to-enable-32k-color-pairs-in-ncurses