---
name: "Math::Gradient"
section: "3pm"
platform: "linux"
package: "libmath-gradient-perl"
version: "0.04-5"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/Math::Gradient"
---

# Math::Gradient(3pm)

Perl extension for calculating gradients for colour transitions, etc.

## NAME

Math::Gradient - Perl extension for calculating gradients for colour transitions, etc.

## SYNOPSIS

use Math::Gradient qw(multi\_gradient);

\# make a 100-point colour palette 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`);

## 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.

## FUNCTIONS

- 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 necessary 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.

- 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 \]).

- 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)

- 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()**).

## AUTHOR

Tyler MacDonald, \<japh@crackerjack.net\>

## 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.
