Is there a command to display colors when giving hex value in terminal?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

An alternative:

show_colour() {
    perl -e 'foreach $a(@ARGV){print "e[48:2::".join(":",unpack("C*",pack("H*",$a)))."m e[49m "};print "n"' "[email protected]"
}

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"' "[email protected]"
}

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.

Method 2

You could change the background colour of the terminal with:

printf 'e]11;%sa' '#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;%sae[0;41m   n   ne[m' '#ff0000'

To display more than one colour:

show_colour() {
  for i do
    printf 'e]4;%d;%sae[0;48;5;%dm%se[mn' "$#" "$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'

Method 3

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x