Since dooberwah wrote "Comments welcome", I thought I would toss some in.

First, I like what you did. It's fun. However, you're not taking full advantage of CGI.pm. I made a few changes to bring in closer to what I would call "production" code. This is a fun script you wrote and I didn't make these changes as criticism. I made them because I have spare time at work and this was fun to play with :)

Features:

#!/usr/bin/perl -T #By Ben Jacobs <dooberwah> 2001 # "Use": Colors a string with a gradient. The user inputs # the string and two colors in hexedcimal. # Comments welcome, I know this isn't a very efficient # script but it's the best I could come up with. use strict; use warnings; use CGI qw/:standard/; use HTML::Entities; # Set globals my $error = ''; my $string_length_limit = 50; $|++; # kill buffering # Grab params my $string = param('string'); my $bgcolor = param('bgcolor'); my $fromcolor = param('fromcolor'); my $tocolor = param('tocolor'); # untaint everything my ( $safe_string ) = ( $string =~ /^(.*)$/ ); $safe_string = encode_entities( $safe_string ); my ( $safe_bgcolor ) = ( $bgcolor =~ /^([a-fA-F0-9]{6})$/ ); my ( $safe_fromcolor ) = ( $fromcolor =~ /^([a-fA-F0-9]{6})$/ ); my ( $safe_tocolor ) = ( $tocolor =~ /^([a-fA-F0-9]{6})$/ ); # validate data if ( length $safe_string > $string_length_limit ) { $safe_string = substr $safe_string, 0, $string_length_limit; param( 'string', $safe_string ); # truncate param to 50 characters + so sticky fields # don't overflow $error .= p( 'Length of input string must not be longer than 50 ch +aracters' ); } if ( ! defined $safe_bgcolor ) { $error .= p( '"Background Color" must be a six digit hexadecimal n +umber.' ); } if ( ! defined $safe_fromcolor ) { $error .= p( '"From Color" must be a six digit hexadecimal number. +' ); } if ( ! defined $safe_tocolor ) { $error .= p( '"To Color" must be a six digit hexadecimal number.' +); } my $error_html = ''; if ( $error and param( 'not_first_run' ) ) { $error_html = error( $error ); } print header, start_html( -title => 'Text Gradient', -bgcolor => $bgcolor ), start_form, $error_html, table( { -border => 0, -cellspacing => 0, -cellpadding => 3 }, Tr( [ td( [ 'String', textfield( -name => 'str +ing', -default => 'Thi +s is a test', -size => $str +ing_length_limit, -maxlength => $str +ing_length_limit ) ], ), td( [ 'Background', textfield( -name => 'bgc +olor', -default => 'FFF +FFF', -size => 6, -maxlength => 6 ) +], ), td( [ 'From color', textfield( -name => 'toc +olor', -default => 'FFF +FCC', -size => 6, -maxlength => 6 ) +], ), td( [ 'To color', textfield( -name => 'fro +mcolor', -default => '000 +0CC', -size => 6, -maxlength => 6 ) +], ) ] ) ), submit, hidden( -name => "not_first_run", -value => "yes" ), end_form; print p( color($string, $fromcolor, $tocolor) ) if ! $error; print end_html; sub hexify { my $red = shift; my $green = shift; my $blue = shift; sprintf("%.2x%.2x%.2x", $red, $green, $blue); } sub color { my $string = shift; my $FromColor = shift; my $ToColor = shift; my @letters = split //, $string; my ($fromR, $fromG, $fromB) = ( $FromColor =~ /(\w{2})(\w{2})(\w{2 +})/ ); my ($toR, $toG, $toB) = ( $ToColor =~ /(\w{2})(\w{2})(\w{2 +})/ ); $fromR = hex($fromR); $toR = hex($toR); $fromG = hex($fromG); $toG = hex($toG); $fromB = hex($fromB); $toB = hex($toB); my $rstep; my $gstep; my $bstep; if ( $fromR > $toR) { $rstep = ($fromR - $toR) / length($string); } elsif ( $fromR < $toR) { $rstep = ($toR - $fromR) / length($string); } if ( $fromG > $toG) { $gstep = ($fromG - $toG) / length($string); } elsif ( $fromR < $toR) { $gstep = ($toG - $fromG) / length($string); } if ( $fromB > $toB) { $bstep = ($fromB - $toB) / length($string); } elsif ( $fromR < $toR) { $bstep = ($toB - $fromB) / length($string); } my $happystring = ''; my $r = 0; my $g = 0; my $b = 0; foreach my $letter (@letters) { if($r == 1) { $fromR -= $rstep; } else { $fromR += $rstep; } if($g == 1) { $fromG -= $rstep; } else { $fromG += $gstep; } if($b == 1) { $fromB -= $bstep; } else { $fromB += $bstep; } if ( $fromR > 255) { $r = 1; } elsif ( $fromR < 0) { $r = 0; } if ( $fromG > 255) { $g = 1; } elsif ( $fromG < 0) { $g = 0; } if ( $fromB > 255) { $b = 1; } elsif ( $fromB < 0) { $b = 0; } my $hexcolors = hexify($fromR, $fromG, $fromB); $happystring .= font( { -color => "#$hexcolors" }, $letter ); } $happystring; } sub error { my $error = shift; return ( h1( 'Error in User Input' ) . p( $error ) ); }

Cheers,
Ovid

Update: I've added a few more features since dooberwah is making the code available on his site.

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Text Gradient by Ovid
in thread Text Gradient by dooberwah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.