Listing all color names

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

Listing all color names

#1 Post by MochiMoppel »

Problem 1
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
Listing with awk
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
Showing the colors
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
    [EDIT 2020-5-21] The segmentation fault problem can be solved by increasing the vboxes and reducing the number of widgets in each box. This should show all color names:

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
Should this go into the HOWTO forum? - Ah... maybe not. No pet file :lol:
Attachments
color_names.png
(15.77 KiB) Downloaded 664 times
Last edited by MochiMoppel on Thu 21 May 2020, 06:31, edited 1 time in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#2 Post by musher0 »

Hello MochiMoppei.

Interesting work.

For more named colors, you may find this site of interest.

IHTH.

Edit, 10 miutes later:
I suppose you know you did the above that as pure exercise to keep your
Linux muscles in good shape? ;)

I am teasing you in hopefully a friendly manner because gcolor2 is already
designed to offer its users what you no doubt took pains to offer us above
-- if you put a link of /usr/share/X11/rgb.txt in /root, like so:

Code: Select all

ln -s /usr/share/X11/rgb.txt /root/.rgb
I have been using this trick since DPup-4.82.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#3 Post by MochiMoppel »

Yad can read the file /usr/share/X11/rgb.txt directly ... or any other file with user defined colors.

Code: Select all

yad --color --gtk-palette --expand-palette --palette=/usr/share/X11/rgb.txt
Nice to have a sort option. With hex values sorted it becomes clear how many duplicates the rgb.txt file contains.
Attachments
yad_color_dialog.png
(70 KiB) Downloaded 627 times

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#4 Post by vovchik »

Dear MochiMoppel,

Thanks. The AWK gtkdialog version is very clever, and the YAD version is very fast and convenient. I have added a self-contained icon and copy the selection result to the clipboard in this little mod.

With kind regards,
vovchik
Attachments
yadcol.tar.gz
(1.76 KiB) Downloaded 219 times

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#5 Post by musher0 »

Hi guys.

gcolor2 has a sort too.

The rgb.txt list itself originates from X.org. I know because there is a note
inside the file.

It has duplicates because, among other things, of the American and
British spelling of "grey" (vs "gray").

IMO you have done this for nothing. It already existed and still exists.
But hey, who am I to decide of one's pastimes ?

I'm trying to figure out why. Please tell me. You have never tried gcolor2
before, and you created your YAD widget thinking there was a genuine
need? Is that it? Without researching if something similar already existed?

One should always do good research before setting on a project, to avoid
duplications. Tsk, tsk, this is such a waste of everybody's time and of your
talent... (IMO)

Sorry for being so frank.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
perdido
Posts: 1528
Joined: Mon 09 Dec 2013, 16:29
Location: ¿Altair IV , Just north of Eeyore Junction.?

#6 Post by perdido »

Hi MochiMoppel,

A little bit off-topic but still about colors :)

What forum code did you use to insert the colors in your first post?
██████#6B8E23 OliveDrab
██████ #EECBAD PeachPuff2
██████ #1874CD DodgerBlue3

I didn't know those colors were available :shock:

.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#7 Post by musher0 »

MochiMoppel wrote:Yad can read the file /usr/share/X11/rgb.txt directly ... or any other file with user defined colors.

Code: Select all

yad --color --gtk-palette --expand-palette --palette=/usr/share/X11/rgb.txt
(...)
One should investigate if this is not a case of the YAD people plagiarizing
the work of the gcolor2 people. We should check the dates of creation of
each, which one came first.

gcolor2 has been around for a long time. And its deisgn is more horizontal
compared to the result of the above yad command.

Just a thought.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#8 Post by Keef »

They both use gtk widgets, some similarity is to be expected.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#9 Post by fredx181 »

Great info ! Thanks @MochiMoppel

Fred

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#10 Post by step »

Thank you very much, MochiMoppel and vovchik.

@vovchik, nice icon! Please try selecting color "blue2", which string gets copied to the clipboard for you?
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#11 Post by puppy_apprentice »

perdido wrote:Hi MochiMoppel,

A little bit off-topic but still about colors :)

What forum code did you use to insert the colors in your first post?
██████#6B8E23 OliveDrab
██████ #EECBAD PeachPuff2
██████ #1874CD DodgerBlue3

I didn't know those colors were available :shock:

.

Code: Select all

[color=#6B8E23]OliveDrab[/color]
Last edited by puppy_apprentice on Wed 06 Feb 2019, 23:57, edited 2 times in total.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#12 Post by musher0 »

Edit, about an hour later:
superseded by v. 0.2, a couple of posts below.

~~~~~~~~~~~
vovchik wrote:Dear MochiMoppel,

Thanks. The AWK gtkdialog version is very clever, and the YAD version is
very fast and convenient. I have added a self-contained icon and copy the
selection result to the clipboard in this little mod.

With kind regards,
vovchik
Ah.

Finally I understand. I was wondering what the fuss was about.

Now, this is original. It adds something and is worth keeping.

I reworked vovchik's archive into a pet. (Please see attached.) I added
a desktop file for it, so it will show in the WM's menu under "Graphics
Utilities" as "yadcol".

Do not enjoy! ;)
Attachments
yadcol-0.1.pet
(9.16 KiB) Downloaded 185 times
named-colors.jpg
This is what it looks like on the wmx WM.
(158.28 KiB) Downloaded 493 times
Last edited by musher0 on Thu 07 Feb 2019, 01:38, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
perdido
Posts: 1528
Joined: Mon 09 Dec 2013, 16:29
Location: ¿Altair IV , Just north of Eeyore Junction.?

#13 Post by perdido »

puppy_apprentice wrote:
perdido wrote:Hi MochiMoppel,

A little bit off-topic but still about colors :)

What forum code did you use to insert the colors in your first post?
██████#6B8E23 OliveDrab
██████ #EECBAD PeachPuff2
██████ #1874CD DodgerBlue3

I didn't know those colors were available :shock:

.

Code: Select all

[color=#6B8E23]OliveDrab[/color]
Thank You puppy_apprentice!

That is a nice option I did not know about.:)

.

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#14 Post by musher0 »

Hello all.

V. 0.2:

Code: Select all

#!/bin/ash
# (c) "vovchik", Febr. 6, 2019
# Incorporating the hexa color site's colors to the old Xorg ones.
# Adaptation: musher0, Febr. 6, 2019
####
# icon # Since it's been created and it's now stored in
# /usr/share/icons, do we need it here? # musher0
mycol=$(yad --window-icon="/usr/share/icons/named_colors.svg" \
     --title "Named Colours" --width=640 --height=400 \
	--color --gtk-palette --expand-palette \
	--palette=/usr/local/share/more-RGB-colors.txt3)
echo "$mycol"
echo -n "$mycol" | xclip -selection clipboard
exit

Notes
Changed width and height, so there is less "white" underneath
the color names window.
# When run from console, gives error: 
# (yad:11119): Gtk-WARNING **: Allocating size to yad-dialog-window 
# 0x6c32e8 without calling gtk_widget_get_preferred_width/height(). How 
# does the code know the size to allocate? 
# (Automatic comment, BTW; not by me.)
As I say in the comments in the script,
-- I changed the width and height of the display, so there is less "white"
underneath the color names window. It fits tighter.

-- I removed the icon creation part (with apologies to vovchik), since it's
been created and it's now stored in /usr/share/icons,

-- this version incorporates the hexa site list of color names (which I
mentioned in post # 2 above) to our traditional Xorg list. There are
approx 1,425 lines in this list, if you exclude the 3 or 4 "!" comment lines.
This list contains approx. twice the color names that the default "rgb.txt"
Xorg list offers.

I used the -dump from links and some awk code to extract the hexa list,
and afterwards did a manual, "de visu", edit. There may still be
inconsistencies in the colors, however, I am not a color expert.
Kindly forgive and report below? I'll do my best to correct them.

-- Concerning the run error: I do not know YAD, so I cannot correct this
error. Is anyone up to it? But the script is working fine AFAICT.

BFN.
Attachments
named-colors-new-look.jpg
Again on wmx WM. YMMV.
(170.52 KiB) Downloaded 473 times
yadcol-0.2.pet
This one incorporates the hexa color names list from post no 2 above.
(19.14 KiB) Downloaded 182 times
Last edited by musher0 on Thu 07 Feb 2019, 18:22, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#15 Post by step »

These lines

Code: Select all

	--palette=/usr/share/X11/rgb.txt)
echo "$mycol"
echo -n "$mycol" | xclip -selection clipboard
could be changed to

Code: Select all

	--palette=/usr/share/X11/rgb.txt) &&
echo "$mycol" &&
echo -n "$mycol" | xclip -selection clipboard
This way if you cancel the dialog existing clipboard contents won't be clobbered.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#16 Post by MochiMoppel »

vovchik wrote:I have added a self-contained icon and copy the selection result to the clipboard in this little mod.
Thank you. Great artwork - as always.

As for sending output to clipboard I see some issues.

- Adding to step's observation I would go further. IMO any overwriting of the clipboard should require the explicit consent of the user. Could be done with an extra button. Normally the GtkColorChooser writes to stdout (which your mod still does), but additionally writing to clipboard might be regarded as unexpected and unwanted.

- Some Puppies use xsel instead of xclip. Should be considered when coding clipboard interaction.

- Color hex values can already be easily selected and copied from the "Color name" field of the GtkColorChooser, so I don't see a need to make copying easier or even compulsory. On the other hand there is no way to copy the names :cry: (well, not with GtkColorChooser but with MochiMoppel's "waste of time" scripts :wink: )

- The GtkColorChooser already has a dedicated clipboard: the 20-field color palette. A bit feeble as the palette can not be saved but useful as long as the application is not closed (e.g. see implementation in Geany).

Instead of writing to the clipboad I think writing to a custom palette file would be a better feature. Yad can optionally return the decimal values instead of the hex values, though in a format that needs further tweaking. I don't know if for yad such saving and ideally updating the color name list is possible while keeping the dialog open, but at least it should be possible after dismissing the dialog.

tlchost
Posts: 2057
Joined: Sun 05 Aug 2007, 23:26
Location: Baltimore, Maryland USA
Contact:

foregound/background

#17 Post by tlchost »

What would be very helpful for folks developing web pages would be a way of manipulating the values of foreground and background colors and viewing the result on the screen...it could avoid unfortunate combinations like white/yellow or black/blue [/i]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#18 Post by MochiMoppel »

OK, here is my prototype for a yad color picker with save capability.
This capability is not meant for X11 colors and therefore slightly off-topic. Since X11 colors should not be messed with and mixed with other names, the save option is disabled for /usr/share/X11/rgb.txt but can be useful to create/read custom color palettes.

How to run the script:
1) Create a new file for the colors you are going to save. Example: File name mycolors.gpl in directory /root/.config/mm_yadcolors/.
The first line of the file should read "GIMP Palette" (mandatory for use in mtPaint) and the following lines should contain the palette name and one or more comments, starting with #
The content would look like this:
GIMP Palette
Name: My favorite colors
#


2) Run below script (let's call it mm_yadcolors) as

Code: Select all

mm_yadcolors  /root/.config/mm_yadcolors/mycolors.gpl
This should open the yad colorpicker with an empty color list and a "Save" button to save your custom colors.
If mm_yadcolors is run without a palette file as argument, the script will open /usr/share/X11/rgb.txt . No "Save" button.

The script saves the last used color and the last used window geometry to the /tmp directory. Change to the .config directory if these values need to survive a reboot.

[Update 2019-2-21] Added support for mtPaint/GIMP palette format.
[Update 2019-2-23] Removed demo code from script and reworded explanation.
[Update 2020-3-29] Fixed issue with newer yad version (thanks step)

Code: Select all

#!/bin/bash
## loads default X11 rgb palette or - when passed as parameter - any user defined color palette
## option to save user defined colors (disabled for X11 palette) 
## supports mtPaint and GIMP palette formats (read/write)  
## remembers last used color, window size and location 

[[ -f $1 ]] && palettefile=$1 || palettefile=/usr/share/X11/rgb.txt
[[ $palettefile = /usr/share/X11/rgb.txt ]] && enable= || enable=true
export geofile=/tmp/yadcol_geometry.txt 
    valfile=/tmp/yadcol_lastused.txt 

function savegeo { 
 WINDOWID=$(xprop -root _NET_ACTIVE_WINDOW) 
 WINDOWID=${WINDOWID##* } 
 XWININFO=$(xwininfo -id $WINDOWID) 
 set -- ${@} ${XWININFO#* X: } 
 AX=$1 AY=$5 RX=$9 RY=${13} 
 echo -n "${15}x${17}+$((AX-RX))+$((AY-RY))" > $geofile 
};export -f savegeo

fileheader=$(grep '^[^ 0-9].*' -o "$palettefile")   #extract GIMP file header
[[ $fileheader ]] && fileheader=${fileheader}$'\n'  #keep variable empty if header does not exist 

while : ;do 
 colorval=$(yad --color --title="YAD - ${palettefile##*/}" --init-color="$(< $valfile)"  --geometry="$(< $geofile)" --always-print-result --gtk-palette --expand-palette --palette=<(awk "/^[ 0-9]/" "$palettefile") ${enable:+--button="Save color:bash -c 'savegeo; kill -s SIGUSR2 \$YAD_PID'"} --button="OK:bash -c 'savegeo; kill -s SIGUSR1 \$YAD_PID'" --button="Cancel:9" )
 ret=$? 
 case $ret in 
 0) : OK; echo -n $colorval; echo $colorval > "$valfile" ; break ;; 
 1) : SAVE; newname=$(gxmessage -title "Save color" -bg $colorval -c -entrytext 'NewColorName' $'\n\t'"Color $colorval") 
      (($?)) || printf '%s%d %d %d\t%s\n%s\n' "$fileheader" 0\x${colorval:1:2} 0\x${colorval:3:2} 0\x${colorval:5:2} "$newname" "$(awk '/^[ 0-9]/' "$palettefile")" > "$palettefile" 
      echo $colorval > "$valfile" ;; 
 *) : CANCEL; break ;; #ret = 9 or 252 
 esac 
done
Attachments
yad_custom_color_dialog.png
(56.01 KiB) Downloaded 1103 times
Last edited by MochiMoppel on Sun 29 Mar 2020, 01:07, edited 4 times in total.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#19 Post by MochiMoppel »

I made some changes to above script and added support for mtPaint/GIMP palette formats.
Files created with the script such as the included demo palette can be loaded into mtPaint and will be displayed as an indexed palette. Unfortunately mtPaint AFAIK has no support for color names.
Reading mtPaint/GIMP files was less of a problem before but their unsupported file headers produced ugly gibberish. Reading should now work without flaws (tested with palettes included in GIMP).

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

Re: foregound/background

#20 Post by musher0 »

tlchost wrote:What would be very helpful for folks developing web pages would be a way of manipulating the values of foreground and background colors and viewing the result on the screen...it could avoid unfortunate combinations like white/yellow or black/blue [/i]
Thanks tlchost, MochiMoppei.

I'd add to tlchost's suggestion -- which is sorely needed, I think, -- that it
would be nice to be able to save maybe up to ten colors, and that this
"color yadget" does not close after each choice.

When I develop an urxvt pseudo-GUI window, I find myself scribbling a lot
of hex colors on a sheet of paper. Having a list handy in an editor could
save time.

As to your approach of leaving rgb.txt alone, I think it is a good idea,
although gcolor2 allows saving new color names in the rgb.txt list.

My 2 ¢. BFN.
Last edited by musher0 on Thu 21 Feb 2019, 15:45, edited 1 time in total.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply