5

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.

  • Do you want to display those colours in the terminal emulator window itself? Or starting a different X application (like xlogo -bg '#ff0000') is OK? – Stéphane Chazelas Nov 19 '18 at 13:24
  • I tried. It's ok. But if I want to display several colors, for example with a file containing color values on each line, maybe i'll prefer some color display like neofetch or pywal. Thanks. – Antharia Jack Nov 19 '18 at 14:09
  • 1
    It's not clear what your question is about. Please add more info to it. Are you trying to set the fg to an arbitrary color? You can do it in xterms and alike with eg. \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
  • Related: https://unix.stackexchange.com/a/269085/117549 – Jeff Schaller Nov 20 '18 at 02:31
  • I'm looking for a feature to simply visualize one or several colors instead of copy/paste an hexadecimal value somewhere else (in terminal.sexy for example). Think like in some code editors which allows it when you write css for example. Just to simplify color configuration (i3, rofi etc.). – Antharia Jack Dec 01 '18 at 20:02

3 Answers3

10

An alternative:

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"' "$@"
}

Another alternative:

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
%

Further reading

  • Jonathan de Boyne Pollard (2018). setterm. nosh Guide. Softwares.
JdeBP
  • 68,745
  • Note that the standard parameter string for truecolor escape sequences, as per ITU-T T.416, is "48:2:color-space-id:red:green:blue". Omitting color-space-id (and shifting the remaining parameters to the left) and/or using semicolons are common misinterpretations of the standard. – egmont Nov 19 '18 at 16:00
  • Nice solution! Just a note that TERM=xterm-256color works well in konsole also. I use your solution to print all the color values from css files at the console. – cmevoli Jan 30 '19 at 14:44
3

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'
  • Change background color of urxvt is ok. But something like neofetch colorscheme display will be great : https://www.ostechnix.com/wp-content/uploads/2016/06/neofetch.png – Antharia Jack Nov 19 '18 at 14:11
  • 1
    this example does not work for me except for red printf '\e]4;1;%s\a\e[0;41m \n \n\e[m' '#ff0000' – eadmaster Jul 12 '20 at 05:53
-1

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.