Math-Gradient-0.04/0040755000175000017500000000000007746334320013743 5ustar farawayfarawayMath-Gradient-0.04/Changes0100644000175000017500000000105207746334311015231 0ustar farawayfarawayRevision history for Perl extension Math::Gradient. 0.04 Fri Oct 24 16:41:44 2003 - still getting used to this PAUSE thing... i forgot to up the version number for 0.03... sorry :-/ 0.03 Fri Oct 24 16:24:20 2003 - fixed spurious "use 5.008;" in Makefile.PL (cpan #4205) - fixed date for version 0.02 in ChangeLog :-) 0.02 Thu Oct 23 12:02:38 2003 - fixed a few bits in the documentation, got rid of requirement for perl 5.8.0 0.01 Tue Oct 21 11:47:09 2003 - original version; created by h2xs 1.22 with options -AX -n Math::Gradient Math-Gradient-0.04/Gradient.pm0100644000175000017500000001616307746334123016043 0ustar farawayfarawaypackage Math::Gradient; use strict; use warnings; =head1 NAME Math::Gradient - Perl extension for calculating gradients for colour transitions, etc. =head1 SYNOPSIS use Math::Gradient qw(multi_gradient); # make a 100-point colour pallette to smothly transition between 6 RGB values my(@hot_spots) = ([ 0, 255, 0 ], [ 255, 255, 0 ], [ 127, 127, 127 ], [ 0, 0, 255 ], [ 127, 0, 0 ], [ 255, 255, 255 ]); my(@gradient) = multi_array_gradient(100, @hot_spots); =head1 DESCRIPTION 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. =head1 FUNCTIONS =over 4 =item gradient($start_value, $end_value, $steps) 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. =item array_gradient($start_value, $end_value, $steps) 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. 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 ]). =item multi_gradient($steps, @values) 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. For example, calling multi_gradient(10, 0, 100, 50) would return: (0, 25, 50, 75, 100, 90, 80, 70, 60, 50) =item multi_array_gradient($steps, @values) multi_array_gradient() is the same as multi_gradient, except that it works on arrayrefs instead of scalars (like array_gradient() is to gradient()). =back =cut use 5.005; use strict; use warnings; require Exporter; sub gradient ($$$); sub array_gradient ($$$); sub multi_array_gradient ($@); sub multi_gradient ($@); our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not export # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use Math::Gradient ':all'; # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( gradient array_gradient multi_gradient multi_array_gradient ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw( ); our $VERSION = '0.04'; # Preloaded methods go here. # Math::Gradient # Take sets of numbers and a specified number of steps, and return a # gradient for going betewen those steps # for example, # [ 2, 4, 6 ], [ 4, 8, 12 ], [ 16, 32, 48 ] with 5 steps would result in # [ 2, 4, 6 ], [ 3, 6, 9 ], [ 4, 8, 12 ], [ 10, 24, 30 ], [ 16, 32, 48 ] # This involves two distinct steps; # making a gradient between two points, # and calculating the gradient between X points. # To make a gradient between two points, we are given the points, # and the number of steps to create between them. # basic_gradient - get start and end number and # of steps to # create in-between the two. returns an array of the intermediary steps. sub gradient ($$$) { my($low, $high, $steps) = @_; my $xsteps = $steps + 1; # steps incl. low my $xdistance = $high - $low; # distance; may be negative my $step_value = $xdistance/$xsteps; # how much to add to each step to create a gradient my $value = $low; # start off with the starting value my @values; foreach my $step (1 .. $steps) { $value += $step_value; push(@values, $value); } return(@values); # we have a gradient! } # takes two arrayrefs, and # of steps. arrayrefs should have same number # of values in each. sub array_gradient ($$$) { my($low, $high, $steps) = @_; my(@values); my $g_count = scalar(@$low); foreach my $x (1 .. scalar(@$low)) { my(@y) = (gradient($low->[$x - 1], $high->[$x - 1], $steps)); foreach my $y (1 .. scalar(@y)) { $values[$y - 1] ||= []; push(@{$values[$y - 1]}, $y[$y - 1]); } } return(@values); } # takes a number of steps and any number of steps already filled in (at least two) # returns the full gradient, including supplied steps sub multi_array_gradient ($@) { my($steps, @start_steps) = @_; if($steps == scalar(@start_steps)) { return(@start_steps); # already have the # of steps we want } my @values; # "steppage" is how many steps we should request on average between # steps we've been supplied. my $steppage = ($steps - scalar(@start_steps)) / (scalar(@start_steps) - 1); my $steps_left = $steps - scalar(@start_steps); my $xstep = 0; while(my $cstep = shift(@start_steps)) { push(@values, $cstep); $xstep += $steppage; if(@start_steps && $xstep >= 1) { my $xxstep = int($xstep); $xstep -= $xxstep; $steps_left -= $xxstep; push(@values, array_gradient($cstep, $start_steps[0], $xxstep)); } elsif(@start_steps && $xstep <= 1) { my $xxstep = int($xstep); $xstep -= $xxstep; $steps_left -= $xxstep; splice(@values, scalar(@values) + $xxstep, abs($xxstep)); } } return(@values); } sub multi_gradient ($@) { my($steps, @start_steps) = (@_); if($steps == scalar(@start_steps)) { return(@start_steps); # already have the # of steps we want } my @values; # "steppage" is how many steps we should request on average between # steps we've been supplied. my $steppage = ($steps - scalar(@start_steps)) / (scalar(@start_steps) - 1); my $steps_left = $steps - scalar(@start_steps); my $xstep = 0; while(scalar(@start_steps)) { my $cstep = shift(@start_steps); push(@values, $cstep); $xstep += $steppage; if(@start_steps && $xstep >= 1) { my $xxstep = int($xstep); $xstep -= $xxstep; $steps_left -= $xxstep; push(@values, gradient($cstep, $start_steps[0], $xxstep)); } elsif(@start_steps && $xstep <= 1) { my $xxstep = int($xstep); $xstep -= $xxstep; $steps_left -= $xxstep; splice(@values, scalar(@values) + $xxstep, abs($xxstep)); } } return(@values); } 1; __END__ # Below is stub documentation for your module. You'd better edit it! =head1 AUTHOR Tyler MacDonald, Ejaph@crackerjack.netE =head1 COPYRIGHT AND LICENSE Copyright 2003 by Tyler MacDonald This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Math-Gradient-0.04/MANIFEST0100644000175000017500000000006607745277455015111 0ustar farawayfarawayChanges Gradient.pm Makefile.PL MANIFEST README t/1.t Math-Gradient-0.04/Makefile.PL0100644000175000017500000000077707746332227015731 0ustar farawayfarawayuse 5.005; use ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'NAME' => 'Math::Gradient', 'VERSION_FROM' => 'Gradient.pm', # finds $VERSION 'PREREQ_PM' => {}, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005 (ABSTRACT_FROM => 'Gradient.pm', # retrieve abstract from module AUTHOR => 'Tyler MacDonald ') : ()), ); Math-Gradient-0.04/README0100644000175000017500000000174107745613434014630 0ustar farawayfarawayMath/Gradient version 0.01 ========================== The README is used to introduce the module and provide instructions on how to install the module, any machine dependencies it may have (for example C compilers and installed libraries) and any other information that should be provided before the module is installed. A README file is required for CPAN modules since CPAN extracts the README file from a module distribution so that people browsing the archive can use it get an idea of the modules uses. It is usually a good idea to provide version information here so that people can decide whether fixes for the module are worth downloading. INSTALLATION To install this module type the following: perl Makefile.PL make make test make install COPYRIGHT AND LICENCE Put the correct copyright and licence information here. Copyright (C) 2003 Tyler MacDonald This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Math-Gradient-0.04/t/0040755000175000017500000000000007745277515014221 5ustar farawayfarawayMath-Gradient-0.04/t/1.t0100644000175000017500000000072107745277455014546 0ustar farawayfaraway# Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl 1.t' ######################### # change 'tests => 1' to 'tests => last_test_to_print'; use Test::More tests => 1; BEGIN { use_ok('Math::Gradient') }; ######################### # Insert your test code below, the Test::More module is use()ed here so read # its man page ( perldoc Test::More ) for help writing this test script.