7

I would like to write a program that utilizes all 256 of the Linux terminal's text colors. To date, however, I have only found a handful of color codes, which are in the format "\033[1;NUMmNUM\033[1;m". However, these escape codes only allow for two or three dozen colors, not the 256 that the Linux terminal supposedly allows for. How can I display those other colors in my curses application?

3 Answers 3

8

You can have a look into TERM settings. It is possible it is set to lesser then capable string. To test colors you can run GNU's msgcat. Try e.g.:

echo $TERM
msgcat --color=test
TERM=xterm-256color
msgcat --color=test

Or use @Ignacio Vazquez-Abrams's loop instead of msgcat.

For more information on how and what to set have a look at e.g.:

6

Stop using escape codes and use the setaf terminfo capability instead.

$ for i in $(seq 0 $(tput colors) ) ; do tput setaf $i ; echo -n "█" ; done ; tput setaf 15 ; echo
4
  • I only get eight colors that way, though, and the terminal supposedly supports 256. Where did all of the other colors go?
    – fouric
    Commented Jun 12, 2013 at 4:53
  • Your terminal only supports 8 colors then. Try another terminal. Commented Jun 12, 2013 at 4:54
  • I have tried LXTerm, Xterm, and the Linux terminal (both with and without tmux), and gotten the same result on all three. Are you sure that the Linux ttys themselves do not support 256 colors?
    – fouric
    Commented Jun 12, 2013 at 5:07
  • tput colors on my console resulted in 8. So I would say that this is not unique to you. Commented Jun 12, 2013 at 5:21
1

TERM tells tput (and other applications that use the terminal database) how many colors your terminal (supposedly) supports and how to set the foreground (setaf) and background (setab) for that.

The example script is close, but doesn't reset properly:

for i in $(seq 0 $(tput colors) ) ; do tput setaf $i ; echo -n "█" ; done ; echo ; tput op

Here is a screenshot from xterm to illustrate:

example of tput and 256colors

For what it's worth, msgcat uses the terminal database, but then follows up with some hardcoded assumptions about the terminal's color capability. Some of that needs rethinking, e.g., noticing a coding error for instance in this line:

               (strlen (term) >= 4 && memcmp (term, "rxvt", 7) == 0)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .