d4_interpolate library

Interpolate numbers, colors, strings, lists, maps, 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. 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)

Or, as a color ramp from t = 0 to t = 1:

{@inject-html}

{@end-inject-html}

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 and b.
interpolateBasis(List<num> values) num Function(num) Value interpolation
Returns a uniform nonrational B-spline interpolator through the specified list of values, which must be numbers.
interpolateBasisClosed(List<num> values) num Function(num) Value interpolation
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 and b 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 and b.
interpolateDiscrete(List<Object?> values) Object? Function(num) Value interpolation
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 and b.
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 and b.
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 and b.
interpolateLab(Object? a, Object? b) String Function(num) Color interpolation
Returns a CIELAB color space interpolator between the two colors a and b.
interpolateList<T>(List<T> a, List<T> b) List<Object?> Function(num) Value interpolation
Returns an interpolator between the two lists a and b.
interpolateMap<K, V>(Map<K, V> a, Map<K, V> b) Map<K, Object?> Function(num) Value interpolation
Returns an interpolator between the two objects a and b.
interpolateNumber(num a, num b) num Function(num) Value interpolation
Returns an interpolator between the two numbers a and b.
interpolateNumberList<T extends num>(List<T> a, List<T> b) List<T> Function(num) Value interpolation
Returns an interpolator between the two list of numbers a and b.
interpolateRgb(Object? a, Object? b) String Function(num) Color interpolation
Returns an RGB color space interpolator between the two colors a and b with a default gamma of 1.
interpolateRgbBasis(List<Object?> colors) String Function(num) Color interpolation
Returns a uniform nonrational B-spline interpolator through the specified list of colors, which are converted to RGB color space.
interpolateRgbBasisClosed(List<Object?> colors) String Function(num) Color interpolation
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 and b; 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 and b.
interpolateZoom(View a, View b) → ZoomInterpolator Zoom interpolation
Returns an interpolator between the two views a and b 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<T> values, [Object? Function(num) interpolatorFactory(T, T) = interpolate]) Object? Function(num) Value interpolation
Returns a piecewise interpolator, composing interpolators for each adjacent pair of values.
quantize<T>(T interpolator(num), int n) List<T> Value interpolation
Returns n uniformly-spaced samples from the specified interpolator, where n is an integer greater than one.