1: #!/usr/bin/perl
2: #By Ben Jacobs <dooberwah> 2001
3: # "Use": Colors a string with a gradient. The user inputs
4: #the string and two colors in hexedcimal.
5: # Comments welcome, I know this isn't a very efficient
6: #script but it's the best I could come up with.
7:
8: # Update 5/24/01: Fixed a bug in the HTML code. Before there was an
9: # extra </HTML> Tag that was messing some browsers up
10:
11: # Update 5/23/01: You can now see this script in action at
12: # http://dooberwah.perlmonk.org/cgi-bin/gradient.pl.
13:
14: # Update 5/22/01: Added in the use of the <b>abs</b> function instead
15: # of writing my own function to do the exact same thing.
16:
17: use strict;
18: #use warnings;
19:
20: use CGI;
21:
22: sub hexify {
23: my $red = shift;
24: my $green = shift;
25: my $blue = shift;
26:
27: my $hexed = sprintf("%.2x%.2x%.2x", $red, $green, $blue);
28: }
29:
30: sub color {
31: my $string = shift;
32: my $FromColor = shift;
33: my $ToColor = shift;
34:
35: my @letters = split //, $string;
36:
37: my ($fromR, $fromG, $fromB) = $FromColor =~/(\w{2})(\w{2})(\w{2})/;
38: my ($toR, $toG, $toB) = $ToColor =~ /(\w{2})(\w{2})(\w{2})/;
39:
40: $fromR = hex($fromR);
41: $toR = hex($toR);
42:
43: $fromG = hex($fromG);
44: $toG = hex($toG);
45:
46: $fromB = hex($fromB);
47: $toB = hex($toB);
48:
49: my $rstep;
50: my $gstep;
51: my $bstep;
52:
53: $rstep = abs($fromR - $toR) / length($string);
54: $gstep = abs($fromG - $toG) / length($string);
55: $rstep = abs($fromB - $toB) / length($string);
56:
57: my $happystring;
58: my $r;
59: my $g;
60: my $b;
61:
62: foreach my $letter (@letters) {
63: if($r == 1) { $fromR -= $rstep; } else { $fromR += $rstep; }
64: if($g == 1) { $fromG -= $rstep; } else { $fromG += $gstep; }
65: if($b == 1) { $fromB -= $bstep; } else { $fromB += $bstep; }
66:
67: if ( $fromR > 255) {
68: $r = 1;
69: }elsif ( $fromR < 0) {
70: $r = 0;
71: }
72:
73: if ( $fromG > 255) {
74: $g = 1;
75: }elsif ( $fromG < 0) {
76: $g = 0;
77: }
78:
79: if ( $fromB > 255) {
80: $b = 1;
81: }elsif ( $fromB < 0) {
82: $b = 0;
83: }
84:
85: my $hexcolors = hexify($fromR, $fromG, $fromB);
86: $happystring .= "<FONT COLOR=\"#$hexcolors\">$letter</FONT>";
87: }
88: $happystring;
89: }
90:
91: my %input = CGI::Vars();
92:
93: my $string = %input->{'string'};
94: my $fromcolor = %input->{'fromcolor'};
95: my $tocolor = %input->{'tocolor'};
96:
97: print "Content type: text/html\n\n";
98: print "<HTML><HEAD><TITLE>Text Gradient</TITLE></HEAD>\n";
99: print "<BODY BGCOLOR=\"#000000\" TEXT=\"#e0e0e0\">\n";
100:
101: print "<form action=\"./gradient.pl\" method=\"GET\">\n";
102: print "String <input type=\"text\" name=\"string\" value=\"$string\"><br>\n";
103: print "FromColor <input type=\"text\" name=\"fromcolor\" value=\"$fromcolor\"><br>\n";
104: print "ToColor <input type=\"text\" name=\"tocolor\" value=\"$tocolor\"><br>\n";
105: print "<input type=\"submit\" value=\"Make it So\">\n";
106: print "</form>\n";
107:
108: print color($string, $fromcolor, $tocolor) . "<br>\n";
109: print "</BODY></HEAD>\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(Ovid) Re: Text Gradient
by Ovid (Cardinal) on May 23, 2001 at 04:04 UTC | |
by dooberwah (Pilgrim) on May 23, 2001 at 06:15 UTC |