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