-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rainbow.pm
executable file
·191 lines (137 loc) · 4.68 KB
/
Rainbow.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
##########################################################################
# Written by Paolo Amedeo, slightly modified by bhaas (1/7/2005) #########
##########################################################################
=head1
Rainbow.pm
=cut
package Rainbow;
use strict;
sub new {
my $packagename = shift;
my $self = {};
bless ($self, $packagename);
return ($self);
}
sub GetRainbow_K_R { # $no_rainbow_cols = GetRainbow_B_R(\@rainbow_colors, $no_rainbow_cols)
shift();
my ($r_rainbow, $col_no) = @_;
die "\n\nGetRainbow: It is possible to handle only a positive number of colors\n\n" unless $col_no > 0;
my ($red, $green, $blue) = (0) x 3;
my @step = (0) x 5; # 5 are the steps for generating the different colors:
# 0, 0, 0 -> 0, 0, 255 (black -> blue)
# 0, 0, 255 -> 0, 255, 255 (blue -> cyan)
# 0, 255, 255 -> 0, 255, 0 (cyan -> green)
# 0, 255, 0 -> 255, 255, 0 (green -> yellow)
# 255, 255, 0 -> 255, 0, 0 (yellow -> red)
# "Rude way of distributing the colors...."
my $n = @step; # 1 + last index in the array @step_no. We want to have more gradients on the top than on the bottom => stepping faster out of the black
while (--$col_no){ # until we don't run out of colors
++$step[--$n];
$n = @step unless $n; # new round of distribution
}
# Converting the number of steps in steps
foreach my $incr (@step){
$incr = 255 / $incr;
}
push(@{$r_rainbow}, [$red, $green, $blue]); # black
# black -> blue
while($blue < 255){
$blue += $step[0];
$blue = 255 if 255 - $blue < $step[0];
push(@{$r_rainbow}, [$red, $green, int($blue)]);
}
# blue -> cyan
while ($green < 255){
$green += $step[1];
$green = 255 if 255 - $green < $step[1];
push(@{$r_rainbow}, [$red, int($green), $blue]);
}
# cyan -> green
while ($blue > 0){
$blue -= $step[2];
$blue = 0 if $blue < $step[2];
push(@{$r_rainbow}, [$red, $green, int($blue)]);
}
# green -> yellow
while ($red < 255){
$red += $step[3];
$red = 255 if 255 - $red < $step[3];
push(@{$r_rainbow}, [int($red), $green, $blue]);
}
# yellow -> red
while ($green > 0){
$green -= $step[4];
$green = 0 if $green < $step[4];
push(@{$r_rainbow}, [$red, int($green), $blue]);
}
return(scalar(@{$r_rainbow}));
}
sub GetRainbow_B_R { # $no_rainbow_cols = GetRainbow_B_R(\@rainbow_colors, $no_rainbow_cols)
shift();
my ($r_rainbow, $col_no) = @_;
die "\n\nGetRainbow: It is possible to handle only a positive number of colors\n\n" unless $col_no > 0;
my ($red, $green, $blue) = (0, 0, 255);
my @step = (0) x 4; # 4 are the steps for generating the different colors:
# 0, 0, 255 -> 0, 255, 255 (blue -> cyan)
# 0, 255, 255 -> 0, 255, 0 (cyan -> green)
# 0, 255, 0 -> 255, 255, 0 (green -> yellow)
# 255, 255, 0 -> 255, 0, 0 (yellow -> red)
# "Rude way of distributing the colors...."
my $n = @step; # 1 + last index in the array @step_no. We want to have more gradients on the top than on the bottom => stepping faster out of the black
while (--$col_no){ # until we don't run out of colors
++$step[--$n];
$n = @step unless $n; # new round of distribution
}
# Converting the number of steps in steps
foreach my $incr (@step){
$incr = 255 / $incr;
}
push(@{$r_rainbow}, [$red, $green, $blue]); # blue
# blue -> cyan
while ($green < 255){
$green += $step[0];
$green = 255 if 255 - $green < $step[0];
push(@{$r_rainbow}, [$red, int($green), $blue]);
}
# cyan -> green
while ($blue > 0){
$blue -= $step[1];
$blue = 0 if $blue < $step[1];
push(@{$r_rainbow}, [$red, $green, int($blue)]);
}
# green -> yellow
while ($red < 255){
$red += $step[2];
$red = 255 if 255 - $red < $step[2];
push(@{$r_rainbow}, [int($red), $green, $blue]);
}
# yellow -> red
while ($green > 0){
$green -= $step[3];
$green = 0 if $green < $step[3];
push(@{$r_rainbow}, [$red, int($green), $blue]);
}
return(scalar(@{$r_rainbow}));
}
1; #EOM
__END__
=over 4
=item SampleProgram
#!/usr/local/bin/perl
use strict;
use Rainbow;
my $ptt = Rainbow->new();
my $col_no = 128;
my @col_list = ();
# Number_of_allocated colors = $ptt->GetRainbow_B_R(\@col_list, Number_of_requested_Colors)
$ptt->GetRainbow_B_R(\@col_list, $col_no) || die "\n\nproblems assigning the colors\n\n";
print "<html>\r\n<head>\r\n<title>Color Test</title>\r\n</head>\r\n",
"<body>\r\n<table cols=128 valign=\"center\" align=\"center\">\r\n\t<tr>\r\n";
foreach my $color (@col_list){
my $hex_col = sprintf("%02x%02x%02x", @{$color}); # Converting in a string of hexadecimal pairs
print "\t\t<td bgcolor=\"#$hex_col\">$hex_col</td>\r\n";
}
print "\t</tr>\r\n</table>\r\n</body>\r\n</html>\r\n";
=back
=cut
;