Skip to content

Commit

Permalink
Merge branch 'no-prefix'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 25, 2015
2 parents 7c7e7c8 + 2130781 commit 5f3a7b0
Show file tree
Hide file tree
Showing 39 changed files with 721 additions and 214 deletions.
128 changes: 101 additions & 27 deletions README.md

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export {default as interpolate} from "./src/interpolate";
export {default as interpolateArray} from "./src/interpolateArray";
export {default as interpolateNumber} from "./src/interpolateNumber";
export {default as interpolateObject} from "./src/interpolateObject";
export {default as interpolateRound} from "./src/interpolateRound";
export {default as interpolateString} from "./src/interpolateString";
export {default as interpolateTransform} from "./src/interpolateTransform";
export {default as interpolateZoom} from "./src/interpolateZoom";
export {default as interpolators} from "./src/interpolators";
export {default as array} from "./src/array";
export {default as number} from "./src/number";
export {default as object} from "./src/object";
export {default as round} from "./src/round";
export {default as string} from "./src/string";
export {default as transform} from "./src/transform";
export {default as values} from "./src/values";
export {default as value} from "./src/value";
export {default as zoom} from "./src/zoom";
export {default as rgb} from "./src/rgb";
export {default as hsl} from "./src/hsl";
export {default as hslLong} from "./src/hslLong";
export {default as lab} from "./src/lab";
export {default as hcl} from "./src/hcl";
export {default as hclLong} from "./src/hclLong";

import cubehelixGamma from "./src/cubehelixGamma";
import cubehelixGammaLong from "./src/cubehelixGammaLong";
export var cubehelix = cubehelixGamma(1);
export var cubehelixLong = cubehelixGammaLong(1);
export {cubehelixGamma, cubehelixGammaLong};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3-interpolate",
"version": "0.1.4",
"version": "0.2.0",
"description": "Interpolate numbers, colors, strings, arrays, objects, whatever!",
"keywords": [
"d3",
Expand All @@ -25,7 +25,7 @@
"prepublish": "npm run test && uglifyjs build/d3-interpolate.js -c -m -o build/d3-interpolate.min.js && rm -f build/d3-interpolate.zip && zip -j build/d3-interpolate.zip -- LICENSE README.md build/d3-interpolate.js build/d3-interpolate.min.js"
},
"dependencies": {
"d3-color": "~0.2.8"
"d3-color": "~0.3.1"
},
"devDependencies": {
"faucet": "0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/interpolateArray.js → src/array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import interpolate from "./interpolate";
import value from "./value";

// TODO sparse arrays?
export default function(a, b) {
Expand All @@ -9,7 +9,7 @@ export default function(a, b) {
n0 = Math.min(a.length, b.length),
i;

for (i = 0; i < n0; ++i) x.push(interpolate(a[i], b[i]));
for (i = 0; i < n0; ++i) x.push(value(a[i], b[i]));
for (; i < na; ++i) c[i] = a[i];
for (; i < nb; ++i) c[i] = b[i];

Expand Down
21 changes: 21 additions & 0 deletions src/cubehelixGamma.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {cubehelix} from "d3-color";
import deltaHue from "./deltaHue";

export default function(gamma) {
return function(a, b) {
a = cubehelix(a);
b = cubehelix(b);
var ah = isNaN(a.h) ? b.h : a.h,
as = isNaN(a.s) ? b.s : a.s,
al = a.l,
bh = isNaN(b.h) ? 0 : deltaHue(b.h, ah),
bs = isNaN(b.s) ? 0 : b.s - as,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * Math.pow(t, gamma);
return a + "";
};
};
};
20 changes: 20 additions & 0 deletions src/cubehelixGammaLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {cubehelix} from "d3-color";

export default function(gamma) {
return function(a, b) {
a = cubehelix(a);
b = cubehelix(b);
var ah = isNaN(a.h) ? b.h : a.h,
as = isNaN(a.s) ? b.s : a.s,
al = a.l,
bh = isNaN(b.h) ? 0 : b.h - ah,
bs = isNaN(b.s) ? 0 : b.s - as,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * Math.pow(t, gamma);
return a + "";
};
};
};
6 changes: 6 additions & 0 deletions src/deltaHue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default function(h1, h0) {
var delta = h1 - h0;
return delta > 180 || delta < -180
? delta - 360 * Math.round(delta / 360)
: delta;
};
19 changes: 19 additions & 0 deletions src/hcl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {hcl} from "d3-color";
import deltaHue from "./deltaHue";

export default function(a, b) {
a = hcl(a);
b = hcl(b);
var ah = isNaN(a.h) ? b.h : a.h,
ac = isNaN(a.c) ? b.c : a.c,
al = a.l,
bh = isNaN(b.h) ? 0 : deltaHue(b.h, ah),
bc = isNaN(b.c) ? 0 : b.c - ac,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.c = ac + bc * t;
a.l = al + bl * t;
return a + "";
};
};
18 changes: 18 additions & 0 deletions src/hclLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {hcl} from "d3-color";

export default function(a, b) {
a = hcl(a);
b = hcl(b);
var ah = isNaN(a.h) ? b.h : a.h,
ac = isNaN(a.c) ? b.c : a.c,
al = a.l,
bh = isNaN(b.h) ? 0 : b.h - ah,
bc = isNaN(b.c) ? 0 : b.c - ac,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.c = ac + bc * t;
a.l = al + bl * t;
return a + "";
};
};
19 changes: 19 additions & 0 deletions src/hsl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {hsl} from "d3-color";
import deltaHue from "./deltaHue";

export default function(a, b) {
a = hsl(a);
b = hsl(b);
var ah = isNaN(a.h) ? b.h : a.h,
as = isNaN(a.s) ? b.s : a.s,
al = a.l,
bh = isNaN(b.h) ? 0 : deltaHue(b.h, ah),
bs = isNaN(b.s) ? 0 : b.s - as,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * t;
return a + "";
};
};
18 changes: 18 additions & 0 deletions src/hslLong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {hsl} from "d3-color";

export default function(a, b) {
a = hsl(a);
b = hsl(b);
var ah = isNaN(a.h) ? b.h : a.h,
as = isNaN(a.s) ? b.s : a.s,
al = a.l,
bh = isNaN(b.h) ? 0 : b.h - ah,
bs = isNaN(b.s) ? 0 : b.s - as,
bl = b.l - al;
return function(t) {
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * t;
return a + "";
};
};
7 changes: 0 additions & 7 deletions src/interpolate.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/interpolators.js

This file was deleted.

18 changes: 18 additions & 0 deletions src/lab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {lab} from "d3-color";

export default function(a, b) {
a = lab(a);
b = lab(b);
var al = a.l,
aa = a.a,
ab = a.b,
bl = b.l - al,
ba = b.a - aa,
bb = b.b - ab;
return function(t) {
a.l = al + bl * t;
a.a = aa + ba * t;
a.b = ab + bb * t;
return a + "";
};
};
File renamed without changes.
4 changes: 2 additions & 2 deletions src/interpolateObject.js → src/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import interpolate from "./interpolate";
import value from "./value";

export default function(a, b) {
var i = {},
Expand All @@ -7,7 +7,7 @@ export default function(a, b) {

for (k in a) {
if (k in b) {
i[k] = interpolate(a[k], b[k]);
i[k] = value(a[k], b[k]);
} else {
c[k] = a[k];
}
Expand Down
18 changes: 18 additions & 0 deletions src/rgb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {rgb} from "d3-color";

export default function(a, b) {
a = rgb(a);
b = rgb(b);
var ar = a.r,
ag = a.g,
ab = a.b,
br = b.r - ar,
bg = b.g - ag,
bb = b.b - ab;
return function(t) {
a.r = ar + br * t;
a.g = ag + bg * t;
a.b = ab + bb * t;
return a + "";
};
};
File renamed without changes.
12 changes: 6 additions & 6 deletions src/interpolateString.js → src/string.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import interpolateNumber from "./interpolateNumber";
import number from "./number";

var reA = /[-+]?(?:\d+\.?\d*|\.?\d+)(?:[eE][-+]?\d+)?/g,
reB = new RegExp(reA.source, "g");

function interpolate0(b) {
function zero(b) {
return function() {
return b;
};
}

function interpolate1(b) {
function one(b) {
return function(t) {
return b(t) + "";
};
Expand Down Expand Up @@ -40,7 +40,7 @@ export default function(a, b) {
else s[++i] = bm;
} else { // interpolate non-matching numbers
s[++i] = null;
q.push({i: i, x: interpolateNumber(am, bm)});
q.push({i: i, x: number(am, bm)});
}
bi = reB.lastIndex;
}
Expand All @@ -55,8 +55,8 @@ export default function(a, b) {
// Special optimization for only a single match.
// Otherwise, interpolate each of the numbers and rejoin the string.
return s.length < 2 ? (q[0]
? interpolate1(q[0].x)
: interpolate0(b))
? one(q[0].x)
: zero(b))
: (b = q.length, function(t) {
for (var i = 0, o; i < b; ++i) s[(o = q[i]).i] = o.x(t);
return s.join("");
Expand Down
26 changes: 13 additions & 13 deletions src/interpolateTransform.js → src/transform.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import interpolateNumber from "./interpolateNumber";
import number from "./number";

var rad2deg = 180 / Math.PI,
identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0},
Expand Down Expand Up @@ -53,36 +53,36 @@ function pop(s) {
return s.length ? s.pop() + "," : "";
}

function interpolateTranslate(ta, tb, s, q) {
function translate(ta, tb, s, q) {
if (ta[0] !== tb[0] || ta[1] !== tb[1]) {
var i = s.push("translate(", null, ",", null, ")");
q.push({i: i - 4, x: interpolateNumber(ta[0], tb[0])}, {i: i - 2, x: interpolateNumber(ta[1], tb[1])});
q.push({i: i - 4, x: number(ta[0], tb[0])}, {i: i - 2, x: number(ta[1], tb[1])});
} else if (tb[0] || tb[1]) {
s.push("translate(" + tb + ")");
}
}

function interpolateRotate(ra, rb, s, q) {
function rotate(ra, rb, s, q) {
if (ra !== rb) {
if (ra - rb > 180) rb += 360; else if (rb - ra > 180) ra += 360; // shortest path
q.push({i: s.push(pop(s) + "rotate(", null, ")") - 2, x: interpolateNumber(ra, rb)});
q.push({i: s.push(pop(s) + "rotate(", null, ")") - 2, x: number(ra, rb)});
} else if (rb) {
s.push(pop(s) + "rotate(" + rb + ")");
}
}

function interpolateSkew(wa, wb, s, q) {
function skew(wa, wb, s, q) {
if (wa !== wb) {
q.push({i: s.push(pop(s) + "skewX(", null, ")") - 2, x: interpolateNumber(wa, wb)});
q.push({i: s.push(pop(s) + "skewX(", null, ")") - 2, x: number(wa, wb)});
} else if (wb) {
s.push(pop(s) + "skewX(" + wb + ")");
}
}

function interpolateScale(ka, kb, s, q) {
function scale(ka, kb, s, q) {
if (ka[0] !== kb[0] || ka[1] !== kb[1]) {
var i = s.push(pop(s) + "scale(", null, ",", null, ")");
q.push({i: i - 4, x: interpolateNumber(ka[0], kb[0])}, {i: i - 2, x: interpolateNumber(ka[1], kb[1])});
q.push({i: i - 4, x: number(ka[0], kb[0])}, {i: i - 2, x: number(ka[1], kb[1])});
} else if (kb[0] !== 1 || kb[1] !== 1) {
s.push(pop(s) + "scale(" + kb + ")");
}
Expand All @@ -92,10 +92,10 @@ export default function(a, b) {
var s = [], // string constants and placeholders
q = []; // number interpolators
a = new Transform(a), b = new Transform(b);
interpolateTranslate(a.translate, b.translate, s, q);
interpolateRotate(a.rotate, b.rotate, s, q);
interpolateSkew(a.skew, b.skew, s, q);
interpolateScale(a.scale, b.scale, s, q);
translate(a.translate, b.translate, s, q);
rotate(a.rotate, b.rotate, s, q);
skew(a.skew, b.skew, s, q);
scale(a.scale, b.scale, s, q);
a = b = null; // gc
return function(t) {
var i = -1, n = q.length, o;
Expand Down
7 changes: 7 additions & 0 deletions src/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import values from "./values";

export default function(a, b) {
var i = values.length, f;
while (--i >= 0 && !(f = values[i](a, b)));
return f;
};
17 changes: 17 additions & 0 deletions src/values.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {color} from "d3-color";
import rgb from "./rgb";
import array from "./array";
import number from "./number";
import object from "./object";
import string from "./string";

export default [
function(a, b) {
var t = typeof b, c;
return (t === "string" ? ((c = color(b)) ? (b = c, rgb) : string)
: b instanceof color ? rgb
: Array.isArray(b) ? array
: t === "object" && isNaN(b) ? object
: number)(a, b);
}
];
File renamed without changes.
Loading

0 comments on commit 5f3a7b0

Please sign in to comment.