How do i use more than 8 colors in ncurses?

2019-08-15 05:19发布

问题:

I just started to use ncurses on Linux. I wanted to use more than 8 colors, but there were only 8 available.

How can I use more colors, or create my own by giving them a names, and set their RGB values?

I tried editing a color with init_color, but that will simply replace one of the current 8 instead of creating new ones.

Note: the value of the global var COLORS is 256, so I believe I can use up to 256 different colors.

回答1:

If your terminal supports it, you should choose (or customize) a terminal description which has more than 8 colors. As it is, there are a lot of existing terminal descriptions which could be used: most are customized for particular terminals (or terminal emulators).

If your terminal supports it, the corresponding description would have the capability initc. That is used by the library call init_color. The xterm-256color entry has this for instance:

initc=\E]4;%p1%d;rgb\:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*%{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\E\\,

which tells the library how to translate the three parameters into an escape sequence. The terminal entries are built up from reusable parts such as xterm+256color, because some terminals lack the feature you are interested in. For those, xterm+256setaf is appropriate.

For reference,

  • Terminal Database Most systems have this as base (small) and extra (large) packages.
  • start_color, init_pair, init_color, has_colors, can_change_color, color_content, pair_content, COLOR_PAIR - curses color manipulation routines
  • My terminal doesn't recognize color
  • Why only 16 (or 256) colors?


标签: c linux ncurses