Search code examples
c++cubuntuncurses

How to enable 32k color pairs in ncurses?


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.


Solution

  • 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:

    enter image description here