UNPKG

19.7 kBTypeScriptView Raw
1// Type definitions for D3JS d3-interpolate module 1.4
2// Project: https://github.com/d3/d3-interpolate/, https://d3js.org/d3-interpolate
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Nathan Bierema <https://github.com/Methuselah96>
8// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
9// TypeScript Version: 2.3
10
11// Last module patch version validated against: 1.4.0
12
13import { ColorCommonInstance } from 'd3-color';
14
15// ---------------------------------------------------------------------------
16// Shared Type Definitions and Interfaces
17// ---------------------------------------------------------------------------
18
19export interface ZoomInterpolator extends Function {
20 (t: number): ZoomView;
21 /**
22 * Recommended duration of zoom transition in milliseconds.
23 */
24 duration: number;
25}
26
27export interface ColorGammaInterpolationFactory extends Function {
28 (a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
29 /**
30 * Returns a new interpolator factory of the same type using the specified *gamma*.
31 * For example, to interpolate from purple to orange with a gamma of 2.2 in RGB space: `d3.interpolateRgb.gamma(2.2)("purple", "orange")`.
32 * See Eric Brasseurs article, [Gamma error in picture scaling](https://web.archive.org/web/20160112115812/http://www.4p8.com/eric.brasseur/gamma.html), for more on gamma correction.
33 */
34 gamma(g: number): ColorGammaInterpolationFactory;
35}
36
37/**
38 * Type zoomView is used to represent a numeric array with three elements.
39 * In order of appearance the elements correspond to:
40 * - cx: *x*-coordinate of the center of the viewport
41 * - cy: *y*-coordinate of the center of the viewport
42 * - width: size of the viewport
43 */
44export type ZoomView = [number, number, number];
45
46export type TypedArray =
47 | Int8Array
48 | Uint8Array
49 | Int16Array
50 | Uint16Array
51 | Int32Array
52 | Uint32Array
53 | Uint8ClampedArray
54 | Float32Array
55 | Float64Array;
56
57export type NumberArray = TypedArray | DataView;
58
59// ---------------------------------------------------------------------------
60// Interpolation Function Factories
61// ---------------------------------------------------------------------------
62
63/**
64 * Returns an `null` constant interpolator.
65 */
66export function interpolate(a: any, b: null): ((t: number) => null);
67/**
68 * Returns an boolean constant interpolator of value `b`.
69 */
70export function interpolate(a: any, b: boolean): ((t: number) => boolean);
71/**
72 * Returns a `interpolateNumber` interpolator.
73 */
74export function interpolate(a: number | { valueOf(): number }, b: number): ((t: number) => number);
75/**
76 * Returns a `interpolateRgb` interpolator.
77 */
78export function interpolate(a: string | ColorCommonInstance, b: ColorCommonInstance): ((t: number) => string);
79/**
80 * Returns a `interpolateDate` interpolator.
81 */
82export function interpolate(a: Date, b: Date): ((t: number) => Date);
83/**
84 * Returns a `interpolateNumberArray` interpolator.
85 */
86export function interpolate<T extends NumberArray>(a: NumberArray | number[], b: T): ((t: number) => T);
87/**
88 * Returns a `interpolateString` interpolator. If `b` is a string coercible to a color use use `interpolateRgb`.
89 */
90export function interpolate(a: string | { toString(): string }, b: string): ((t: number) => string);
91/**
92 * Returns a `interpolateArray` interpolator.
93 */
94export function interpolate<U extends any[]>(a: any[], b: U): ((t: number) => U);
95/**
96 * Returns a `interpolateNumber` interpolator.
97 */
98export function interpolate(a: number | { valueOf(): number }, b: { valueOf(): number }): ((t: number) => number);
99/**
100 * Returns a `interpolateObject` interpolator.
101 */
102export function interpolate<U extends object>(a: any, b: U): ((t: number) => U);
103
104/**
105 * Returns an interpolator between the two numbers `a` and `b`.
106 * The returned interpolator is equivalent to: `(t) => a * (1 - t) + b * t`.
107 */
108export function interpolateNumber(a: number | { valueOf(): number }, b: number | { valueOf(): number }): ((t: number) => number);
109
110/**
111 * Returns an interpolator between the two numbers `a` and `b`; the interpolator is similar to `interpolateNumber`,
112 * except it will round the resulting value to the nearest integer.
113 */
114export function interpolateRound(a: number | { valueOf(): number }, b: number | { valueOf(): number }): ((t: number) => number);
115
116/**
117 * Returns an interpolator between the two strings `a` and `b`.
118 * The string interpolator finds numbers embedded in `a` and `b`, where each number is of the form understood by JavaScript.
119 * A few examples of numbers that will be detected within a string: `-1`, `42`, `3.14159`, and `6.0221413e+23`.
120 *
121 * For each number embedded in `b`, the interpolator will attempt to find a corresponding number in `a`.
122 * If a corresponding number is found, a numeric interpolator is created using `interpolateNumber`.
123 * The remaining parts of the string `b` are used as a template.
124 *
125 * For example, if `a` is `"300 12px sans-serif"`, and `b` is `"500 36px Comic-Sans"`, two embedded numbers are found.
126 * The remaining static parts (of string `b`) are a space between the two numbers (`" "`), and the suffix (`"px Comic-Sans"`).
127 * The result of the interpolator at `t` = 0.5 is `"400 24px Comic-Sans"`.
128 */
129export function interpolateString(a: string | { toString(): string }, b: string | { toString(): string }): ((t: number) => string);
130
131/**
132 * Returns an interpolator between the two dates `a` and `b`.
133 *
134 * Note: *no defensive copy* of the returned date is created; the same Date instance is returned for every evaluation of the interpolator.
135 * No copy is made for performance reasons; interpolators are often part of the inner loop of animated transitions.
136 */
137export function interpolateDate(a: Date, b: Date): ((t: number) => Date);
138
139export type ArrayInterpolator<A extends any[]> = ((t: number) => A);
140
141/**
142 * Returns an interpolator between the two arrays `a` and `b`. Internally, an array template is created that is the same length in `b`.
143 * For each element in `b`, if there exists a corresponding element in `a`, a generic interpolator is created for the two elements using `interpolate`.
144 * If there is no such element, the static value from `b` is used in the template.
145 * Then, for the given parameter `t`, the template’s embedded interpolators are evaluated. The updated array template is then returned.
146 *
147 * For example, if `a` is the array `[0, 1]` and `b` is the array `[1, 10, 100]`, then the result of the interpolator for `t = 0.5` is the array `[0.5, 5.5, 100]`.
148 *
149 * Note: *no defensive copy* of the template array is created; modifications of the returned array may adversely affect subsequent evaluation of the interpolator.
150 * No copy is made for performance reasons; interpolators are often part of the inner loop of animated transitions.
151 */
152export function interpolateArray<A extends any[]>(a: any[], b: A): ArrayInterpolator<A>;
153/**
154 * interpolateNumberArray is called
155 */
156export function interpolateArray<T extends NumberArray>(a: NumberArray | number[], b: T): ((t: number) => T);
157
158/**
159 * Returns an interpolator between the two arrays of numbers a and b.
160 * Internally, an array template is created that is the same type and length as b.
161 * For each element in b, if there exists a corresponding element in a, the values are directly interpolated in the array template.
162 * If there is no such element, the static value from b is copied.
163 * The updated array template is then returned.
164 *
165 * Note: For performance reasons, no defensive copy is made of the template array and the arguments a and b; modifications of these arrays may affect subsequent evaluation of the interpolator.
166 */
167export function interpolateNumberArray<T extends NumberArray | number[]>(a: NumberArray | number[], b: T): ((t: number) => T);
168
169/**
170 * Returns an interpolator between the two objects `a` and `b`. Internally, an object template is created that has the same properties as `b`.
171 * For each property in `b`, if there exists a corresponding property in `a`, a generic interpolator is created for the two elements using `interpolate`.
172 * If there is no such property, the static value from `b` is used in the template.
173 * Then, for the given parameter `t`, the template's embedded interpolators are evaluated and the updated object template is then returned.
174 *
175 * For example, if `a` is the object `{x: 0, y: 1}` and `b` is the object `{x: 1, y: 10, z: 100}`, the result of the interpolator for `t = 0.5` is the object `{x: 0.5, y: 5.5, z: 100}`.
176 *
177 * Note: *no defensive copy* of the template object is created; modifications of the returned object may adversely affect subsequent evaluation of the interpolator.
178 * No copy is made for performance reasons; interpolators are often part of the inner loop of animated transitions.
179 */
180export function interpolateObject<U extends object>(a: any, b: U): ((t: number) => U);
181
182/**
183 * Returns an interpolator between the two 2D CSS transforms represented by `a` and `b`.
184 * Each transform is decomposed to a standard representation of translate, rotate, *x*-skew and scale; these component transformations are then interpolated.
185 * This behavior is standardized by CSS: see [matrix decomposition for animation](http://www.w3.org/TR/css3-2d-transforms/#matrix-decomposition).
186 */
187export function interpolateTransformCss(a: string, b: string): ((t: number) => string);
188
189/**
190 * Returns an interpolator between the two 2D SVG transforms represented by `a` and `b`.
191 * Each transform is decomposed to a standard representation of translate, rotate, *x*-skew and scale; these component transformations are then interpolated.
192 * This behavior is standardized by CSS: see [matrix decomposition for animation](http://www.w3.org/TR/css3-2d-transforms/#matrix-decomposition).
193 */
194export function interpolateTransformSvg(a: string, b: string): ((t: number) => string);
195
196/**
197 * Returns an interpolator between the two views `a` and `b` of a two-dimensional plane,
198 * based on [“Smooth and efficient zooming and panning”](http://www.win.tue.nl/~vanwijk/zoompan.pdf).
199 * Each view is defined as an array of three numbers: *cx*, *cy* and *width*.
200 * The first two coordinates *cx*, *cy* represent the center of the viewport; the last coordinate *width* represents the size of the viewport.
201 *
202 * The returned interpolator exposes a *duration* property which encodes the recommended transition duration in milliseconds.
203 * This duration is based on the path length of the curved trajectory through *x,y* space.
204 * If you want to a slower or faster transition, multiply this by an arbitrary scale factor (*V* as described in the original paper).
205 */
206export function interpolateZoom(a: ZoomView, b: ZoomView): ZoomInterpolator;
207
208/**
209 * Returns a discrete interpolator for the given array of values. The returned interpolator maps `t` in `[0, 1 / n)` to values[0],
210 * `t` in `[1 / n, 2 / n)` to `values[1]`, and so on, where `n = values.length`. In effect, this is a lightweight quantize scale with a fixed domain of [0, 1].
211 */
212export function interpolateDiscrete<T>(values: T[]): ((t: number) => T);
213
214// Sampling ------------------------------------------------------------------
215
216/**
217 * Returns `n` uniformly-spaced samples from the specified `interpolator`, where `n` is an integer greater than one.
218 * The first sample is always at `t = 0`, and the last sample is always at `t = 1`.
219 * This can be useful in generating a fixed number of samples from a given interpolator,
220 * such as to derive the range of a [quantize scale](https://github.com/d3/d3-scale#quantize-scales) from a [continuous interpolator](https://github.com/d3/d3-scale#interpolateWarm).
221 *
222 * Caution: this method will not work with interpolators that do not return defensive copies of their output,
223 * such as `d3.interpolateArray`, `d3.interpolateDate` and `d3.interpolateObject`. For those interpolators, you must wrap the interpolator and create a copy for each returned value.
224 */
225export function quantize<T>(interpolator: ((t: number) => T), n: number): T[];
226
227// Color Spaces
228
229/**
230 * Returns an RGB color space interpolator between the two colors `a` and `b` with a configurable gamma. If the gamma is not specified, it defaults to 1.0.
231 * The colors `a` and `b` need not be in RGB; they will be converted to RGB using [`d3.rgb`](https://github.com/d3/d3-color#rgb). The return value of the interpolator is an RGB string.
232 */
233export const interpolateRgb: ColorGammaInterpolationFactory;
234
235/**
236 * Returns a uniform nonrational B-spline interpolator through the specified array of *colors*, which are converted to RGB color space.
237 * Implicit control points are generated such that the interpolator returns `colors[0]` at `t = 0` and `colors[colors.length - 1]` at `t = 1`.
238 * Opacity interpolation is not currently supported. See also `d3.interpolateBasis`, and see [d3-scale-chromatic](https://github.com/d3/d3-scale-chromatic) for examples.
239 */
240export function interpolateRgbBasis(colors: Array<string | ColorCommonInstance>): ((t: number) => string);
241
242/**
243 * Returns a uniform nonrational B-spline interpolator through the specified array of colors, which are converted to RGB color space.
244 * The control points are implicitly repeated such that the resulting spline has cyclical C² continuity when repeated around `t` in [0,1];
245 * this is useful, for example, to create cyclical color scales. Opacity interpolation is not currently supported.
246 * See also `d3.interpolateBasisClosed, and see [d3-scale-chromatic](https://github.com/d3/d3-scale-chromatic) for examples.
247 */
248export function interpolateRgbBasisClosed(colors: Array<string | ColorCommonInstance>): ((t: number) => string);
249
250/**
251 * Returns an HSL color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in HSL;
252 * they will be converted to HSL using `d3.hsl`. If either color’s hue or saturation is NaN, the opposing color’s channel value is used.
253 * The shortest path between hues is used. The return value of the interpolator is an RGB string.
254 */
255export function interpolateHsl(a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
256
257/**
258 * Like `interpolateHsl`, but does not use the shortest path between hues.
259 */
260export function interpolateHslLong(a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
261
262/**
263 * Returns a Lab color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in Lab;
264 * they will be converted to Lab using `d3.lab`. The return value of the interpolator is an RGB string.
265 */
266export function interpolateLab(a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
267
268/**
269 * Returns an HCL color space interpolator between the two colors `a` and `b`. The colors `a` and `b` need not be in HCL;
270 * they will be converted to HCL using `d3.hcl`. If either color’s hue or chroma is NaN, the opposing color’s channel value is used.
271 * The shortest path between hues is used. The return value of the interpolator is an RGB string.
272 */
273export function interpolateHcl(a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
274
275/**
276 * Like `interpolateHcl`, but does not use the shortest path between hues.
277 */
278export function interpolateHclLong(a: string | ColorCommonInstance, b: string | ColorCommonInstance): ((t: number) => string);
279
280/**
281 * Returns a Cubehelix color space interpolator between the two colors `a` and `b` using a configurable `gamma`.
282 * If the gamma is not specified, it defaults to 1.0. The colors `a` and `b` need not be in Cubehelix;
283 * they will be converted to Cubehelix using [`d3.cubehelix`](https://github.com/d3/d3-color#cubehelix).
284 * If either color’s hue or saturation is NaN, the opposing color’s channel value is used. The shortest path between hues is used. The return value of the interpolator is an RGB string.
285 */
286export const interpolateCubehelix: ColorGammaInterpolationFactory;
287
288/**
289 * Like `interpolateCubehelix`, but does not use the shortest path between hues.
290 */
291export const interpolateCubehelixLong: ColorGammaInterpolationFactory;
292
293/**
294 * Returns an interpolator between the two hue angles `a` and `b`. If either hue is NaN, the opposing value is used.
295 * The shortest path between hues is used. The return value of the interpolator is a number in `[0, 360)`.
296 */
297export function interpolateHue(a: number, b: number): ((t: number) => number);
298
299// Splines -------------------------------------------------------------------
300
301/**
302 * Returns a uniform nonrational B-spline interpolator through the specified array of `values`, which must be numbers.
303 * Implicit control points are generated such that the interpolator returns `values[0]` at `t` = 0 and `values[values.length - 1]` at `t` = 1.
304 * See also [`d3.curveBasis`](https://github.com/d3/d3-shape#curveBasis).
305 */
306export function interpolateBasis(splineNodes: number[]): ((t: number) => number);
307
308/**
309 * Returns a uniform nonrational B-spline interpolator through the specified array of `values`, which must be numbers.
310 * The control points are implicitly repeated such that the resulting one-dimensional spline has cyclical C² continuity when repeated around `t` in [0,1].
311 * See also [`d3.curveBasisClosed`](https://github.com/d3/d3-shape#curveBasisClosed).
312 */
313export function interpolateBasisClosed(splineNodes: number[]): ((t: number) => number);
314
315// Piecewise -----------------------------------------------------------------
316
317/**
318 * Returns a piecewise zoom interpolator, composing zoom interpolators for each adjacent pair of zoom view.
319 * The returned interpolator maps `t` in `[0, 1 / (n - 1)]` to `interpolate(values[0], values[1])`, `t` in `[1 / (n - 1), 2 / (n - 1)]` to `interpolate(values[1], values[2])`,
320 * and so on, where `n = values.length`. In effect, this is a lightweight linear scale.
321 * For example, to blend through three different zoom views: `d3.piecewise(d3.interpolateZoom, [[0, 0, 1], [0, 0, 10], [0, 0, 15]])`.
322 */
323export function piecewise(interpolate: (a: ZoomView, b: ZoomView) => ZoomInterpolator, values: ZoomView[]): ZoomInterpolator;
324
325/**
326 * Returns a piecewise array interpolator, composing array interpolators for each adjacent pair of arrays.
327 * The returned interpolator maps `t` in `[0, 1 / (n - 1)]` to `interpolate(values[0], values[1])`, `t` in `[1 / (n - 1), 2 / (n - 1)]` to `interpolate(values[1], values[2])`,
328 * and so on, where `n = values.length`. In effect, this is a lightweight linear scale.
329 * For example, to blend through three different arrays: `d3.piecewise(d3.interpolateArray, [[0, 0, 1], [0, 0, 10], [0, 0, 15]])`.
330 */
331export function piecewise<A extends any[]>(interpolate: (a: any[], b: A) => ArrayInterpolator<A>, values: A[]): ArrayInterpolator<A>;
332
333/**
334 * Returns a piecewise interpolator, composing interpolators for each adjacent pair of values.
335 * The returned interpolator maps `t` in `[0, 1 / (n - 1)]` to `interpolate(values[0], values[1])`, `t` in `[1 / (n - 1), 2 / (n - 1)]` to `interpolate(values[1], values[2])`,
336 * and so on, where `n = values.length`. In effect, this is a lightweight linear scale.
337 * For example, to blend through red, green and blue: `d3.piecewise(d3.interpolateRgb.gamma(2.2), ["red", "green", "blue"])`.
338 */
339export function piecewise<TData, Interpolator>(interpolate: (a: TData, b: TData) => Interpolator, values: TData[]): (t: number) => any;
340
\No newline at end of file