Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Web-Safe Color Chart

by Zecho (Hermit)
on Oct 05, 2001 at 10:24 UTC ( #116922=perlquestion: print w/replies, xml ) Need Help??

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

This was inspired by a javascript I had seen somewhere once, and I found it quite usefull. So as a service to all non-javascript users (namely me) I wrote this perl version.

So to the point, it works! but... other than the non use of CGI.pm I think it still could be improved a bit.. please try it out on your machine or visit this demo so you can get the whole picture.

Make sure you use the updated code below, and not the first version.

Any suggestions would be greatly appreciated!
#!/usr/bin/perl -w use strict; my @nums = qw/00 20 40 60 80 A0 C0 FF/; my $f=0; my $g=0; my $h=0; print "Content-type: text/html\n\n"; print '<html><head><title>Web-Safe Colors</title><head>'; print '<body><table align=center border=0><tr><td>'; while($f<8) { $g=0; $h=0; print '<table width=100% border=1 cellpadding=5>'; while ($g<8){ $h=0; print "<tr>\n"; while ($h<8){ print "<td align=center bgcolor=\"\#$nums[$f]$nums[$g] +$nums[$h]\">"; print "<font color=\"\#$nums[7-$f]$nums[7-$g]$nums[7-$ +h]\">"; print "$nums[$f]$nums[$g]$nums[$h]<\/font><\/td> "; + $h+=1; } print '</tr>'; $g+=1; } print "<\/table>\n<p>"; $f+=1; } print '</td></tr></table></body></html>';
I plan to rewrite it using CGI as soon as I learn CGI.

Replies are listed 'Best First'.
Sneaky glob trick (was Web-Safe Color Chart)
by blakem (Monsignor) on Oct 05, 2001 at 12:46 UTC
    fore!!
    perl -e'print"$_\n"for glob("{00,20,40,60,80,A0,C0,FF}"x3);'
    Update: trimmed a dozen chars thanks to CheeseLord
    perl -le'print for glob"{00,33,66,99,CC,FF}"x3'
    Update 2: I don't fully understand the meat of this myself (i think it came from tilly) If anyone knows how this works, I'd actually love an explanation, especially since the glob docs aren't very helpful.

    Update 3: Indeed it was tilly that showed me this trick. Original node at Re (tilly) 2: Sort of like a file handle, but not.

    -Blake

      First of all, glob expands the pattern according to shell conventions, so

      glob "foob{a,b,c}r"
      returns the list
      ('foobar', 'foobbr', 'foobcr')
      just like bash would return 'foobar foobbr foobcr' expandig metacharacters

      The x operator applied to a string repeats the string the specified number of times, so

      "{a,b,c}" x 3 evaluates as "{a,b,c}{a,b,c}{a,b,c}"

      When you feed this expression to glob, the result is a list of all the possible combinations of a, b and c in a string of three characters, from 'aaa' to 'ccc'.

      This could be easily achieved through magical string autoincrement, but the beauty of this method (very nice, I love it!) is that it applies to any set of strings to combine, not just characters.

      -- TMTOWTDI

        Thanks for the explanation... I've searched in vain for the node where I originally found this trick. Here is the code i squirrled away from it though:
        #!/usr/bin/perl print map "$_\n", glob("{A,B,C}{a,b,c}");
        I guess the part that puzzled me is that glob *usually* interacts with the filesystem. glob("*.pl") will return list of perl scripts in your cwd.... why aren't the values above checked against the filesystem for actual files?

        Update: The code above came from Re (tilly) 2: Sort of like a file handle, but not.

        -Blake

      nice one!

      Can you please explain the use of glob here?


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
Re: Web-Safe Color Chart
by merlyn (Sage) on Oct 05, 2001 at 10:48 UTC
Re: Web-Safe Color Chart
by trantor (Chaplain) on Oct 05, 2001 at 10:57 UTC

    As far as I know, and as merlyn pointed out, a Web safe palette should consist of 6 * 6 * 6 = 216 colours only.

    Besides this, an obviuos improvement in the readability of your code could be using foreach instead of while loops, e.g.:

    my @nums = qw/00 33 66 99 CC FF/; foreach my $r (@nums) { foreach my $g (@nums) { foreach my $b (@nums) { # Do something with $r, $g and $b } } }

    The usual way of representing this palette is either an 8 * 8 grid (using only the first 216 cells) or 6 tables, 6 * 6 each. The second approach is easier to program, the first more compact.

    Speaking about CGI.pm, since you don't process input parameters, can't generate errors, always use the same Content-type, there are no big security concerns that should force you to use CGI instead of rolling your own. Nonetheless it is a good exercise and a good starting point for studying the module, which should be customary for more complex CGIs.

    -- TMTOWTDI

Re: Web-Safe Color Chart
by Zecho (Hermit) on Oct 05, 2001 at 11:25 UTC
    ack... I don't know where I got those values.. (actually I do and am ashamed to admit it) I have recoded it to meet the spec and the corrected version is below. The demo has been updated to the new code as well
    Thanks merlyn & trantor for pointing that out

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://116922]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2020-12-01 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (20 votes). Check out past polls.

    Notices?