6

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?

7

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.:

5

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
  • I only get eight colors that way, though, and the terminal supposedly supports 256. Where did all of the other colors go? – fouric Jun 12 '13 at 4:53
  • Your terminal only supports 8 colors then. Try another terminal. – Ignacio Vazquez-Abrams Jun 12 '13 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 Jun 12 '13 at 5:07
  • tput colors on my console resulted in 8. So I would say that this is not unique to you. – Ignacio Vazquez-Abrams Jun 12 '13 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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