UNPKG

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