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?
|
|---|
| 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 :)
| [reply] [d/l] |
by pra (Initiate) on Oct 18, 2017 at 11:11 UTC | |
I was thinking on other lines! Thank you, eval should do the job. | [reply] |
|
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:
Hope this helps, BR.
Seeking for Perl wisdom...on the process of learning...not there...yet!
| [reply] [d/l] [select] |
by choroba (Cardinal) on Oct 17, 2017 at 16:38 UTC | |
| [reply] [d/l] |
by Your Mother (Archbishop) on Oct 17, 2017 at 17:00 UTC | |
You misspelled "color." So, close though... :P
| [reply] [d/l] |
by LanX (Saint) on Oct 17, 2017 at 17:51 UTC | |
by pra (Initiate) on Oct 18, 2017 at 11:07 UTC | |
by Your Mother (Archbishop) on Oct 18, 2017 at 15:16 UTC | |
| |
|
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". ;-) | [reply] |
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:
I ran the code you showed below and got the same numbers:
I downloaded a copy of the Xorg version you linked below. After canonicalising the whitespace of that and my local copy with:
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 | [reply] [d/l] [select] |
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. | [reply] |
|
A way to validate a string as a color:
by holli (Abbot) on Oct 17, 2017 at 19:38 UTC | |
Where names clash, the web version has "web" prepended, eg. x11 "green", css "webgreen". Also see this.
holli You can lead your users to water, but alas, you cannot drown them. | [reply] [d/l] |
by holli (Abbot) on Oct 17, 2017 at 19:41 UTC | |
holli You can lead your users to water, but alas, you cannot drown them. | [reply] [d/l] |
by afoken (Chancellor) on Oct 17, 2017 at 20:47 UTC | |
Your list looks a little bit short:
(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". ;-) | [reply] [d/l] |
by holli (Abbot) on Oct 17, 2017 at 20:55 UTC | |
<Reveal this spoiler or all in this thread> 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. | [reply] [d/l] |
by afoken (Chancellor) on Oct 17, 2017 at 21:06 UTC | |
by holli (Abbot) on Oct 17, 2017 at 21:45 UTC | |
|
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.
| [reply] |