pra has asked for the wisdom of the Perl Monks concerning the following question:

What is the best way to find out if a given string is a valid color? I am writing a Perl/TK program that will read in a file which contains color names. Users might not be aware of which names are allowed, or could mis-type something.

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?

Certainly, if I use an incorrect color name, I get an error message. Could I make use of the verification system being used there, and if so, how?

  • Comment on Best way to validate a string as a color?

Replies are listed 'Best First'.
Re: Best way to validate a string as a color?
by tybalt89 (Monsignor) on Oct 17, 2017 at 18:58 UTC

    eval {} is your friend :)

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1201508 use strict; use warnings; my @colors = qw( red green nosuchcolor blue ); my $defaultcolor = 'black'; use Tk; my $mw = MainWindow->new; for my $color ( @colors ) { eval { $mw->Label( -text => $color, -fg => $color )->pack; 1; } // $mw->Label( -text => $color, -fg => $defaultcolor )->pack; } MainLoop;

      I was thinking on other lines! Thank you, eval should do the job.

Re: Best way to validate a string as a color?
by thanos1983 (Parson) on Oct 17, 2017 at 16:19 UTC

    Hello pra,

    Welcome to the Monastery. What about Term::ANSIColor/Function Interface/colorvalid, sample of code bellow:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Term::ANSIColor qw(colorvalid); use Term::ANSIScreen qw/:color :constants/; my $valid = colorvalid ('magenta'); say "Color string is ", $valid ? "valid" : "invalid"; say MAGENTA . "Magenta" . RESET; __END__ $ perl test.pl Color string is valid Magenta

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

        You misspelled "color." So, close though... :P

        perl -MString::Approx=amatch -le 'print "OK" if amatch("color", "colou +r")' OK
Re: Best way to validate a string as a color?
by afoken (Chancellor) on Oct 17, 2017 at 19:17 UTC
    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". ;-)

      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

      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.

A way to validate a string as a color:
by holli (Abbot) on Oct 17, 2017 at 19:38 UTC
    The hash below contains a combined list of X11 and CSS3 color names.
    Where names clash, the web version has "web" prepended, eg. x11 "green", css "webgreen". Also see this.

    my %colors = ( 'gray' => { rgb => [0xbe, 0xbe, 0xbe], name => 'Gray' +}, 'green' => { rgb => [0x00, 0xff, 0x00], name => 'Green' + }, 'maroon' => { rgb => [0xb0, 0x30, 0x60], name => 'Maroon +' }, 'purple' => { rgb => [0xa0, 0x20, 0xf0], name => 'Purple +' }, 'webgray' => { rgb => [0x80, 0x80, 0x80], name => 'Web Gr +ay' }, 'webgreen' => { rgb => [0x00, 0x80, 0x00], name => 'Web Gr +een' }, 'webmaroon' => { rgb => [0x7f, 0x00, 0x00], name => 'Web Ma +roon' }, 'webpurple' => { rgb => [0x7f, 0x00, 0x7f], name => 'Web Pu +rple' }, 'aliceblue' => { rgb => [0xf0, 0xf8, 0xff], name => 'Alice +Blue' }, 'antiquewhite' => { rgb => [0xfa, 0xeb, 0xd7], name => 'Antiqu +e White' }, 'aqua' => { rgb => [0x00, 0xff, 0xff], name => 'Aqua' +}, 'aquamarine' => { rgb => [0x7f, 0xff, 0xd4], name => 'Aquama +rine' }, 'azure' => { rgb => [0xf0, 0xff, 0xff], name => 'Azure' + }, 'beige' => { rgb => [0xf5, 0xf5, 0xdc], name => 'Beige' + }, 'bisque' => { rgb => [0xff, 0xe4, 0xc4], name => 'Bisque +' }, 'black' => { rgb => [0x00, 0x00, 0x00], name => 'Black' + }, 'blanchedalmond' => { rgb => [0xff, 0xeb, 0xcd], name => 'Blanch +ed Almond' }, 'blue' => { rgb => [0x00, 0x00, 0xff], name => 'Blue' +}, 'blueviolet' => { rgb => [0x8a, 0x2b, 0xe2], name => 'Blue V +iolet' }, 'brown' => { rgb => [0xa5, 0x2a, 0x2a], name => 'Brown' + }, 'burlywood' => { rgb => [0xde, 0xb8, 0x87], name => 'Burlyw +ood' }, 'cadetblue' => { rgb => [0x5f, 0x9e, 0xa0], name => 'Cadet +Blue' }, 'chartreuse' => { rgb => [0x7f, 0xff, 0x00], name => 'Chartr +euse' }, 'chocolate' => { rgb => [0xd2, 0x69, 0x1e], name => 'Chocol +ate' }, 'coral' => { rgb => [0xff, 0x7f, 0x50], name => 'Coral' + }, 'cornflower' => { rgb => [0x64, 0x95, 0xed], name => 'Cornfl +ower' }, 'cornsilk' => { rgb => [0xff, 0xf8, 0xdc], name => 'Cornsi +lk' }, 'crimson' => { rgb => [0xdc, 0x14, 0x3c], name => 'Crimso +n' }, 'cyan' => { rgb => [0x00, 0xff, 0xff], name => 'Cyan' +}, 'darkblue' => { rgb => [0x00, 0x00, 0x8b], name => 'Dark B +lue' }, 'darkcyan' => { rgb => [0x00, 0x8b, 0x8b], name => 'Dark C +yan' }, 'darkgoldenrod' => { rgb => [0xb8, 0x86, 0x0b], name => 'Dark G +oldenrod' }, 'darkgray' => { rgb => [0xa9, 0xa9, 0xa9], name => 'Dark G +ray' }, 'darkgreen' => { rgb => [0x00, 0x64, 0x00], name => 'Dark G +reen' }, 'darkkhaki' => { rgb => [0xbd, 0xb7, 0x6b], name => 'Dark K +haki' }, 'darkmagenta' => { rgb => [0x8b, 0x00, 0x8b], name => 'Dark M +agenta' }, 'darkolivegreen' => { rgb => [0x55, 0x6b, 0x2f], name => 'Dark O +live Green' }, 'darkorange' => { rgb => [0xff, 0x8c, 0x00], name => 'Dark O +range' }, 'darkorchid' => { rgb => [0x99, 0x32, 0xcc], name => 'Dark O +rchid' }, 'darkred' => { rgb => [0x8b, 0x00, 0x00], name => 'Dark R +ed' }, 'darksalmon' => { rgb => [0xe9, 0x96, 0x7a], name => 'Dark S +almon' }, 'darkseagreen' => { rgb => [0x8f, 0xbc, 0x8f], name => 'Dark S +ea Green' }, 'darkslateblue' => { rgb => [0x48, 0x3d, 0x8b], name => 'Dark S +late Blue' }, 'darkslategray' => { rgb => [0x2f, 0x4f, 0x4f], name => 'Dark S +late Gray' }, 'darkturquoise' => { rgb => [0x00, 0xce, 0xd1], name => 'Dark T +urquoise' }, 'darkviolet' => { rgb => [0x94, 0x00, 0xd3], name => 'Dark V +iolet' }, 'deeppink' => { rgb => [0xff, 0x14, 0x93], name => 'Deep P +ink' }, 'deepskyblue' => { rgb => [0x00, 0xbf, 0xff], name => 'Deep S +ky Blue' }, 'dimgray' => { rgb => [0x69, 0x69, 0x69], name => 'Dim Gr +ay' }, 'dodgerblue' => { rgb => [0x1e, 0x90, 0xff], name => 'Dodger + Blue' }, 'firebrick' => { rgb => [0xb2, 0x22, 0x22], name => 'Firebr +ick' }, 'floralwhite' => { rgb => [0xff, 0xfa, 0xf0], name => 'Floral + White' }, 'forestgreen' => { rgb => [0x22, 0x8b, 0x22], name => 'Forest + Green' }, 'fuchsia' => { rgb => [0xff, 0x00, 0xff], name => 'Fuchsi +a' }, 'gainsboro' => { rgb => [0xdc, 0xdc, 0xdc], name => 'Gainsb +oro' }, 'ghostwhite' => { rgb => [0xf8, 0xf8, 0xff], name => 'Ghost +White' }, 'gold' => { rgb => [0xff, 0xd7, 0x00], name => 'Gold' +}, 'goldenrod' => { rgb => [0xda, 0xa5, 0x20], name => 'Golden +rod' }, 'greenyellow' => { rgb => [0xad, 0xff, 0x2f], name => 'Green +Yellow' }, 'honeydew' => { rgb => [0xf0, 0xff, 0xf0], name => 'Honeyd +ew' }, 'hotpink' => { rgb => [0xff, 0x69, 0xb4], name => 'Hot Pi +nk' }, 'indianred' => { rgb => [0xcd, 0x5c, 0x5c], name => 'Indian + Red' }, 'indigo' => { rgb => [0x4b, 0x00, 0x82], name => 'Indigo +' }, 'ivory' => { rgb => [0xff, 0xff, 0xf0], name => 'Ivory' + }, 'khaki' => { rgb => [0xf0, 0xe6, 0x8c], name => 'Khaki' + }, 'lavender' => { rgb => [0xe6, 0xe6, 0xfa], name => 'Lavend +er' }, 'lavenderblush' => { rgb => [0xff, 0xf0, 0xf5], name => 'Lavend +er Blush' }, 'lawngreen' => { rgb => [0x7c, 0xfc, 0x00], name => 'Lawn G +reen' }, 'lemonchiffon' => { rgb => [0xff, 0xfa, 0xcd], name => 'Lemon +Chiffon' }, 'lightblue' => { rgb => [0xad, 0xd8, 0xe6], name => 'Light +Blue' }, 'lightcoral' => { rgb => [0xf0, 0x80, 0x80], name => 'Light +Coral' }, 'lightcyan' => { rgb => [0xe0, 0xff, 0xff], name => 'Light +Cyan' }, 'lightgoldenrod' => { rgb => [0xfa, 0xfa, 0xd2], name => 'Light +Goldenrod' }, 'lightgray' => { rgb => [0xd3, 0xd3, 0xd3], name => 'Light +Gray' }, 'lightgreen' => { rgb => [0x90, 0xee, 0x90], name => 'Light +Green' }, 'lightpink' => { rgb => [0xff, 0xb6, 0xc1], name => 'Light +Pink' }, 'lightsalmon' => { rgb => [0xff, 0xa0, 0x7a], name => 'Light +Salmon' }, 'lightseagreen' => { rgb => [0x20, 0xb2, 0xaa], name => 'Light +Sea Green' }, 'lightskyblue' => { rgb => [0x87, 0xce, 0xfa], name => 'Light +Sky Blue' }, 'lightslategray' => { rgb => [0x77, 0x88, 0x99], name => 'Light +Slate Gray' }, 'lightsteelblue' => { rgb => [0xb0, 0xc4, 0xde], name => 'Light +Steel Blue' }, 'lightyellow' => { rgb => [0xff, 0xff, 0xe0], name => 'Light +Yellow' }, 'lime' => { rgb => [0x00, 0xff, 0x00], name => 'Lime' +}, 'limegreen' => { rgb => [0x32, 0xcd, 0x32], name => 'Lime G +reen' }, 'linen' => { rgb => [0xfa, 0xf0, 0xe6], name => 'Linen' + }, 'magenta' => { rgb => [0xff, 0x00, 0xff], name => 'Magent +a' }, 'mediumaquamarine' => { rgb => [0x66, 0xcd, 0xaa], name => 'Medium + Aquamarine' }, 'mediumblue' => { rgb => [0x00, 0x00, 0xcd], name => 'Medium + Blue' }, 'mediumorchid' => { rgb => [0xba, 0x55, 0xd3], name => 'Medium + Orchid' }, 'mediumpurple' => { rgb => [0x93, 0x70, 0xdb], name => 'Medium + Purple' }, 'mediumseagreen' => { rgb => [0x3c, 0xb3, 0x71], name => 'Medium + Sea Green' }, 'mediumslateblue' => { rgb => [0x7b, 0x68, 0xee], name => 'Medium + Slate Blue' }, 'mediumspringgreen' => { rgb => [0x00, 0xfa, 0x9a], name => 'Medium + Spring Green' }, 'mediumturquoise' => { rgb => [0x48, 0xd1, 0xcc], name => 'Medium + Turquoise' }, 'mediumvioletred' => { rgb => [0xc7, 0x15, 0x85], name => 'Medium + Violet Red' }, 'midnightblue' => { rgb => [0x19, 0x19, 0x70], name => 'Midnig +ht Blue' }, 'mintcream' => { rgb => [0xf5, 0xff, 0xfa], name => 'Mint C +ream' }, 'mistyrose' => { rgb => [0xff, 0xe4, 0xe1], name => 'Misty +Rose' }, 'moccasin' => { rgb => [0xff, 0xe4, 0xb5], name => 'Moccas +in' }, 'navajowhite' => { rgb => [0xff, 0xde, 0xad], name => 'Navajo + White' }, 'navyblue' => { rgb => [0x00, 0x00, 0x80], name => 'Navy B +lue' }, 'oldlace' => { rgb => [0xfd, 0xf5, 0xe6], name => 'Old La +ce' }, 'olive' => { rgb => [0x80, 0x80, 0x00], name => 'Olive' + }, 'olivedrab' => { rgb => [0x6b, 0x8e, 0x23], name => 'Olive +Drab' }, 'orange' => { rgb => [0xff, 0xa5, 0x00], name => 'Orange +' }, 'orangered' => { rgb => [0xff, 0x45, 0x00], name => 'Orange + Red' }, 'orchid' => { rgb => [0xda, 0x70, 0xd6], name => 'Orchid +' }, 'palegoldenrod' => { rgb => [0xee, 0xe8, 0xaa], name => 'Pale G +oldenrod' }, 'palegreen' => { rgb => [0x98, 0xfb, 0x98], name => 'Pale G +reen' }, 'paleturquoise' => { rgb => [0xaf, 0xee, 0xee], name => 'Pale T +urquoise' }, 'palevioletred' => { rgb => [0xdb, 0x70, 0x93], name => 'Pale V +iolet Red' }, 'papayawhip' => { rgb => [0xff, 0xef, 0xd5], name => 'Papaya + Whip' }, 'peachpuff' => { rgb => [0xff, 0xda, 0xb9], name => 'Peach +Puff' }, 'peru' => { rgb => [0xcd, 0x85, 0x3f], name => 'Peru' +}, 'pink' => { rgb => [0xff, 0xc0, 0xcb], name => 'Pink' +}, 'plum' => { rgb => [0xdd, 0xa0, 0xdd], name => 'Plum' +}, 'powderblue' => { rgb => [0xb0, 0xe0, 0xe6], name => 'Powder + Blue' }, 'rebeccapurple' => { rgb => [0x66, 0x33, 0x99], name => 'Rebecc +a Purple' }, 'red' => { rgb => [0xff, 0x00, 0x00], name => 'Red' } +, 'rosybrown' => { rgb => [0xbc, 0x8f, 0x8f], name => 'Rosy B +rown' }, 'royalblue' => { rgb => [0x41, 0x69, 0xe1], name => 'Royal +Blue' }, 'saddlebrown' => { rgb => [0x8b, 0x45, 0x13], name => 'Saddle + Brown' }, 'salmon' => { rgb => [0xfa, 0x80, 0x72], name => 'Salmon +' }, 'sandybrown' => { rgb => [0xf4, 0xa4, 0x60], name => 'Sandy +Brown' }, 'seagreen' => { rgb => [0x2e, 0x8b, 0x57], name => 'Sea Gr +een' }, 'seashell' => { rgb => [0xff, 0xf5, 0xee], name => 'Seashe +ll' }, 'sienna' => { rgb => [0xa0, 0x52, 0x2d], name => 'Sienna +' }, 'silver' => { rgb => [0xc0, 0xc0, 0xc0], name => 'Silver +' }, 'skyblue' => { rgb => [0x87, 0xce, 0xeb], name => 'Sky Bl +ue' }, 'slateblue' => { rgb => [0x6a, 0x5a, 0xcd], name => 'Slate +Blue' }, 'slategray' => { rgb => [0x70, 0x80, 0x90], name => 'Slate +Gray' }, 'snow' => { rgb => [0xff, 0xfa, 0xfa], name => 'Snow' +}, 'springgreen' => { rgb => [0x00, 0xff, 0x7f], name => 'Spring + Green' }, 'steelblue' => { rgb => [0x46, 0x82, 0xb4], name => 'Steel +Blue' }, 'tan' => { rgb => [0xd2, 0xb4, 0x8c], name => 'Tan' } +, 'teal' => { rgb => [0x00, 0x80, 0x80], name => 'Teal' +}, 'thistle' => { rgb => [0xd8, 0xbf, 0xd8], name => 'Thistl +e' }, 'tomato' => { rgb => [0xff, 0x63, 0x47], name => 'Tomato +' }, 'turquoise' => { rgb => [0x40, 0xe0, 0xd0], name => 'Turquo +ise' }, 'violet' => { rgb => [0xee, 0x82, 0xee], name => 'Violet +' }, 'wheat' => { rgb => [0xf5, 0xde, 0xb3], name => 'Wheat' + }, 'white' => { rgb => [0xff, 0xff, 0xff], name => 'White' + }, 'whitesmoke' => { rgb => [0xf5, 0xf5, 0xf5], name => 'White +Smoke' }, 'yellow' => { rgb => [0xff, 0xff, 0x00], name => 'Yellow +' }, 'yellowgreen' => { rgb => [0x9a, 0xcd, 0x32], name => 'Yellow + Green' } );


    holli

    You can lead your users to water, but alas, you cannot drown them.
      Also, there is App::ColorNamer, which uses the CSS colors only, but they too are just in an array in the sourcecode so you can easily tweak that to your needs.


      holli

      You can lead your users to water, but alas, you cannot drown them.

      Your list looks a little bit short:

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

      (From Slackware64 14.2)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        That's because rgb.txt contains a of duplicates and probably system dependent variations. I complied with the list on Wikipedia I linked to.


        holli

        You can lead your users to water, but alas, you cannot drown them.
Re: Best way to validate a string as a color?
by 1nickt (Canon) on Oct 17, 2017 at 15:43 UTC

    Maybe of use: String::Approx.


    The way forward always starts with a minimal test.