#!/usr/bin/perl
# Michael Tartaglia - Fading Text: User enters three colors
# and a text string and the colors fade letter by letter
# through that string. This script was translated from
# its original JScript, also by Michael Tartaglia.
# Orig. addr: http://members.aol.com/shmajent/misc/textfade.html
require template;
require math;
template::startHTML("Freelance Program","Fading Text");
# READ IN ENVIRONMENT VARIABLE "QUERY_STRING"
if ($ENV{QUERY_STRING} ne "") {
$buffer = $ENV{'QUERY_STRING'};
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split (/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/</;
$FORM{$name} = $value;
}
$s = caps($FORM{'startColor'} eq "" ? $FORM{'c1'} : $FORM{'startColor'}); # START COLOR
$e = caps($FORM{'endColor'} eq "" ? $FORM{'c2'} : $FORM{'endColor'}); # ... FADE-TO COLOR
$b = caps($FORM{'backColor'} ne "" ? checkHex($FORM{'backColor'}) : $FORM{'backColor'}); # BG COLOR
$x = $FORM{'text'}; # TEXT STRING
if ($x ne "") {
$faded= fadeText(checkHex($s), checkHex($e), $b, $x); # FADED STRING AS HTML
# PRINTING OUT RESULTS
print "\nResults... ",
"\n$faded
\n",
"...and now copy the code:",
"\n
",
"\n\n";
} else {
# IF NO TEXT HAS BEEN ENTERED
print "Warning You have not ",
"filled your text field!
";
}
} else {
$s = ""; # DEFAULT START VALUE
$e = ""; # DEFAULT END VALUE
$b = ""; # DEFAULT BG VALUE
$x = ""; # DEFAULT TEXT
}
$showList = printColorList(); # INSTEAD OF REPEATING HTML SELECTION LIST,
# LIST IS SAVED AS VARIABLE, AND VARIABLE
# IS USED TO PRINT THE LIST
# PRINT FORM TO SCREEN WITH INSTRUCTIONS
print <Welcome To The Text Fader!
Enter the hexidecimal codes in the appropriate fields below,
then type in the text you want the colors to fade through,
and finally, click the submit button to see the result!
The HTML code will then be waiting for you with a sample!
From which color? #
To which color? #
With what background? #
(leave blank if no background is desired)
What message?
EndOfHTML
template::endHTML();
sub fadeText
{
$fromColor = $_[0];
$toColor = $_[1];
$bgColor = $_[2];
$text = $_[3];
# IF BG IS DESIRED, USE SPAN TAG...
$HTMLout = "";
$HTMLout = "" if ($_[2] ne "");
$textL = length $text;
@fromColorsSplit = ( hex substr ($fromColor, 0, 2),
hex substr ($fromColor, 2, 2),
hex substr ($fromColor, 4, 2) );
@toColorsSplit = ( hex substr ($toColor, 0, 2),
hex substr ($toColor, 2, 2),
hex substr ($toColor, 4, 2) );
# VALUES BY WHICH TO INCREMENT RED, GREEN, AND BLUE
@inc = ( (($fromColorsSplit[0]-$toColorsSplit[0])/($textL-1)),
(($fromColorsSplit[1]-$toColorsSplit[1])/($textL-1)),
(($fromColorsSplit[2]-$toColorsSplit[2])/($textL-1)) );
# FOR AS MANY LETTERS IN THE STRING, START FROM BEGINNING COLOR
# AND ADD BY VALUES IN @inc UNTIL END COLOR IS REACHED
for ($i = 0; $i < $textL; ++$i)
{
$letter = substr($text, $i, 1);
if ($letter ne " ") {
$hexColor = numberToHex($fromColorsSplit[0], $fromColorsSplit[1],
$fromColorsSplit[2]);
$HTMLout = $HTMLout . "$letter";
} else {
$HTMLout = $HTMLout . " ";
}
$fromColorsSplit[0] -= eval($inc[0]);
$fromColorsSplit[1] -= eval($inc[1]);
$fromColorsSplit[2] -= eval($inc[2]);
}
$HTMLout = $HTMLout . "" if ($bgColor ne "");
return $HTMLout;
}
# VERIFY THAT ENTERED HEXIDECIMAL CODE IS VALID
sub checkHex
{
@hexLetters = qw ( 0 .. 9 A .. F );
$hexIn = $_[0];
$hexOK = "";
$hexInL= length $hexIn;
for ($digit = 0; $digit < 16; ++$digit) {
$match = 0;
foreach (0 .. $hexInL) {
$match = 1 if (substr($hexIn,$digit,1) ne [a-fA-F0-9]);
}
if ($match) {
$hexOK = $hexOK . substr($hexIn,$digit,1);
} else {
$hexOK = $hexOK . "F";
}
}
if ($hexInL-6 != 0) {
for ($digit = 0; $digit < $hexInL-6; ++$digit) {
$hexOK = $hexOK . "0";
}
}
return $hexOK;
}
# CONVERT DECIMAL TO HEXIDECIMAL
sub numberToHex
{
@inArray = (math::round($_[0]), math::round($_[1]), math::round($_[2]));
@hexLetters = ("0","1","2","3","4","5","6","7","8","9",
"A","B","C","D","E","F");
$outHex = "";
for ($pair = 0; $pair < 3; ++$pair)
{
if ($inArray[$pair] >= 255) {
$outHex = $outHex . "FF";
} elsif ($inArray[$pair] <= 0) {
$outHex = $outHex . "00";
} else {
$outHex = $outHex . "" . $hexLetters[math::floor($inArray[$pair]/16)]
. $hexLetters[$inArray[$pair]%16];
}
}
return $outHex;
}
sub caps
{
return "\U$_[0]\E";
}
# RETURN COLOR LIST TO VARIABLE, THEREBY AVOIDING REDUNDANCY
sub printColorList
{
return <Some choices: