Skip to content

Commit

Permalink
Optional parameters to interpolators.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 19, 2015
1 parent d894818 commit 19a7122
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 63 deletions.
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,30 +163,31 @@ Returns an HCL color space interpolator between the two colors *a* and *b*. The

Like [hcl](#hcl), but does not use the shortest path between hues.

<a name="cubehelix" href="#cubehelix">#</a> d3_interpolate.<b>cubehelix</b>(<i>a</i>, <i>b</i>)
<a name="cubehelix" href="#cubehelix">#</a> d3_interpolate.<b>cubehelix</b>(<i>a</i>, <i>b</i>[, <i>gamma</i>])

<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelix.png" width="100%" height="80" alt="cubehelix">

Returns a Cubehelix color space interpolator between the two colors *a* and *b* using the default *gamma* of 1.0. The colors *a* and *b* need not be in Cubehelix; they will be converted to Cubehelix using [color.cubehelix](https://github.com/d3/d3-color#cubehelix). 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 a hexadecimal RGB string.
Or, with a gamma of 3.0 to emphasize high-intensity values:

<a name="cubehelixLong" href="#cubehelixLong">#</a> d3_interpolate.<b>cubehelixLong</b>(<i>a</i>, <i>b</i>)
<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixGamma.png" width="100%" height="80" alt="cubehelixGamma">

<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixLong.png" width="100%" height="80" alt="cubehelixLong">
Returns a Cubehelix color space interpolator between the two colors *a* and *b* using the specified *gamma*. If *gamma* is not specified, it defaults to 1.0. The colors *a* and *b* need not be in Cubehelix; they will be converted to Cubehelix using [color.cubehelix](https://github.com/d3/d3-color#cubehelix). 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 a hexadecimal RGB string.

Like [cubehelix](#cubehelix), but does not use the shortest path between hues.
<a name="cubehelixLong" href="#cubehelixLong">#</a> d3_interpolate.<b>cubehelixLong</b>(<i>a</i>, <i>b</i>[, <i>gamma</i>])

<a name="cubehelixGamma" href="#cubehelixGamma">#</a> d3_interpolate.<b>cubehelixGamma</b>(<i>gamma</i>)
<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixLong.png" width="100%" height="80" alt="cubehelixLong">

<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixGamma.png" width="100%" height="80" alt="cubehelixGamma">
Or, with a gamma of 3.0 to emphasize high-intensity values:

Returns a Cubehelix color space interpolator factory using the specified *gamma*. A gamma value less than one emphasizes low intensity values, while a gamma value greater than one emphasizes high intensity values. For example:
<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixGammaLong.png" width="100%" height="80" alt="cubehelixGammaLong">

```js
var i = d3_interpolate.cubehelixGamma(3)("purple", "orange");
```
Like [cubehelix](#cubehelix), but does not use the shortest path between hues.

<a name="cubehelixGammaLong" href="#cubehelixGammaLong">#</a> d3_interpolate.<b>cubehelixGammaLong</b>(<i>gamma</i>)
<a name="bind" href="#bind">#</a> d3_interpolate.<b>bind</b>(<i>type</i>[, <i>parameters…</i>])

<img src="https://raw.githubusercontent.com/d3/d3-interpolate/master/img/cubehelixGammaLong.png" width="100%" height="80" alt="cubehelixGammaLong">
A convenience function for binding zero or more *parameters* to the specified interpolation function *type*. If no *parameters* are specified, this function simply returns *type*. The returned function takes two arguments *a* and *b* and passes any optional *parameters* to the underlying function *type*. For example, the following statements are equivalent:

Like [cubehelixGamma](#cubehelixGamma), but does not use the shortest path between hues.
```js
d3_interpolate.bind(d3_interpolate.cubehelix, 3)("purple", "orange");
d3_interpolate.cubehelix("purple", "orange", 3);
```
9 changes: 3 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ 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};
export {default as cubehelix} from "./src/cubehelix";
export {default as cubehelixLong} from "./src/cubehelixLong";
export {default as bind} from "./src/bind";
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "d3-interpolate",
"version": "0.2.1",
"version": "0.3.0",
"description": "Interpolate numbers, colors, strings, arrays, objects, whatever!",
"keywords": [
"d3",
"interpolate",
"interpolation"
"interpolation",
"color"
],
"homepage": "https://github.com/d3/d3-interpolate",
"license": "BSD-3-Clause",
Expand Down
16 changes: 16 additions & 0 deletions src/bind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var slice = Array.prototype.slice;

function bindN(type, args) {
args = slice.call(args);
args[0] = null;
args.unshift(null);
return function(a, b) {
args[0] = a;
args[1] = b;
return type.apply(null, args);
};
}

export default function(type) {
return arguments.length === 1 ? type : bindN(type, arguments);
};
20 changes: 20 additions & 0 deletions src/cubehelix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {cubehelix} from "d3-color";
import deltaHue from "./deltaHue";

export default function(a, b, gamma) {
if (arguments.length < 3) gamma = 1;
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 + "";
};
};
21 changes: 0 additions & 21 deletions src/cubehelixGamma.js

This file was deleted.

20 changes: 0 additions & 20 deletions src/cubehelixGammaLong.js

This file was deleted.

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

export default function(a, b, gamma) {
if (arguments.length < 3) gamma = 1;
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 + "";
};
};
15 changes: 15 additions & 0 deletions test/bind-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var tape = require("tape"),
interpolate = require("../");

tape("bind(type) returns type", function(test) {
test.equal(interpolate.bind(interpolate.cubehelix), interpolate.cubehelix);
test.equal(interpolate.bind(interpolate.rgb), interpolate.rgb);
test.end();
});

tape("bind(type, parameter) binds the specified parameter to the given type", function(test) {
test.equal(interpolate.bind(interpolate.cubehelix, 3)("purple", "orange")(0.5), interpolate.cubehelix("purple", "orange", 3)(0.5));
test.equal(interpolate.bind(interpolate.cubehelixLong, 3)("purple", "orange")(0.5), interpolate.cubehelixLong("purple", "orange", 3)(0.5));
test.equal(interpolate.bind(interpolate.rgb, 3)("purple", "orange")(0.5), interpolate.rgb("purple", "orange", 3)(0.5)); // ignored
test.end();
});

0 comments on commit 19a7122

Please sign in to comment.