Re: Best way to validate a string as a color?
by tybalt89 (Monsignor) on Oct 17, 2017 at 18:58 UTC
|
#!/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;
| [reply] [d/l] |
|
|
| [reply] |
Re: Best way to validate a string as a color?
by thanos1983 (Parson) on Oct 17, 2017 at 16:19 UTC
|
#!/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!
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] |
|
|
perl -MString::Approx=amatch -le 'print "OK" if amatch("color", "colou
+r")'
OK
| [reply] [d/l] |
|
|
|
|
|
|
|
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] |
|
|
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".
]
| [reply] [d/l] [select] |
|
|
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
|
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
>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". ;-)
| [reply] [d/l] |
|
|
| [reply] [d/l] |
|
|
|
|
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] |