UNPKG

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