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?
Best Answer
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:To redefines color 1 (normally
red
) to 1000‰ red, 0‰ green, 0‰ blue (#ff0000
).So:
would do what you want.
To see what escape sequence that corresponds to:
So, on my terminal (
xterm
), I can also do: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), withxterm
:Or to reset a single color:
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 thextermcontrol
command to make it easier: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 likexterm-256color
.