Setting a color option for X applications usually requires to specify a color as RGB hex value (e.g. #FF0022) .
As an alternative to those cryptic hex values the user may use color names like "white" or "orange", which is fine, but more than 700 of such names exist, some of them exotic like
██████ #6B8E23 OliveDrab
██████ #EECBAD PeachPuff2
██████ #1874CD DodgerBlue3
So the question is: Where to find those names?
Well, usable color names are listed in the file /usr/share/X11/rgb.txt , but....
Problem 2
names in /usr/share/X11/rgb.txt are listed with their decimal RGB codes, e.g. "238 203 173 PeachPuff2" instead of "#EECBAD PeachPuff2". How to list all names with their hex codes?
Listing with sed
Using sed for this job may sound odd since sed is not really good at on-the-fly conversions. But it can be done. GNU sed knows an e flag for the s command (not to be confused with the -e option), which allows to process a match with a shell command and then replace the found match with the shell command output. My shell command of choice here is the bash built-in printf, which is able to convert decimal values to hex:
Code: Select all
sed -rn 's/(^[0-9 ]+)(.*)/printf "#%02X%02X%02X\t%s %s" \1 \2/ep' /usr/share/X11/rgb.txt
While sed works for me, I find it awfully slow. On my netbook it takes 5 sec.
I tried awk and find it to be 100 times (!) faster. After all awk comes with its own printf implementation and does not require to run a printf shell command for each of the 700+ lines. This should fly:
Code: Select all
awk 'NF<7 {printf "#%02X%02X%02X\t%s %s %s\n",$1,$2,$3,$4,$5,$6}' /usr/share/X11/rgb.txt
Above generated list can be used to create more sophisticated displays. The goal is to see the colors, not just the names. It should be possible to create a yad list, an svg image or a HTML page. I tried my luck with gtkdialog and its colorbutton widget. Simple code and it even works, though there is a catch: The number of widgets seems to be limited. Above a certain limit gtkdialog generates a "Segmentation fault" error. I therefore removed the numerous grey/gray variants and also all names with spaces and kept only their otherwise identical camelcase variants (e.g. removed "forest green" but kept "ForestGreen" - which could also be written "forestgreen" as names are not case sensitive):
Code: Select all
awk 'BEGIN {print "<window title=\"Color Names\" allow-shrink=\"true\" width-request=\"300\" height-request=\"500\"><vbox scrollable=\"true\" >"}
!/[Gg]r[ea]y/ && NF<5 {printf "%s%02X%02X%02X%s%02X%02X%02X %s%s\n","<hbox><colorbutton><default>#",$1,$2,$3,"</default></colorbutton><entry><default>\"#",$1,$2,$3,$4,"\"</default></entry></hbox>"}
END {print "</vbox></window>"}' /usr/share/X11/rgb.txt | gtkdialog -s
Code: Select all
awk 'BEGIN {print "<window title=\"Color Names\" allow-shrink=\"true\" width-request=\"300\" height-request=\"500\"><vbox scrollable=\"true\"><vbox>"}
i++==400 {print "</vbox><vbox>"} NF<7 {printf "%s%02X%02X%02X%s%02X%02X%02X %s %s %s %s\n","<hbox><colorbutton><default>#",$1,$2,$3,"</default></colorbutton><entry><default>\"#",$1,$2,$3,$4,$5,$6,"\"</default></entry></hbox>"}
END {print "</vbox></vbox></window>"}' /usr/share/X11/rgb.txt | gtkdialog -s