2

Would it be possible for me to print a color in my terminal without using the colors from my .Xresources? For example, echo -e "\033[32m Test" will print "Test" but it's in the color that I've defined as green in my .Xresources. That being said, how would I print a color that isn't defined, such as #FF0000 without redefining my preset.

1

Colours in a terminal (shell) window have no real relation to colours in your .Xresources file.

If you want to print colours in a terminal (shell) window you should seriously consider the tput command. This uses the terminfo database to generate the necessary escape sequences to obtain colours (amongst other things).

You can man 5 terminfo and read the section on Color Handling. Here's the table of available colours:

Colour    Value RGB
black     0     0, 0, 0
red       1     max,0,0
green     2     0,max,0
yellow    3     max,max,0
blue      4     0,0,max
magenta   5     max,0,max
cyan      6     0,max,max
white     7     max,max,max

and you use the setaf (set ANSI foreground) and setab (set ANSI background) commands to set them. Use sgr0 to reset them afterwards.

For example

tput setaf 4    # Blue text
tput setab 3    # Yellow background
echo -n "This is blue text on a yellow background"
tput sgr0       # Reset colours
echo 
|improve this answer|||||
  • 2
    If your terminal reads ~/.Xresources (eg., rxvt or xterm), then it will use the colours defined there. – jasonwryan Apr 4 '17 at 22:21
  • @jasonwryan I've not seen that behaviour but I'll check it (tomorrow). Thanks for the pointer – roaima Apr 4 '17 at 22:31
  • @roaima I believe that's what's happening with me. When I try to print a color such as red it will print what I've defined as red in my .Xresources – Erich Apr 5 '17 at 12:04
  • @Erich and @jasonwryan I can't reproduce this. I've added a series of *colorN: #rrggbb entries in my ~/.Xresources and run xrdb -merge on it. A new xterm still seems to give me the standard set of colours for tput setaf $af – roaima Apr 5 '17 at 15:07
  • I guess I still don't understand, so setting blackf="${esc}[30m";, then printing later in that script ${blackf}This is a test will print the black I've defined in my ~.Xresources and my terminal's config. If it helps I'm using termite. – Erich Apr 5 '17 at 17:12
0

Some terminal emulators support direct RGB true colors. See https://gist.github.com/XVilka/8346728 for details and usage examples.

|improve this answer|||||
  • That Gist is excellent. If you could summarise the relevant parts of it that apply to the OP's question this could be a superb answer. – roaima Apr 5 '17 at 14:57

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.