2

is there any way to echo/print text in the color of a given hex code (#000000, #FFFFFF, etc.) regardless of one's own Xresources/Xdefaults color definitions?

Like, if Bob had in his Xresources/Xdefaults:

*color1 : #800000 

And Tom had in his Xresources/Xdefaults:

*color1 : #CC3300

And some oddball had in his Xresources/Xdefaults:

*color1 : #8080FF

Is there anything they could all three type that would show all three of them something like this?

screenshot

6

Many terminal emulators allow to redefine colors with escape sequences, there's even a terminfo capability for that: initc. With those, and assuming the terminfo database is correct, you can do:

tput initc 1 1000 0 0

To redefines color 1 (normally red) to 1000‰ red, 0‰ green, 0‰ blue (#ff0000).

So:

tput initc 1 1000 0 0
tput setaf 1 # to set the foreground color to 1
echo '██ = #FF0000'
tput sgr0

would do what you want.

To see what escape sequence that corresponds to:

$ tput initc 1 1000 0 0 | cat -vt
^[]4;1;rgb:FF/00/00^[\

So, on my terminal (xterm), I can also do:

printf '\e]4;1;rgb:FF/00/00\e\\\e[31m██ = #FF0000\e[m\n'

Note that it changes the color of color1. So if you change that to blue, all the text that had been displayed with that color will automatically change color.

To reset the colors to their initial values (initial at the time xterm was started), with xterm:

printf '\e]104\a'

Or to reset a single color:

printf '\e]104;1\a'

To query the current value of a color, there's a control sequence that causes xterm to send back the value as terminal input. You can use the xtermcontrol command to make it easier:

$ xtermcontrol --get-color1
rgb:ffff/ffff/0000

But that only works for the first 16 colors (xterm nowadays supports 256).

On terminals that don't support resetting the colors to their defaults, but support 256 colors à la xterm, you may want to use colors 17 and above as those are rarely used by applications.

However note that some terminfo database incorrectly specify how to assign and use those colors for those terminals, so you may want either to hardcode the escape sequences of force $TERM to something like xterm-256color.

printf '\e]4;17;rgb:ff/ff/00\a\e[38;5;17mThis is yellow\e[m\n'
printf '\e]4;18;rgb:ff/00/ff\a\e[38;5;18mThis is magenta\e[m\n'
  • 1
    Caveat: this changes the terminal settings, with AFAIK no way to restore them to their original state when the application exits. (You can restore the default colors with tpuc oc, but that doesn't work if the user had customized his colors dynamically.) – Gilles 'SO- stop being evil' Dec 6 '13 at 1:40
  • @Gilles, the oc capability doesn't seem to be defined for xterm in my terminfo database, but printf '\e]104\a' does reset the colors to the corresponding X resources (as defined by the system or the user). See my updated answer. – Stéphane Chazelas Dec 6 '13 at 11:11
  • But that's restoring the default colors, not the colors from when the application started. This setting is meant as a user customization, it can't be used as an application customization unless the application starts the terminal for its sole use. – Gilles 'SO- stop being evil' Dec 6 '13 at 11:13
  • @Gilles, OK, I now see what you mean. The application can still query the current color values on startup and restore them on exit. – Stéphane Chazelas Dec 6 '13 at 11:14

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.