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

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.

Replies are listed 'Best First'.
Re: A way to validate a string as a color:
by holli (Abbot) on Oct 17, 2017 at 19:41 UTC
    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.
Re: A way to validate a string as a color:
by afoken (Chancellor) on Oct 17, 2017 at 20:47 UTC

    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
      shitload
      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.

        Still, Slackware's rgb.txt defines 512 unique colors (and 270 dupes). The Xorg version is identical to the Slackware version.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)