8

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.

3 Answers 3

7

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

3

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 range 0 to COLORS-1 (except for the default colors extension), or use color pairs outside the range 0 to COLOR_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.

12
  • 1
    This isn't what I asked about. I know that there are 32k combinations of colors, this wasn't the question about that. It was a question about COLOR_PAIRS variable, which specifies how many different color pairs are supported. The default is measly 256, and I read somewhere that it can be set to 32k, but I don't know how.
    – redspah
    Commented Nov 29, 2015 at 17:56
  • @redspah: Read this answer again. Commented Nov 29, 2015 at 17:58
  • @LightnessRacesinOrbit Lemme try to explain myself again. This isn't a question about possible color pairs themselves, it's about increasing the size of buffer that holds pairs defined with init_pair(). Right now, it's set to 256, meaning that there can be only 256 color pairs defined at any given time, and while you're free to redefine them, you won't get more than 256 pairs. I'm looking for a method to change the size of that buffer, so that there can be place for 32k different pairs to be defined at the same time.
    – redspah
    Commented Nov 29, 2015 at 18:01
  • No need. This answer tells you what you need to know. You simply need to keep an open mind and throw away your false assumptions before absorbing it! Commented Nov 29, 2015 at 18:02
  • @redspah I'm guessing what your misapprehension was & tried to improve the answer.
    – abligh
    Commented Nov 29, 2015 at 18:05
0

Use init_extended (available from ncurses 6.1 and up):

If you're here today, don't worry, I'm here with a solution.

You can install ncurses library with home-brew (make sure you install the latest one which is 6.4). That saves you the burden of compiling your own, because now you can just call init_extended_color(...) and init_extended_pair(...), make VERY SURE that you are using YOUR ncurses installation and not the system default, which means specifying path with -L and name with -l, if you won't use both you won't use both options you won't link it correctly!!!

g++ -I/opt/homebrew/Cellar/ncurses/6.4/include -I./include ./src/*.cpp -o app --std=c++20 -L/opt/homebrew/Cellar/ncurses/6.4/lib -lncurses -lform -lpanel -lmenu -o app

-lform, -lpanel, -lmenu are there only because of the needs of my project: -lncurses is the required one

Notice the fact that I specify both the -L (library path) before the -l (library name). In case you didn't know, -l looks for files in the -L path first :)

If you don't have or can't use home-brew for whatever reason, just skip the whole homebrew part.

the init_extended functions were added in curses 6.1 and take advantage of the full 32K range of the SHORT variable. I hope it helps anyone who needs to know this today!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.