How can I display colors in terminal to handle hexadecimal color values ? It can be useful for theming, XResources etc. For example :
$ command '#FF0000'
// display a red square
I use urxvt, i3wm in manjaro.
How can I display colors in terminal to handle hexadecimal color values ? It can be useful for theming, XResources etc. For example :
$ command '#FF0000'
// display a red square
I use urxvt, i3wm in manjaro.
show_colour() { perl -e 'foreach $a(@ARGV){print "\e[48:2::".join(":",unpack("C*",pack("H*",$a)))."m \e[49m "};print "\n"' "$@" }
Example usage:
$ show_colour "FF0088" "61E931" "1256E2"
This prints spaces with the given RGB background colours. Note that you must not use #
in the RGB code. I leave stripping that if present as an exercise for the reader. ☺
This does not alter the terminal emulator's palette.
Caveat: Your terminal emulator must understand direct colour SGR control sequences, using the correct ITU T.416 forms. A few do. More understand these control sequences in certain long-standing faulty formulations. And you'll find that rxvt-unicode does not understand them at all. For one common faulty formulation substitute this ambiguous form:
show_colour() { perl -e 'foreach $a(@ARGV){print "\e[48;2;".join(";",unpack("C*",pack("H*",$a)))."m \e[49m "};print "\n"' "$@" }
Use my portable setterm
, which I mentioned at https://unix.stackexchange.com/a/491883/5132 . It understands hexadecimal RGB notation, and even uses #
as the indicator for it.
Example usage:
$ setterm -7 --background '#FF0088' ; printf ' ' ; \ > setterm -7 --background '#61E931' ; printf ' ' ; \ > setterm -7 --background '#1256E2' ; printf ' ' ; \ > setterm -7 --background default ; printf '\n'
This prints the same as the other example on terminals that understand direct colour SGR control sequences.
One difference from the preceding alternative is that setterm
also works on other terminals. It has fallbacks for terminal types that do not understand direct colour SGR control sequences. On terminal types that only understand indexed colour (i.e. only 256 colours) or on terminals that only understand the 16 AIXTerm colours, it tries to pick the nearest to the desired RGB colour:
% TERM=rxvt-256color setterm -7 --background "#FF0088" |hexdump -C 00000000 1b 5b 34 38 3b 35 3b 31 39 38 6d |.[48;5;198m| 0000000b % TERM=ansi COLORTERM=16color setterm -7 --background "#FF0088" |hexdump -C 00000000 1b 5b 31 30 35 6d |.[105m| 00000006 % TERM=ansi setterm -7 --background "#FF0088" |hexdump -C 00000000 1b 5b 34 35 6d |.[45m| 00000005 %
setterm
. nosh Guide. Softwares.You could change the background colour of the terminal with:
printf '\e]11;%s\a' '#ff0000'
Which seems to work with xterm
, VTE-based terminals (like gnome-terminal
), konsole
and rxvt
at least.
You can also change other colours than the background's if you prefer. Like change the colour 1 and display a rectangle in that colour with:
printf '\e]4;1;%s\a\e[0;41m \n \n\e[m' '#ff0000'
To display more than one colour:
show_colour() {
for i do
printf '\e]4;%d;%s\a\e[0;48;5;%dm%s\e[m\n' "$#" "$i" "$#" "$i"
shift
done
}
show_colour black purple green '#ff0000'
That does permanently change the palette for that emulator window though. Use tput oc
to restore the default colours.
Other option could be to run:
xlogo -bg '#ff0000'
Or
rxvt -bg '#ff0000'
printf '\e]4;1;%s\a\e[0;41m \n \n\e[m' '#ff0000'
– eadmaster
Jul 12 '20 at 05:53
In the KDE terminal program konsole version 21.12.3 (https://konsole.kde.org/), if you hover over a hex color code it will show you that color as a pop-up square.
xlogo -bg '#ff0000'
) is OK? – Stéphane Chazelas Nov 19 '18 at 13:24\e[38;2;213;117;37m
. Similar for the bg:\e[48;2;37;213;117m
. Or you already know that but don't know how to parse and split hex specs in the shell? Are you trying to modify the palette (what std colors 1,2,3 stand for)? etc. – Nov 19 '18 at 16:15