line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::Gradient; |
2
|
1
|
|
|
1
|
|
32212
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
44
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
Math::Gradient - Perl extension for calculating gradients for colour transitions, etc. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use Math::Gradient qw(multi_gradient); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# make a 100-point colour pallette to smothly transition between 6 RGB values |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my(@hot_spots) = ([ 0, 255, 0 ], [ 255, 255, 0 ], [ 127, 127, 127 ], [ 0, 0, 255 ], [ 127, 0, 0 ], [ 255, 255, 255 ]); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my(@gradient) = multi_array_gradient(100, @hot_spots); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Math::Gradient is used to calculate smooth transitions between numerical values (also known as a "Gradient"). I wrote this module mainly to mix colours, but it probably has several other applications. Methods are supported to handle both basic and multiple-point gradients, both with scalars and arrays. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 FUNCTIONS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=over 4 |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=item gradient($start_value, $end_value, $steps) |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This function will return an array of evenly distributed values between $start_value and $end_value. All three values supplied should be numeric. $steps should be the number of steps that should occur between the two points; for instance, gradient(0, 10, 4) would return the array (2, 4, 6, 8); the 4 evenly-distributed steps neccessary to get from 0 to 10, whereas gradient(0, 1, 3) would return (0.25, 0.5, 0.75). This is the basest function in the Math::Gradient module and isn't very exciting, but all of the other functions below derive their work from it. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item array_gradient($start_value, $end_value, $steps) |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
While gradient() takes numeric values for $start_value and $end_value, array_gradient() takes arrayrefs instead. The arrays supplied are expected to be lists of numerical values, and all of the arrays should contain the same number of elements. array_gradient() will return a list of arrayrefs signifying the gradient of all values on the lists $start_value and $end_value. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
For example, calling array_gradient([ 0, 100, 2 ], [ 100, 50, 70], 3) would return: ([ 25, 87.5, 19 ], [ 50, 75, 36 ], [ 75, 62.5, 53 ]). |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=item multi_gradient($steps, @values) |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
multi_gradient() calculates multiple gradients at once, returning one list that is an even transition between all points, with the values supplied interpolated evenly within the list. If $steps is less than the number of entries in the list @values, items are deleted from @values instead. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
For example, calling multi_gradient(10, 0, 100, 50) would return: (0, 25, 50, 75, 100, 90, 80, 70, 60, 50) |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item multi_array_gradient($steps, @values) |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
multi_array_gradient() is the same as multi_gradient, except that it works on arrayrefs instead of scalars (like array_gradient() is to gradient()). |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=back |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
1
|
|
28
|
use 5.005; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
52
|
|
52
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
53
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
985
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
require Exporter; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub gradient ($$$); |
58
|
|
|
|
|
|
|
sub array_gradient ($$$); |
59
|
|
|
|
|
|
|
sub multi_array_gradient ($@); |
60
|
|
|
|
|
|
|
sub multi_gradient ($@); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
65
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
66
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# This allows declaration use Math::Gradient ':all'; |
69
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
70
|
|
|
|
|
|
|
# will save memory. |
71
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
72
|
|
|
|
|
|
|
gradient array_gradient multi_gradient multi_array_gradient |
73
|
|
|
|
|
|
|
) ] ); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
our @EXPORT = qw( |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# Preloaded methods go here. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Math::Gradient |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Take sets of numbers and a specified number of steps, and return a |
89
|
|
|
|
|
|
|
# gradient for going betewen those steps |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# for example, |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# [ 2, 4, 6 ], [ 4, 8, 12 ], [ 16, 32, 48 ] with 5 steps would result in |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# [ 2, 4, 6 ], [ 3, 6, 9 ], [ 4, 8, 12 ], [ 10, 24, 30 ], [ 16, 32, 48 ] |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# This involves two distinct steps; |
98
|
|
|
|
|
|
|
# making a gradient between two points, |
99
|
|
|
|
|
|
|
# and calculating the gradient between X points. |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# To make a gradient between two points, we are given the points, |
103
|
|
|
|
|
|
|
# and the number of steps to create between them. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# basic_gradient - get start and end number and # of steps to |
107
|
|
|
|
|
|
|
# create in-between the two. returns an array of the intermediary steps. |
108
|
|
|
|
|
|
|
sub gradient ($$$) |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
0
|
1
|
|
my($low, $high, $steps) = @_; |
111
|
0
|
|
|
|
|
|
my $xsteps = $steps + 1; # steps incl. low |
112
|
0
|
|
|
|
|
|
my $xdistance = $high - $low; # distance; may be negative |
113
|
0
|
|
|
|
|
|
my $step_value = $xdistance/$xsteps; # how much to add to each step to create a gradient |
114
|
0
|
|
|
|
|
|
my $value = $low; # start off with the starting value |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my @values; |
117
|
0
|
|
|
|
|
|
foreach my $step (1 .. $steps) |
118
|
|
|
|
|
|
|
{ |
119
|
0
|
|
|
|
|
|
$value += $step_value; |
120
|
0
|
|
|
|
|
|
push(@values, $value); |
121
|
|
|
|
|
|
|
} |
122
|
0
|
|
|
|
|
|
return(@values); # we have a gradient! |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# takes two arrayrefs, and # of steps. arrayrefs should have same number |
126
|
|
|
|
|
|
|
# of values in each. |
127
|
|
|
|
|
|
|
sub array_gradient ($$$) |
128
|
|
|
|
|
|
|
{ |
129
|
0
|
|
|
0
|
1
|
|
my($low, $high, $steps) = @_; |
130
|
0
|
|
|
|
|
|
my(@values); |
131
|
0
|
|
|
|
|
|
my $g_count = scalar(@$low); |
132
|
0
|
|
|
|
|
|
foreach my $x (1 .. scalar(@$low)) |
133
|
|
|
|
|
|
|
{ |
134
|
0
|
|
|
|
|
|
my(@y) = (gradient($low->[$x - 1], $high->[$x - 1], $steps)); |
135
|
0
|
|
|
|
|
|
foreach my $y (1 .. scalar(@y)) |
136
|
|
|
|
|
|
|
{ |
137
|
0
|
|
0
|
|
|
|
$values[$y - 1] ||= []; |
138
|
0
|
|
|
|
|
|
push(@{$values[$y - 1]}, $y[$y - 1]); |
|
0
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
} |
141
|
0
|
|
|
|
|
|
return(@values); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# takes a number of steps and any number of steps already filled in (at least two) |
145
|
|
|
|
|
|
|
# returns the full gradient, including supplied steps |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub multi_array_gradient ($@) |
148
|
|
|
|
|
|
|
{ |
149
|
0
|
|
|
0
|
1
|
|
my($steps, @start_steps) = @_; |
150
|
0
|
0
|
|
|
|
|
if($steps == scalar(@start_steps)) |
151
|
|
|
|
|
|
|
{ |
152
|
0
|
|
|
|
|
|
return(@start_steps); # already have the # of steps we want |
153
|
|
|
|
|
|
|
} |
154
|
0
|
|
|
|
|
|
my @values; |
155
|
|
|
|
|
|
|
# "steppage" is how many steps we should request on average between |
156
|
|
|
|
|
|
|
# steps we've been supplied. |
157
|
0
|
|
|
|
|
|
my $steppage = ($steps - scalar(@start_steps)) / (scalar(@start_steps) - 1); |
158
|
0
|
|
|
|
|
|
my $steps_left = $steps - scalar(@start_steps); |
159
|
0
|
|
|
|
|
|
my $xstep = 0; |
160
|
0
|
|
|
|
|
|
while(my $cstep = shift(@start_steps)) |
161
|
|
|
|
|
|
|
{ |
162
|
0
|
|
|
|
|
|
push(@values, $cstep); |
163
|
0
|
|
|
|
|
|
$xstep += $steppage; |
164
|
0
|
0
|
0
|
|
|
|
if(@start_steps && $xstep >= 1) |
|
|
0
|
0
|
|
|
|
|
165
|
|
|
|
|
|
|
{ |
166
|
0
|
|
|
|
|
|
my $xxstep = int($xstep); |
167
|
0
|
|
|
|
|
|
$xstep -= $xxstep; |
168
|
0
|
|
|
|
|
|
$steps_left -= $xxstep; |
169
|
0
|
|
|
|
|
|
push(@values, array_gradient($cstep, $start_steps[0], $xxstep)); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
elsif(@start_steps && $xstep <= 1) |
172
|
|
|
|
|
|
|
{ |
173
|
0
|
|
|
|
|
|
my $xxstep = int($xstep); |
174
|
0
|
|
|
|
|
|
$xstep -= $xxstep; |
175
|
0
|
|
|
|
|
|
$steps_left -= $xxstep; |
176
|
0
|
|
|
|
|
|
splice(@values, scalar(@values) + $xxstep, abs($xxstep)); |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
0
|
|
|
|
|
|
return(@values); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub multi_gradient ($@) |
183
|
|
|
|
|
|
|
{ |
184
|
0
|
|
|
0
|
1
|
|
my($steps, @start_steps) = (@_); |
185
|
0
|
0
|
|
|
|
|
if($steps == scalar(@start_steps)) |
186
|
|
|
|
|
|
|
{ |
187
|
0
|
|
|
|
|
|
return(@start_steps); # already have the # of steps we want |
188
|
|
|
|
|
|
|
} |
189
|
0
|
|
|
|
|
|
my @values; |
190
|
|
|
|
|
|
|
# "steppage" is how many steps we should request on average between |
191
|
|
|
|
|
|
|
# steps we've been supplied. |
192
|
0
|
|
|
|
|
|
my $steppage = ($steps - scalar(@start_steps)) / (scalar(@start_steps) - 1); |
193
|
0
|
|
|
|
|
|
my $steps_left = $steps - scalar(@start_steps); |
194
|
0
|
|
|
|
|
|
my $xstep = 0; |
195
|
0
|
|
|
|
|
|
while(scalar(@start_steps)) |
196
|
|
|
|
|
|
|
{ |
197
|
0
|
|
|
|
|
|
my $cstep = shift(@start_steps); |
198
|
0
|
|
|
|
|
|
push(@values, $cstep); |
199
|
0
|
|
|
|
|
|
$xstep += $steppage; |
200
|
0
|
0
|
0
|
|
|
|
if(@start_steps && $xstep >= 1) |
|
|
0
|
0
|
|
|
|
|
201
|
|
|
|
|
|
|
{ |
202
|
0
|
|
|
|
|
|
my $xxstep = int($xstep); |
203
|
0
|
|
|
|
|
|
$xstep -= $xxstep; |
204
|
0
|
|
|
|
|
|
$steps_left -= $xxstep; |
205
|
0
|
|
|
|
|
|
push(@values, gradient($cstep, $start_steps[0], $xxstep)); |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
elsif(@start_steps && $xstep <= 1) |
208
|
|
|
|
|
|
|
{ |
209
|
0
|
|
|
|
|
|
my $xxstep = int($xstep); |
210
|
0
|
|
|
|
|
|
$xstep -= $xxstep; |
211
|
0
|
|
|
|
|
|
$steps_left -= $xxstep; |
212
|
0
|
|
|
|
|
|
splice(@values, scalar(@values) + $xxstep, abs($xxstep)); |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} |
215
|
0
|
|
|
|
|
|
return(@values); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
1; |
220
|
|
|
|
|
|
|
__END__ |