in reply to Best way to validate a string as a color?

It seems to me that I could compare with names from the list which on my Linux machine is in /usr/share/X11/rgb.txt (and add a check for colors of the form #rrggbb, no reason to exclude those). Can I expect that list to be standard on all systems (Linux, Windows, Macs)? Even if it is standard, is that the best way to do it?

Yes, /usr/share/X11/rgb.txt should be part of standard X11, so you should be able to use it on (almost) any Unix system running X11. Windows is something completely different. You won't find the /usr/share/X11 on a native Windows. MacOS X inherits from Unix, but does many things differently. AFAIK, you can run X11 on MacOS X, but it does not run X11 out of the box.

I doubt the content of rgb.txt changes very much, so a quick and dirty way would be to include that file with your program. On the other hand, some part of Perl/Tk must be able to handle colors even on Windows and MacOS X. So, maybe call a Tk method to convert a string to a color and check if it fails.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
  • Comment on Re: Best way to validate a string as a color?

Replies are listed 'Best First'.
Re^2: Best way to validate a string as a color?
by kcott (Archbishop) on Oct 18, 2017 at 04:32 UTC

    G'day Alexander,

    Just some supporting information regarding your "Mac" comments.

    "MacOS X inherits from Unix, but does many things differently."

    It does have the "rgb.txt" file but it's in a different directory: "/opt/X11/share/X11/". There's a symlink from "/usr/X11/" to "/opt/X11/", so these are the same file:

    $ ls -i1 /opt/X11/share/X11/rgb.txt /usr/X11/share/X11/rgb.txt 85658151 /opt/X11/share/X11/rgb.txt 85658151 /usr/X11/share/X11/rgb.txt

    I ran the code you showed below and got the same numbers:

    $ perl -nE '@x=split;$seen{join(" ",@x[0,1,2])}++;END{say 0+keys%seen} +' < /opt/X11/share/X11/rgb.txt 512 $ wc -l /opt/X11/share/X11/rgb.txt 782 /opt/X11/share/X11/rgb.txt

    I downloaded a copy of the Xorg version you linked below. After canonicalising the whitespace of that and my local copy with:

    $ perl -ne 's/[\t ]+/ /g; print' original_name > canonical_name

    A diff showed them to be identical.

    "AFAIK, you can run X11 on MacOS X, but it does not run X11 out of the box."

    That's correct on both counts.

    My version is 10.12.5 "macOS Sierra".

    [Yes, they've changed the name from "Mac OS X" to "macOS". I have no idea why. As far as I was aware, the "X" was intended to be a roman numeral referring to the "10.x.x" series: the major version is still "10". ]

    — Ken

Re^2: Best way to validate a string as a color?
by pra (Initiate) on Oct 18, 2017 at 11:20 UTC

    In that case, I can include the list as an appendix to the documentation. I find it a little odd: there's 100 shades of grey (and 100 shades of gray), but no indigo, crimson or scarlet. I was thinking of saying that all common colour names would be accepted, but it seems not all will.