UNPKG

13.6 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-color')) :
3 typeof define === 'function' && define.amd ? define(['exports', 'd3-color'], factory) :
4 (factory((global.d3_interpolate = {}),global.d3_color));
5}(this, function (exports,d3Color) { 'use strict';
6
7 function constant(x) {
8 return function() {
9 return x;
10 };
11 }
12
13 function linear(a, d) {
14 return function(t) {
15 return a + t * d;
16 };
17 }
18
19 function exponential(a, b, y) {
20 return a = Math.pow(a, y), b = Math.pow(b, y) - a, y = 1 / y, function(t) {
21 return Math.pow(a + t * b, y);
22 };
23 }
24
25 function hue(a, b) {
26 var d = b - a;
27 return d ? linear(a, d > 180 || d < -180 ? d - 360 * Math.round(d / 360) : d) : constant(isNaN(a) ? b : a);
28 }
29
30 function gamma(y) {
31 return (y = +y) === 1 ? nogamma : function(a, b) {
32 return b - a ? exponential(a, b, y) : constant(isNaN(a) ? b : a);
33 };
34 }
35
36 function nogamma(a, b) {
37 var d = b - a;
38 return d ? linear(a, d) : constant(isNaN(a) ? b : a);
39 }
40
41 var rgb$1 = (function gamma$$(y) {
42 var interpolateColor = gamma(y);
43
44 function interpolateRgb(start, end) {
45 var r = interpolateColor((start = d3Color.rgb(start)).r, (end = d3Color.rgb(end)).r),
46 g = interpolateColor(start.g, end.g),
47 b = interpolateColor(start.b, end.b),
48 opacity = interpolateColor(start.opacity, end.opacity);
49 return function(t) {
50 start.r = r(t);
51 start.g = g(t);
52 start.b = b(t);
53 start.opacity = opacity(t);
54 return start + "";
55 };
56 }
57
58 interpolateRgb.gamma = gamma$$;
59
60 return interpolateRgb;
61 })(1);
62
63 // TODO sparse arrays?
64 function array(a, b) {
65 var x = [],
66 c = [],
67 na = a ? a.length : 0,
68 nb = b ? b.length : 0,
69 n0 = Math.min(na, nb),
70 i;
71
72 for (i = 0; i < n0; ++i) x.push(value(a[i], b[i]));
73 for (; i < na; ++i) c[i] = a[i];
74 for (; i < nb; ++i) c[i] = b[i];
75
76 return function(t) {
77 for (i = 0; i < n0; ++i) c[i] = x[i](t);
78 return c;
79 };
80 }
81
82 function number(a, b) {
83 return a = +a, b -= a, function(t) {
84 return a + b * t;
85 };
86 }
87
88 function object(a, b) {
89 var i = {},
90 c = {},
91 k;
92
93 if (a === null || typeof a !== "object") a = {};
94 if (b === null || typeof b !== "object") b = {};
95
96 for (k in a) {
97 if (k in b) {
98 i[k] = value(a[k], b[k]);
99 } else {
100 c[k] = a[k];
101 }
102 }
103
104 for (k in b) {
105 if (!(k in a)) {
106 c[k] = b[k];
107 }
108 }
109
110 return function(t) {
111 for (k in i) c[k] = i[k](t);
112 return c;
113 };
114 }
115
116 var reA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g;
117 var reB = new RegExp(reA.source, "g");
118 function zero(b) {
119 return function() {
120 return b;
121 };
122 }
123
124 function one(b) {
125 return function(t) {
126 return b(t) + "";
127 };
128 }
129
130 function string(a, b) {
131 var bi = reA.lastIndex = reB.lastIndex = 0, // scan index for next number in b
132 am, // current match in a
133 bm, // current match in b
134 bs, // string preceding current number in b, if any
135 i = -1, // index in s
136 s = [], // string constants and placeholders
137 q = []; // number interpolators
138
139 // Coerce inputs to strings.
140 a = a + "", b = b + "";
141
142 // Interpolate pairs of numbers in a & b.
143 while ((am = reA.exec(a))
144 && (bm = reB.exec(b))) {
145 if ((bs = bm.index) > bi) { // a string precedes the next number in b
146 bs = b.slice(bi, bs);
147 if (s[i]) s[i] += bs; // coalesce with previous string
148 else s[++i] = bs;
149 }
150 if ((am = am[0]) === (bm = bm[0])) { // numbers in a & b match
151 if (s[i]) s[i] += bm; // coalesce with previous string
152 else s[++i] = bm;
153 } else { // interpolate non-matching numbers
154 s[++i] = null;
155 q.push({i: i, x: number(am, bm)});
156 }
157 bi = reB.lastIndex;
158 }
159
160 // Add remains of b.
161 if (bi < b.length) {
162 bs = b.slice(bi);
163 if (s[i]) s[i] += bs; // coalesce with previous string
164 else s[++i] = bs;
165 }
166
167 // Special optimization for only a single match.
168 // Otherwise, interpolate each of the numbers and rejoin the string.
169 return s.length < 2 ? (q[0]
170 ? one(q[0].x)
171 : zero(b))
172 : (b = q.length, function(t) {
173 for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
174 return s.join("");
175 });
176 }
177
178 var values = [
179 function(a, b) {
180 var t = typeof b, c;
181 return (t === "string" ? ((c = d3Color.color(b)) ? (b = c, rgb$1) : string)
182 : b instanceof d3Color.color ? rgb$1
183 : Array.isArray(b) ? array
184 : t === "object" && isNaN(b) ? object
185 : number)(a, b);
186 }
187 ];
188
189 function value(a, b) {
190 var i = values.length, f;
191 while (--i >= 0 && !(f = values[i](a, b)));
192 return f;
193 }
194
195 function round(a, b) {
196 return a = +a, b -= a, function(t) {
197 return Math.round(a + b * t);
198 };
199 }
200
201 var rad2deg = 180 / Math.PI;
202 var identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0};
203 var g;
204 // Compute x-scale and normalize the first row.
205 // Compute shear and make second row orthogonal to first.
206 // Compute y-scale and normalize the second row.
207 // Finally, compute the rotation.
208 function Transform(string) {
209 if (!g) g = document.createElementNS("http://www.w3.org/2000/svg", "g");
210 if (string) g.setAttribute("transform", string), t = g.transform.baseVal.consolidate();
211
212 var t,
213 m = t ? t.matrix : identity,
214 r0 = [m.a, m.b],
215 r1 = [m.c, m.d],
216 kx = normalize(r0),
217 kz = dot(r0, r1),
218 ky = normalize(combine(r1, r0, -kz)) || 0;
219
220 if (r0[0] * r1[1] < r1[0] * r0[1]) {
221 r0[0] *= -1;
222 r0[1] *= -1;
223 kx *= -1;
224 kz *= -1;
225 }
226
227 this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * rad2deg;
228 this.translate = [m.e, m.f];
229 this.scale = [kx, ky];
230 this.skew = ky ? Math.atan2(kz, ky) * rad2deg : 0;
231 }
232
233 function dot(a, b) {
234 return a[0] * b[0] + a[1] * b[1];
235 }
236
237 function normalize(a) {
238 var k = Math.sqrt(dot(a, a));
239 if (k) a[0] /= k, a[1] /= k;
240 return k;
241 }
242
243 function combine(a, b, k) {
244 a[0] += k * b[0];
245 a[1] += k * b[1];
246 return a;
247 }
248
249 function pop(s) {
250 return s.length ? s.pop() + "," : "";
251 }
252
253 function translate(ta, tb, s, q) {
254 if (ta[0] !== tb[0] || ta[1] !== tb[1]) {
255 var i = s.push("translate(", null, ",", null, ")");
256 q.push({i: i - 4, x: number(ta[0], tb[0])}, {i: i - 2, x: number(ta[1], tb[1])});
257 } else if (tb[0] || tb[1]) {
258 s.push("translate(" + tb + ")");
259 }
260 }
261
262 function rotate(ra, rb, s, q) {
263 if (ra !== rb) {
264 if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; // shortest path
265 q.push({i: s.push(pop(s) + "rotate(", null, ")") - 2, x: number(ra, rb)});
266 } else if (rb) {
267 s.push(pop(s) + "rotate(" + rb + ")");
268 }
269 }
270
271 function skew(wa, wb, s, q) {
272 if (wa !== wb) {
273 q.push({i: s.push(pop(s) + "skewX(", null, ")") - 2, x: number(wa, wb)});
274 } else if (wb) {
275 s.push(pop(s) + "skewX(" + wb + ")");
276 }
277 }
278
279 function scale(ka, kb, s, q) {
280 if (ka[0] !== kb[0] || ka[1] !== kb[1]) {
281 var i = s.push(pop(s) + "scale(", null, ",", null, ")");
282 q.push({i: i - 4, x: number(ka[0], kb[0])}, {i: i - 2, x: number(ka[1], kb[1])});
283 } else if (kb[0] !== 1 || kb[1] !== 1) {
284 s.push(pop(s) + "scale(" + kb + ")");
285 }
286 }
287
288 function transform(a, b) {
289 var s = [], // string constants and placeholders
290 q = []; // number interpolators
291 a = new Transform(a), b = new Transform(b);
292 translate(a.translate, b.translate, s, q);
293 rotate(a.rotate, b.rotate, s, q);
294 skew(a.skew, b.skew, s, q);
295 scale(a.scale, b.scale, s, q);
296 a = b = null; // gc
297 return function(t) {
298 var i = -1, n = q.length, o;
299 while (++i < n) s[(o = q[i]).i] = o.x(t);
300 return s.join("");
301 };
302 }
303
304 var rho = Math.SQRT2;
305 var rho2 = 2;
306 var rho4 = 4;
307 var epsilon2 = 1e-12;
308 function cosh(x) {
309 return ((x = Math.exp(x)) + 1 / x) / 2;
310 }
311
312 function sinh(x) {
313 return ((x = Math.exp(x)) - 1 / x) / 2;
314 }
315
316 function tanh(x) {
317 return ((x = Math.exp(2 * x)) - 1) / (x + 1);
318 }
319
320 // p0 = [ux0, uy0, w0]
321 // p1 = [ux1, uy1, w1]
322 function zoom(p0, p1) {
323 var ux0 = p0[0], uy0 = p0[1], w0 = p0[2],
324 ux1 = p1[0], uy1 = p1[1], w1 = p1[2],
325 dx = ux1 - ux0,
326 dy = uy1 - uy0,
327 d2 = dx * dx + dy * dy,
328 i,
329 S;
330
331 // Special case for u0 ≅ u1.
332 if (d2 < epsilon2) {
333 S = Math.log(w1 / w0) / rho;
334 i = function(t) {
335 return [
336 ux0 + t * dx,
337 uy0 + t * dy,
338 w0 * Math.exp(rho * t * S)
339 ];
340 }
341 }
342
343 // General case.
344 else {
345 var d1 = Math.sqrt(d2),
346 b0 = (w1 * w1 - w0 * w0 + rho4 * d2) / (2 * w0 * rho2 * d1),
347 b1 = (w1 * w1 - w0 * w0 - rho4 * d2) / (2 * w1 * rho2 * d1),
348 r0 = Math.log(Math.sqrt(b0 * b0 + 1) - b0),
349 r1 = Math.log(Math.sqrt(b1 * b1 + 1) - b1);
350 S = (r1 - r0) / rho;
351 i = function(t) {
352 var s = t * S,
353 coshr0 = cosh(r0),
354 u = w0 / (rho2 * d1) * (coshr0 * tanh(rho * s + r0) - sinh(r0));
355 return [
356 ux0 + u * dx,
357 uy0 + u * dy,
358 w0 * coshr0 / cosh(rho * s + r0)
359 ];
360 }
361 }
362
363 i.duration = S * 1000;
364
365 return i;
366 }
367
368 function interpolateHsl(start, end) {
369 var h = hue((start = d3Color.hsl(start)).h, (end = d3Color.hsl(end)).h),
370 s = nogamma(start.s, end.s),
371 l = nogamma(start.l, end.l),
372 opacity = nogamma(start.opacity, end.opacity);
373 return function(t) {
374 start.h = h(t);
375 start.s = s(t);
376 start.l = l(t);
377 start.opacity = opacity(t);
378 return start + "";
379 };
380 }
381
382 function interpolateHslLong(start, end) {
383 var h = nogamma((start = d3Color.hsl(start)).h, (end = d3Color.hsl(end)).h),
384 s = nogamma(start.s, end.s),
385 l = nogamma(start.l, end.l),
386 opacity = nogamma(start.opacity, end.opacity);
387 return function(t) {
388 start.h = h(t);
389 start.s = s(t);
390 start.l = l(t);
391 start.opacity = opacity(t);
392 return start + "";
393 };
394 }
395
396 function interpolateLab(start, end) {
397 var l = nogamma((start = d3Color.lab(start)).l, (end = d3Color.lab(end)).l),
398 a = nogamma(start.a, end.a),
399 b = nogamma(start.b, end.b),
400 opacity = nogamma(start.opacity, end.opacity);
401 return function(t) {
402 start.l = l(t);
403 start.a = a(t);
404 start.b = b(t);
405 start.opacity = opacity(t);
406 return start + "";
407 };
408 }
409
410 function interpolateHcl(start, end) {
411 var h = hue((start = d3Color.hcl(start)).h, (end = d3Color.hcl(end)).h),
412 c = nogamma(start.c, end.c),
413 l = nogamma(start.l, end.l),
414 opacity = nogamma(start.opacity, end.opacity);
415 return function(t) {
416 start.h = h(t);
417 start.c = c(t);
418 start.l = l(t);
419 start.opacity = opacity(t);
420 return start + "";
421 };
422 }
423
424 function interpolateHclLong(start, end) {
425 var h = nogamma((start = d3Color.hcl(start)).h, (end = d3Color.hcl(end)).h),
426 c = nogamma(start.c, end.c),
427 l = nogamma(start.l, end.l),
428 opacity = nogamma(start.opacity, end.opacity);
429 return function(t) {
430 start.h = h(t);
431 start.c = c(t);
432 start.l = l(t);
433 start.opacity = opacity(t);
434 return start + "";
435 };
436 }
437
438 var cubehelix$1 = (function gamma(y) {
439 y = +y;
440
441 function interpolateCubehelix(start, end) {
442 var h = hue((start = d3Color.cubehelix(start)).h, (end = d3Color.cubehelix(end)).h),
443 s = nogamma(start.s, end.s),
444 l = nogamma(start.l, end.l),
445 opacity = nogamma(start.opacity, end.opacity);
446 return function(t) {
447 start.h = h(t);
448 start.s = s(t);
449 start.l = l(Math.pow(t, y));
450 start.opacity = opacity(t);
451 return start + "";
452 };
453 }
454
455 interpolateCubehelix.gamma = gamma;
456
457 return interpolateCubehelix;
458 })(1);
459
460 var cubehelixLong = (function gamma(y) {
461 y = +y;
462
463 function interpolateCubehelixLong(start, end) {
464 var h = nogamma((start = d3Color.cubehelix(start)).h, (end = d3Color.cubehelix(end)).h),
465 s = nogamma(start.s, end.s),
466 l = nogamma(start.l, end.l),
467 opacity = nogamma(start.opacity, end.opacity);
468 return function(t) {
469 start.h = h(t);
470 start.s = s(t);
471 start.l = l(Math.pow(t, y));
472 start.opacity = opacity(t);
473 return start + "";
474 };
475 }
476
477 interpolateCubehelixLong.gamma = gamma;
478
479 return interpolateCubehelixLong;
480 })(1);
481
482 var version = "0.5.2";
483
484 exports.version = version;
485 exports.interpolate = value;
486 exports.interpolators = values;
487 exports.interpolateArray = array;
488 exports.interpolateNumber = number;
489 exports.interpolateObject = object;
490 exports.interpolateRound = round;
491 exports.interpolateString = string;
492 exports.interpolateTransform = transform;
493 exports.interpolateZoom = zoom;
494 exports.interpolateRgb = rgb$1;
495 exports.interpolateHsl = interpolateHsl;
496 exports.interpolateHslLong = interpolateHslLong;
497 exports.interpolateLab = interpolateLab;
498 exports.interpolateHcl = interpolateHcl;
499 exports.interpolateHclLong = interpolateHclLong;
500 exports.interpolateCubehelix = cubehelix$1;
501 exports.interpolateCubehelixLong = cubehelixLong;
502
503}));
\No newline at end of file