2

I'm trying to print colors using terminfo database and have successfully parsed the terminal database files present for each terminal. In my case I've gnome terminal which definitely supports colors.

Now there are several commands in terminfo database like -

set_foreground
set_background
set_a_foreground
set_a_background

Since I want to set foreground color, I picked set_a_foreground which says it is compatible with ANSI seq. But still I don't know how to actually print colors with either of them.

Both of them say something like this - Set foreground color #1 in their description and their actual string looks like this on my terminal - ESC[3%p1%dm.

So my question is, which one should should I use of set_a_ or set_ version and how to print any color with them.

1 Answers1

2

The distinction between set_foreground and set_a_foreground (as well as the background capabilities) is in the terminfo(5) manual page in the Color Handling section. Keep in mind that the long names are used infrequently, and that you should be looking for setf versus setaf:

   The  setaf/setab  and setf/setb capabilities take a single
   numeric argument each.  Argument values 0-7 of setaf/setab
   are  portably defined as follows (the middle column is the
   symbolic #define available in the header for the curses or
   ncurses  libraries).  The terminal hardware is free to map
   these as it likes, but  the  RGB  values  indicate  normal
   locations in color space.

         Color       #define       Value       RGB
         black     COLOR_BLACK       0     0, 0, 0
         red       COLOR_RED         1     max,0,0
         green     COLOR_GREEN       2     0,max,0
         yellow    COLOR_YELLOW      3     max,max,0
         blue      COLOR_BLUE        4     0,0,max
         magenta   COLOR_MAGENTA     5     max,0,max
         cyan      COLOR_CYAN        6     0,max,max
         white     COLOR_WHITE       7     max,max,max

   The  argument  values of setf/setb historically correspond
   to a different mapping, i.e.,

         Color       #define       Value       RGB
         black     COLOR_BLACK       0     0, 0, 0
         blue      COLOR_BLUE        1     0,0,max
         green     COLOR_GREEN       2     0,max,0
         cyan      COLOR_CYAN        3     0,max,max
         red       COLOR_RED         4     max,0,0
         magenta   COLOR_MAGENTA     5     max,0,max
         yellow    COLOR_YELLOW      6     max,max,0
         white     COLOR_WHITE       7     max,max,max

   It is important to not confuse the two sets of color capa-
   bilities;  otherwise  red/blue will be interchanged on the
   display.

Most applications using just terminfo (and not curses) use the tparm function to format the string, substituting a (numeric) parameter, and then use tputs on the resulting string to actually write it. The two account for padding and delays (not usually found in color capabilities, but in terminfo generally).

The ncurses-examples program dots uses these functions to randomly draw colored cells on the screen. (In the example, tparm2, tparm3 are macros which provide the extra parameters that tparm prototype requires).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105