7

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.

redspah
  • 257
  • 3
  • 9

2 Answers2

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

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
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.

Community
  • 1
  • 1
abligh
  • 24,573
  • 4
  • 47
  • 84
  • 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 Nov 29 '15 at 17:56
  • @redspah: Read this answer again. – Lightness Races in Orbit Nov 29 '15 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 Nov 29 '15 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! – Lightness Races in Orbit Nov 29 '15 at 18:02
  • @redspah I'm guessing what your misapprehension was & tried to improve the answer. – abligh Nov 29 '15 at 18:05
  • Your edit hit the mark. I'm looking for a way to change the value of COLOR_PAIRS to 32k. Should I edit my quesion? – redspah Nov 29 '15 at 18:10
  • @redspah I don't believe you can just change the value of `COLOR_PAIRS`. You would have to recompile the library. – abligh Nov 29 '15 at 18:13
  • @abligh Alright then. What flags do I need to recompile ncurses with then? – redspah Nov 29 '15 at 18:13
  • @redspah as you are on Ubuntu, I would start with `apt-get source` per http://askubuntu.com/questions/28372/how-do-i-get-and-modify-the-source-code-of-packages-installed-through-apt-get and use the compile flags that uses - however, note that the library you build may well be incompatible with other shared library users, so I would ensure you static link to it only. – abligh Nov 29 '15 at 18:15
  • Just looked through the INSTALL file for ncurses. No mention of increasing the amount of color pairs available. – redspah Nov 29 '15 at 18:37
  • @redspah no I think you will need to alter the source code. Look for a `#define` of `COLOUR_PAIRS`. – abligh Nov 29 '15 at 18:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96486/discussion-between-redspah-and-abligh). – redspah Nov 29 '15 at 19:02