d4_interpolate library
Interpolate numbers, colors, strings, arrays, objects, whatever!
This package provides a variety of interpolation methods for blending between two values. Values may be numbers, colors, strings, lists, or even deeply-nested maps. For example:
final i = interpolateNumber(10, 20);
i(0.0); // 10
i(0.2); // 12
i(0.5); // 15
i(1.0); // 20
The returned function i
is called an interpolator. Given a starting value
a and an ending value b, it takes a parameter t in the domain [0, 1]
and returns the corresponding interpolated value between a and b. An
interpolator typically returns a value equivalent to a at t = 0 and a
value equivalent to b at t = 1.
You can interpolate more than just numbers. To find the perceptual midpoint between steelblue and brown:
interpolateLab("steelblue", "brown")(0.5); // "rgb(142, 92, 109)"
Here’s a more elaborate example demonstrating type inference used by interpolate:
final i = interpolate({"colors": ["red", "blue"]}, {"colors": ["white", "black"]});
i(0.0); // {colors: [rgb(255, 0, 0), rgb(0, 0, 255)]}
i(0.5); // {colors: [rgb(255, 128, 128), rgb(0, 0, 128)]}
i(1.0); // {colors: [rgb(255, 255, 255), rgb(0, 0, 0)]}
Note that the generic value interpolator detects not only nested maps and lists, but also color strings and numbers embedded in strings!
Functions
-
interpolate(
Object? a, Object? b) → Object? Function(num) Value interpolation -
Returns an interpolator between the two arbitrary values
a
andb
. -
interpolateBasis(
List< Value interpolationnum> values) → num Function(num) -
Returns a uniform nonrational B-spline interpolator through the specified
list of
values
, which must be numbers. -
interpolateBasisClosed(
List< Value interpolationnum> values) → num Function(num) -
Returns a uniform nonrational B-spline interpolator through the specified
list of
values
, which must be numbers. -
interpolateCubehelix(
Object? a, Object? b) → String Function(num) Color interpolation -
Returns an Cubehelix color space interpolator between the two colors
a
andb
with a default gamma of 1. -
interpolateCubehelixGamma(
num gamma) → String Function(num) Function(Object?, Object?) Color interpolation -
Returns a new Cubehelix color space interpolator factory using the specified
gamma
. -
interpolateCubehelixGammaLong(
num gamma) → String Function(num) Function(Object?, Object?) Color interpolation - Like interpolateCubehelixGamma, but does not use the shortest path between hues.
-
interpolateCubehelixLong(
Object? a, Object? b) → String Function(num) Color interpolation - Like interpolateCubehelixGammaLong, but does not use the shortest path between hues.
-
interpolateDate(
DateTime a, DateTime b) → DateTime Function(num) Value interpolation -
Returns an interpolator between the two dates
a
andb
. -
interpolateDiscrete(
List< Value interpolationObject?> values) → Object? Function(num) -
Returns a discrete interpolator for the given list of
values
. -
interpolateHcl(
Object? a, Object? b) → String Function(num) Color interpolation -
Returns a
CIELChab color space
interpolator between the two colors
a
andb
. -
interpolateHclLong(
Object? a, Object? b) → String Function(num) Color interpolation - Like interpolateHcl, but does not use the shortest path between hues.
-
interpolateHsl(
Object? a, Object? b) → String Function(num) Color interpolation -
Returns an HSL color space interpolator between the two colors
a
andb
. -
interpolateHslLong(
Object? a, Object? b) → String Function(num) Color interpolation - Like interpolateHsl, but does not use the shortest path between hues.
-
interpolateHue(
num a, num b) → num Function(num) Color interpolation -
Returns an interpolator between the two hue angles
a
andb
. -
interpolateLab(
Object? a, Object? b) → String Function(num) Color interpolation -
Returns a
CIELAB color space
interpolator between the two colors
a
andb
. -
interpolateList<
T> (List< Value interpolationT> a, List<T> b) → List<Object?> Function(num) -
Returns an interpolator between the two lists
a
andb
. -
interpolateMap<
K, V> (Map< Value interpolationK, V> a, Map<K, V> b) → Map<K, Object?> Function(num) -
Returns an interpolator between the two objects
a
andb
. -
interpolateNumber(
num a, num b) → num Function(num) Value interpolation -
Returns an interpolator between the two numbers
a
andb
. -
interpolateNumberList<
T extends num> (List< Value interpolationT> a, List<T> b) → List<T> Function(num) -
Returns an interpolator between the two list of numbers
a
andb
. -
interpolateRgb(
Object? a, Object? b) → String Function(num) Color interpolation -
Returns an RGB color space interpolator between the two colors
a
andb
with a default gamma of 1. -
interpolateRgbBasis(
List< Color interpolationObject?> colors) → String Function(num) -
Returns a uniform nonrational B-spline interpolator through the specified
list of
colors
, which are converted to RGB color space. -
interpolateRgbBasisClosed(
List< Color interpolationObject?> colors) → String Function(num) -
Returns a uniform nonrational B-spline interpolator through the specified
list of
colors
, which are converted to RGB color space. -
interpolateRgbGamma(
num y) → String Function(num) Function(Object?, Object?) Color interpolation -
Returns a new RGB color space interpolator factory using the specified
gamma
. -
interpolateRound(
num a, num b) → int Function(num) Value interpolation -
Returns an interpolator between the two numbers
a
andb
; the interpolator is similar to interpolateNumber, except it will round the resulting value to the nearest integer. -
interpolateString(
String a, String b) → String Function(num) Value interpolation -
Returns an interpolator between the two strings
a
andb
. -
interpolateZoom(
View a, View b) → ZoomInterpolator Zoom interpolation -
Returns an interpolator between the two views
a
andb
of a two-dimensional plane, based on “Smooth and efficient zooming and panning” by Jarke J. van Wijk and Wim A.A. Nuij. -
interpolateZoomRho(
num rho) → ZoomInterpolator Function(View, View) Zoom interpolation -
Returns a new zoom interpolator using the specified curvature
rho
. -
piecewise<
T> (List< Value interpolationT> values, [Object? Function(num) interpolatorFactory(T, T) = interpolate]) → Object? Function(num) -
Returns a piecewise interpolator, composing interpolators for each adjacent
pair of
values
. -
quantize<
T> (T interpolator(num), int n) → List< Value interpolationT> -
Returns
n
uniformly-spaced samples from the specifiedinterpolator
, wheren
is an integer greater than one.