diff --git a/.eslintrc b/.eslintrc index b2f6ec6..bd022aa 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,9 +4,11 @@ ecmaFeatures: env: es6: true browser: true + node: true extends: "eslint:recommended" rules: no-cond-assign: 0 + no-floating-decimal: 2 diff --git a/README.md b/README.md index d7a661f..2782b27 100644 --- a/README.md +++ b/README.md @@ -15,16 +15,16 @@ The returned function `i` is called an *interpolator*. Given a starting value *a You can interpolate more than just numbers. To find the perceptual midpoint between steelblue and brown: ```js -d3.interpolateLab("steelblue", "brown")(0.5); // "#8e5c6d" +d3.interpolateLab("steelblue", "brown")(0.5); // "rgb(142, 92, 109)" ``` Here’s a more elaborate example demonstrating type inference used by [interpolate](#interpolate): ```js var i = d3.interpolate({colors: ["red", "blue"]}, {colors: ["white", "black"]}); -i(0.0); // {colors: ["#ff0000", "#0000ff"]} -i(0.5); // {colors: ["#ff8080", "#000080"]} -i(1.0); // {colors: ["#ffffff", "#000000"]} +i(0.0); // {colors: ["rgb(255, 0, 0)", "rgb(0, 0, 255)"]} +i(0.5); // {colors: ["rgb(255, 128, 128)", "rgb(0, 0, 128)"]} +i(1.0); // {colors: ["rgb(255, 255, 255)", "rgb(0, 0, 0)"]} ``` Note that the generic value interpolator detects not only nested objects and arrays, but also color strings and numbers embedded in strings! @@ -34,7 +34,7 @@ Note that the generic value interpolator detects not only nested objects and arr If you use NPM, `npm install d3-interpolate`. Otherwise, download the [latest release](https://github.com/d3/d3-interpolate/releases/latest). The released bundle supports AMD, CommonJS, and vanilla environments. Create a custom build using [Rollup](https://github.com/rollup/rollup) or your preferred bundler. You can also load directly from [d3js.org](https://d3js.org): ```html - + ``` @@ -97,13 +97,13 @@ Returns an interpolator between the two strings *a* and *b*. The string interpol For each number embedded in *b*, the interpolator will attempt to find a corresponding number in *a*. If a corresponding number is found, a numeric interpolator is created using [interpolateNumber](#interpolateNumber). The remaining parts of the string *b* are used as a template: the static parts of the string *b* remain constant for the interpolation, with the interpolated numeric values embedded in the template. -For example, if *a* is `"300 12px sans-serif"`, and *b* is `"500 36px Comic-Sans"`, two embedded numbers are found. The remaining static parts of the string are a space between the two numbers (`" "`), and the suffix (`"px Comic-Sans"`). The result of the interpolator at *t* = .5 is `"400 24px Comic-Sans"`. +For example, if *a* is `"300 12px sans-serif"`, and *b* is `"500 36px Comic-Sans"`, two embedded numbers are found. The remaining static parts of the string are a space between the two numbers (`" "`), and the suffix (`"px Comic-Sans"`). The result of the interpolator at *t* = 0.5 is `"400 24px Comic-Sans"`. # d3.interpolateArray(a, b) Returns an interpolator between the two arrays *a* and *b*. Internally, an array template is created that is the same length in *b*. For each element in *b*, if there exists a corresponding element in *a*, a generic interpolator is created for the two elements using [interpolate](#interpolate). If there is no such element, the static value from *b* is used in the template. Then, for the given parameter *t*, the template’s embedded interpolators are evaluated. The updated array template is then returned. -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* = .5 is the array `[.5, 5.5, 100]`. +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]`. Note: **no defensive copy** of the template array is created; modifications of the returned array may adversely affect subsequent evaluation of the interpolator. No copy is made for performance reasons; interpolators are often part of the inner loop of [animated transitions](https://github.com/d3/d3-transition). @@ -111,7 +111,7 @@ Note: **no defensive copy** of the template array is created; modifications of t Returns an interpolator between the two objects *a* and *b*. Internally, an object template is created that has the same properties as *b*. For each property in *b*, if there exists a corresponding property in *a*, a generic interpolator is created for the two elements using [interpolate](#interpolate). If there is no such property, the static value from *b* is used in the template. Then, for the given parameter *t*, the template's embedded interpolators are evaluated and the updated object template is then returned. -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* = .5 is the object `{x: .5, y: 5.5, z: 100}`. +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}`. Object interpolation is particularly useful for *dataspace interpolation*, where data is interpolated rather than attribute values. For example, you can interpolate an object which describes an arc in a pie chart, and then use d3.svg.arc to compute the new SVG path data. @@ -135,13 +135,13 @@ Or, with a corrected [gamma](#interpolate_gamma) of 2.2: rgbGamma -Returns an RGB color space interpolator between the two colors *a* and *b* with a configurable [gamma](#interpolate_gamma). If the gamma is not specified, it defaults to 1.0. The colors *a* and *b* need not be in RGB; they will be converted to RGB using [color.rgb](https://github.com/d3/d3-color#rgb). The return value of the interpolator is a hexadecimal RGB string. +Returns an RGB color space interpolator between the two colors *a* and *b* with a configurable [gamma](#interpolate_gamma). If the gamma is not specified, it defaults to 1.0. The colors *a* and *b* need not be in RGB; they will be converted to RGB using [color.rgb](https://github.com/d3/d3-color#rgb). The return value of the interpolator is an RGB string. # d3.interpolateHsl(a, b) hsl -Returns an HSL color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in HSL; they will be converted to HSL using [color.hsl](https://github.com/d3/d3-color#hsl). 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. +Returns an HSL color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in HSL; they will be converted to HSL using [color.hsl](https://github.com/d3/d3-color#hsl). 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. # d3.interpolateHslLong(a, b) @@ -153,13 +153,13 @@ Like [interpolateHsl](#interpolateHsl), but does not use the shortest path betwe lab -Returns a Lab color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in Lab; they will be converted to Lab using [color.lab](https://github.com/d3/d3-color#lab). The return value of the interpolator is a hexadecimal RGB string. +Returns a Lab color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in Lab; they will be converted to Lab using [color.lab](https://github.com/d3/d3-color#lab). The return value of the interpolator is an RGB string. # d3.interpolateHcl(a, b) hcl -Returns an HCL color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in HCL; they will be converted to HCL using [color.hcl](https://github.com/d3/d3-color#hcl). If either color’s hue or chroma 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. +Returns an HCL color space interpolator between the two colors *a* and *b*. The colors *a* and *b* need not be in HCL; they will be converted to HCL using [color.hcl](https://github.com/d3/d3-color#hcl). If either color’s hue or chroma 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. # d3.interpolateHclLong(a, b) @@ -175,7 +175,7 @@ Or, with a [gamma](#interpolate_gamma) of 3.0 to emphasize high-intensity values cubehelixGamma -Returns a Cubehelix color space interpolator between the two colors *a* and *b* using a configurable [gamma](#interpolate_gamma). If the 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. +Returns a Cubehelix color space interpolator between the two colors *a* and *b* using a configurable [gamma](#interpolate_gamma). If the 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 an RGB string. # d3.interpolateCubehelixLong(a, b) diff --git a/package.json b/package.json index 2057655..8d7915b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "d3-interpolate", - "version": "0.5.1", + "version": "0.5.2", "description": "Interpolate numbers, colors, strings, arrays, objects, whatever!", "keywords": [ "d3", @@ -22,12 +22,12 @@ }, "scripts": { "pretest": "mkdir -p build && node -e 'process.stdout.write(\"var version = \\\"\" + require(\"./package.json\").version + \"\\\"; export * from \\\"../index\\\"; export {version};\");' > build/bundle.js && rollup -f umd -g d3-color:d3_color -n d3_interpolate -o build/d3-interpolate.js -- build/bundle.js", - "test": "faucet `find test -name '*-test.js'` && eslint index.js src", + "test": "faucet `find test -name '*-test.js'` && eslint index.js src test", "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", "postpublish": "VERSION=`node -e 'console.log(require(\"./package.json\").version)'`; git push && git tag -am \"Release $VERSION.\" v${VERSION} && git push --tags && cp build/d3-interpolate.js ../d3.github.com/d3-interpolate.v0.5.js && cp build/d3-interpolate.min.js ../d3.github.com/d3-interpolate.v0.5.min.js && cd ../d3.github.com && git add d3-interpolate.v0.5.js d3-interpolate.v0.5.min.js && git commit -m \"d3-interpolate ${VERSION}\" && git push" }, "dependencies": { - "d3-color": "~0.3.4" + "d3-color": "~0.4.0" }, "devDependencies": { "faucet": "0.0", diff --git a/src/color.js b/src/color.js new file mode 100644 index 0000000..28833d8 --- /dev/null +++ b/src/color.js @@ -0,0 +1,29 @@ +import constant from "./constant"; + +function linear(a, d) { + return function(t) { + return a + t * d; + }; +} + +function exponential(a, b, y) { + return a = Math.pow(a, y), b = Math.pow(b, y) - a, y = 1 / y, function(t) { + return Math.pow(a + t * b, y); + }; +} + +export function hue(a, b) { + var d = b - a; + return d ? linear(a, d > 180 || d < -180 ? d - 360 * Math.round(d / 360) : d) : constant(isNaN(a) ? b : a); +} + +export function gamma(y) { + return (y = +y) === 1 ? nogamma : function(a, b) { + return b - a ? exponential(a, b, y) : constant(isNaN(a) ? b : a); + }; +} + +export default function nogamma(a, b) { + var d = b - a; + return d ? linear(a, d) : constant(isNaN(a) ? b : a); +} diff --git a/src/constant.js b/src/constant.js new file mode 100644 index 0000000..b7d42e7 --- /dev/null +++ b/src/constant.js @@ -0,0 +1,5 @@ +export default function(x) { + return function() { + return x; + }; +} diff --git a/src/cubehelix.js b/src/cubehelix.js index 10abab3..dd57d62 100644 --- a/src/cubehelix.js +++ b/src/cubehelix.js @@ -1,33 +1,24 @@ -import {cubehelix as color} from "d3-color"; -import deltaHue from "./deltaHue"; +import {cubehelix} from "d3-color"; +import interpolateColor, {hue as interpolateHue} from "./color"; export default (function gamma(y) { y = +y; - function cubehelix(a, b) { - a = color(a); - b = color(b); - var ah = a.h, - as = a.s, - al = a.l, - bh = b.h, - bs = b.s, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(as)) as = bs; - if (isNaN(al)) al = bl; - bh = deltaHue(bh, ah) || 0; - bs = (bs - as) || 0; - bl -= al; + function interpolateCubehelix(start, end) { + var h = interpolateHue((start = cubehelix(start)).h, (end = cubehelix(end)).h), + s = interpolateColor(start.s, end.s), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.s = as + bs * t; - a.l = al + bl * Math.pow(t, y); - return a + ""; + start.h = h(t); + start.s = s(t); + start.l = l(Math.pow(t, y)); + start.opacity = opacity(t); + return start + ""; }; } - cubehelix.gamma = gamma; + interpolateCubehelix.gamma = gamma; - return cubehelix; + return interpolateCubehelix; })(1); diff --git a/src/cubehelixLong.js b/src/cubehelixLong.js index fa793a8..e708973 100644 --- a/src/cubehelixLong.js +++ b/src/cubehelixLong.js @@ -1,32 +1,24 @@ -import {cubehelix as color} from "d3-color"; +import {cubehelix} from "d3-color"; +import interpolateColor from "./color"; export default (function gamma(y) { y = +y; - function cubehelix(a, b) { - a = color(a); - b = color(b); - var ah = a.h, - as = a.s, - al = a.l, - bh = b.h, - bs = b.s, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(as)) as = bs; - if (isNaN(al)) al = bl; - bh = (bh - ah) || 0; - bs = (bs - as) || 0; - bl -= al; + function interpolateCubehelixLong(start, end) { + var h = interpolateColor((start = cubehelix(start)).h, (end = cubehelix(end)).h), + s = interpolateColor(start.s, end.s), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.s = as + bs * t; - a.l = al + bl * Math.pow(t, y); - return a + ""; + start.h = h(t); + start.s = s(t); + start.l = l(Math.pow(t, y)); + start.opacity = opacity(t); + return start + ""; }; } - cubehelix.gamma = gamma; + interpolateCubehelixLong.gamma = gamma; - return cubehelix; + return interpolateCubehelixLong; })(1); diff --git a/src/deltaHue.js b/src/deltaHue.js deleted file mode 100644 index 9a25d74..0000000 --- a/src/deltaHue.js +++ /dev/null @@ -1,6 +0,0 @@ -export default function(h1, h0) { - var delta = h1 - h0; - return delta > 180 || delta < -180 - ? delta - 360 * Math.round(delta / 360) - : delta; -} diff --git a/src/hcl.js b/src/hcl.js index 0ea296b..f0cb24d 100644 --- a/src/hcl.js +++ b/src/hcl.js @@ -1,25 +1,16 @@ import {hcl} from "d3-color"; -import deltaHue from "./deltaHue"; +import interpolateColor, {hue as interpolateHue} from "./color"; -export default function(a, b) { - a = hcl(a); - b = hcl(b); - var ah = a.h, - ac = a.c, - al = a.l, - bh = b.h, - bc = b.c, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(ac)) ac = bc; - if (isNaN(al)) al = bl; - bh = deltaHue(bh, ah) || 0; - bc = (bc - ac) || 0; - bl -= al; +export default function interpolateHcl(start, end) { + var h = interpolateHue((start = hcl(start)).h, (end = hcl(end)).h), + c = interpolateColor(start.c, end.c), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.c = ac + bc * t; - a.l = al + bl * t; - return a + ""; + start.h = h(t); + start.c = c(t); + start.l = l(t); + start.opacity = opacity(t); + return start + ""; }; } diff --git a/src/hclLong.js b/src/hclLong.js index 233190c..81d9e8f 100644 --- a/src/hclLong.js +++ b/src/hclLong.js @@ -1,24 +1,16 @@ import {hcl} from "d3-color"; +import interpolateColor from "./color"; -export default function(a, b) { - a = hcl(a); - b = hcl(b); - var ah = a.h, - ac = a.c, - al = a.l, - bh = b.h, - bc = b.c, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(ac)) ac = bc; - if (isNaN(al)) al = bl; - bh = (bh - ah) || 0; - bc = (bc - ac) || 0; - bl -= al; +export default function interpolateHclLong(start, end) { + var h = interpolateColor((start = hcl(start)).h, (end = hcl(end)).h), + c = interpolateColor(start.c, end.c), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.c = ac + bc * t; - a.l = al + bl * t; - return a + ""; + start.h = h(t); + start.c = c(t); + start.l = l(t); + start.opacity = opacity(t); + return start + ""; }; } diff --git a/src/hsl.js b/src/hsl.js index e25755d..bcf8d9e 100644 --- a/src/hsl.js +++ b/src/hsl.js @@ -1,25 +1,16 @@ import {hsl} from "d3-color"; -import deltaHue from "./deltaHue"; +import interpolateColor, {hue as interpolateHue} from "./color"; -export default function(a, b) { - a = hsl(a); - b = hsl(b); - var ah = a.h, - as = a.s, - al = a.l, - bh = b.h, - bs = b.s, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(as)) as = bs; - if (isNaN(al)) al = bl; - bh = deltaHue(bh, ah) || 0; - bs = (bs - as) || 0; - bl -= al; +export default function interpolateHsl(start, end) { + var h = interpolateHue((start = hsl(start)).h, (end = hsl(end)).h), + s = interpolateColor(start.s, end.s), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.s = as + bs * t; - a.l = al + bl * t; - return a + ""; + start.h = h(t); + start.s = s(t); + start.l = l(t); + start.opacity = opacity(t); + return start + ""; }; } diff --git a/src/hslLong.js b/src/hslLong.js index a538a0c..b898534 100644 --- a/src/hslLong.js +++ b/src/hslLong.js @@ -1,24 +1,16 @@ import {hsl} from "d3-color"; +import interpolateColor from "./color"; -export default function(a, b) { - a = hsl(a); - b = hsl(b); - var ah = a.h, - as = a.s, - al = a.l, - bh = b.h, - bs = b.s, - bl = b.l || 0; - if (isNaN(ah)) ah = bh; - if (isNaN(as)) as = bs; - if (isNaN(al)) al = bl; - bh = (bh - ah) || 0; - bs = (bs - as) || 0; - bl -= al; +export default function interpolateHslLong(start, end) { + var h = interpolateColor((start = hsl(start)).h, (end = hsl(end)).h), + s = interpolateColor(start.s, end.s), + l = interpolateColor(start.l, end.l), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.h = ah + bh * t; - a.s = as + bs * t; - a.l = al + bl * t; - return a + ""; + start.h = h(t); + start.s = s(t); + start.l = l(t); + start.opacity = opacity(t); + return start + ""; }; } diff --git a/src/lab.js b/src/lab.js index aeed166..cb1ff29 100644 --- a/src/lab.js +++ b/src/lab.js @@ -1,24 +1,16 @@ import {lab} from "d3-color"; +import interpolateColor from "./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 || 0, - ba = b.a || 0, - bb = b.b || 0; - if (isNaN(al)) al = bl; - if (isNaN(aa)) aa = ba; - if (isNaN(ab)) ab = bb; - bl -= al; - ba -= aa; - bb -= ab; +export default function interpolateLab(start, end) { + var l = interpolateColor((start = lab(start)).l, (end = lab(end)).l), + a = interpolateColor(start.a, end.a), + b = interpolateColor(start.b, end.b), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.l = al + bl * t; - a.a = aa + ba * t; - a.b = ab + bb * t; - return a + ""; + start.l = l(t); + start.a = a(t); + start.b = b(t); + start.opacity = opacity(t); + return start + ""; }; } diff --git a/src/rgb.js b/src/rgb.js index 7c36685..80e0540 100644 --- a/src/rgb.js +++ b/src/rgb.js @@ -1,58 +1,24 @@ -import {rgb as color} from "d3-color"; +import {rgb} from "d3-color"; +import {gamma as interpolateGamma} from "./color"; -function rgb(a, b) { - a = color(a); - b = color(b); - var ar = a.r, - ag = a.g, - ab = a.b, - br = b.r || 0, - bg = b.g || 0, - bb = b.b || 0; - if (isNaN(ar)) ar = br; - if (isNaN(ag)) ag = bg; - if (isNaN(ab)) ab = bb; - br -= ar; - bg -= ag; - bb -= ab; - return function(t) { - a.r = ar + br * t; - a.g = ag + bg * t; - a.b = ab + bb * t; - return a + ""; - }; -} +export default (function gamma(y) { + var interpolateColor = interpolateGamma(y); -rgb.gamma = function gamma(y) { - y = +y; - - function rgb(a, b) { - a = color(a); - b = color(b); - var ar = Math.pow(a.r, y), - ag = Math.pow(a.g, y), - ab = Math.pow(a.b, y), - br = Math.pow(b.r || 0, y), - bg = Math.pow(b.g || 0, y), - bb = Math.pow(b.b || 0, y); - if (isNaN(ar)) ar = br; - if (isNaN(ag)) ag = bg; - if (isNaN(ab)) ab = bb; - br -= ar; - bg -= ag; - bb -= ab; - y = 1 / y; + function interpolateRgb(start, end) { + var r = interpolateColor((start = rgb(start)).r, (end = rgb(end)).r), + g = interpolateColor(start.g, end.g), + b = interpolateColor(start.b, end.b), + opacity = interpolateColor(start.opacity, end.opacity); return function(t) { - a.r = Math.pow(ar + br * t, y); - a.g = Math.pow(ag + bg * t, y); - a.b = Math.pow(ab + bb * t, y); - return a + ""; + start.r = r(t); + start.g = g(t); + start.b = b(t); + start.opacity = opacity(t); + return start + ""; }; } - rgb.gamma = gamma; - - return rgb; -}; + interpolateRgb.gamma = gamma; -export default rgb; + return interpolateRgb; +})(1); diff --git a/test/array-test.js b/test/array-test.js index 7920e46..15406b2 100644 --- a/test/array-test.js +++ b/test/array-test.js @@ -2,26 +2,26 @@ var tape = require("tape"), interpolate = require("../"); tape("interpolateArray(a, b) interpolates defined elements in a and b", function(test) { - test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24])(.5), [3, 18]); + test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24])(0.5), [3, 18]); test.end(); }); tape("interpolateArray(a, b) interpolates nested objects and arrays", function(test) { - test.deepEqual(interpolate.interpolateArray([[2, 12]], [[4, 24]])(.5), [[3, 18]]); - test.deepEqual(interpolate.interpolateArray([{foo: [2, 12]}], [{foo: [4, 24]}])(.5), [{foo: [3, 18]}]); + test.deepEqual(interpolate.interpolateArray([[2, 12]], [[4, 24]])(0.5), [[3, 18]]); + test.deepEqual(interpolate.interpolateArray([{foo: [2, 12]}], [{foo: [4, 24]}])(0.5), [{foo: [3, 18]}]); test.end(); }); tape("interpolateArray(a, b) merges non-shared elements", function(test) { - test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24, 12])(.5), [3, 18, 12]); - test.deepEqual(interpolate.interpolateArray([2, 12, 12], [4, 24])(.5), [3, 18, 12]); + test.deepEqual(interpolate.interpolateArray([2, 12], [4, 24, 12])(0.5), [3, 18, 12]); + test.deepEqual(interpolate.interpolateArray([2, 12, 12], [4, 24])(0.5), [3, 18, 12]); test.end(); }); tape("interpolateArray(a, b) treats undefined as an empty array", function(test) { - test.deepEqual(interpolate.interpolateArray(undefined, [2, 12])(.5), [2, 12]); - test.deepEqual(interpolate.interpolateArray([2, 12], undefined)(.5), [2, 12]); - test.deepEqual(interpolate.interpolateArray(undefined, undefined)(.5), []); + test.deepEqual(interpolate.interpolateArray(undefined, [2, 12])(0.5), [2, 12]); + test.deepEqual(interpolate.interpolateArray([2, 12], undefined)(0.5), [2, 12]); + test.deepEqual(interpolate.interpolateArray(undefined, undefined)(0.5), []); test.end(); }); @@ -30,7 +30,7 @@ tape("interpolateArray(a, b) interpolates array-like objects", function(test) { args = (function() { return arguments; })(2, 12); array[0] = 2; array[1] = 12; - test.deepEqual(interpolate.interpolateArray(array, [4, 24])(.5), [3, 18]); - test.deepEqual(interpolate.interpolateArray(args, [4, 24])(.5), [3, 18]); + test.deepEqual(interpolate.interpolateArray(array, [4, 24])(0.5), [3, 18]); + test.deepEqual(interpolate.interpolateArray(args, [4, 24])(0.5), [3, 18]); test.end(); }); diff --git a/test/cubehelix-test.js b/test/cubehelix-test.js index eeac960..18da14a 100644 --- a/test/cubehelix-test.js +++ b/test/cubehelix-test.js @@ -9,18 +9,19 @@ tape("interpolateCubehelix(a, b) converts a and b to Cubehelix colors", function test.end(); }); -tape("interpolateCubehelix(a, b) interpolates in Cubehelix and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateCubehelix("steelblue", "#f00")(0.2), "#5864da"); +tape("interpolateCubehelix(a, b) interpolates in Cubehelix and returns an RGB string", function(test) { + test.equal(interpolate.interpolateCubehelix("steelblue", "#f00")(0.2), "rgb(88, 100, 218)"); + test.equal(interpolate.interpolateCubehelix("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(88, 100, 218, 0.84)"); test.end(); }); tape("interpolateCubehelix.gamma(3)(a, b) returns the expected values", function(test) { - test.equal(interpolate.interpolateCubehelix.gamma(3)("steelblue", "#f00")(0.2), "#606be4"); + test.equal(interpolate.interpolateCubehelix.gamma(3)("steelblue", "#f00")(0.2), "rgb(96, 107, 228)"); test.end(); }); tape("interpolateCubehelix.gamma(g) coerces the specified gamma to a number", function(test) { - test.equal(interpolate.interpolateCubehelix.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "#606be4"); + test.equal(interpolate.interpolateCubehelix.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "rgb(96, 107, 228)"); test.end(); }); @@ -38,45 +39,45 @@ tape("interpolateCubehelix(a, b) is equivalent to interpolateCubehelix.gamma(1)( tape("interpolateCubehelix(a, b) uses the shortest path when interpolating hue difference greater than 180°", function(test) { var i = interpolate.interpolateCubehelix("purple", "orange"); - test.equal(i(0.0), "#800080"); - test.equal(i(0.2), "#d0017f"); - test.equal(i(0.4), "#ff115d"); - test.equal(i(0.6), "#ff342b"); - test.equal(i(0.8), "#ff6905"); - test.equal(i(1.0), "#ffa500"); + test.equal(i(0.0), "rgb(128, 0, 128)"); + test.equal(i(0.2), "rgb(208, 1, 127)"); + test.equal(i(0.4), "rgb(255, 17, 93)"); + test.equal(i(0.6), "rgb(255, 52, 43)"); + test.equal(i(0.8), "rgb(255, 105, 5)"); + test.equal(i(1.0), "rgb(255, 165, 0)"); test.end(); }); tape("interpolateCubehelix(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix("#f60", color.cubehelix(NaN, NaN, 0))(0.5), "#a22900"); - test.equal(interpolate.interpolateCubehelix("#6f0", color.cubehelix(NaN, NaN, 0))(0.5), "#03ad00"); + test.equal(interpolate.interpolateCubehelix("#f60", color.cubehelix(NaN, NaN, 0))(0.5), "rgb(162, 41, 0)"); + test.equal(interpolate.interpolateCubehelix("#6f0", color.cubehelix(NaN, NaN, 0))(0.5), "rgb(3, 173, 0)"); test.end(); }); tape("interpolateCubehelix(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#f60")(0.5), "#a22900"); - test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#6f0")(0.5), "#03ad00"); + test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#f60")(0.5), "rgb(162, 41, 0)"); + test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#6f0")(0.5), "rgb(3, 173, 0)"); test.end(); }); tape("interpolateCubehelix(a, b) uses a’s chroma when b’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix("#ccc", color.cubehelix(NaN, NaN, 0))(0.5), "#666666"); - test.equal(interpolate.interpolateCubehelix("#f00", color.cubehelix(NaN, NaN, 0))(0.5), "#930000"); + test.equal(interpolate.interpolateCubehelix("#ccc", color.cubehelix(NaN, NaN, 0))(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateCubehelix("#f00", color.cubehelix(NaN, NaN, 0))(0.5), "rgb(147, 0, 0)"); test.end(); }); tape("interpolateCubehelix(a, b) uses b’s chroma when a’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#ccc")(0.5), "#666666"); - test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#f00")(0.5), "#930000"); + test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#ccc")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateCubehelix(color.cubehelix(NaN, NaN, 0), "#f00")(0.5), "rgb(147, 0, 0)"); test.end(); }); tape("interpolateCubehelix(a, b) uses b’s luminance when a’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix(null, color.cubehelix(20, 1.5, 0.5))(0.5), "#f85d00"); + test.equal(interpolate.interpolateCubehelix(null, color.cubehelix(20, 1.5, 0.5))(0.5), "rgb(248, 93, 0)"); test.end(); }); -tape("interpolateCubehelix(a, b) uses zero when b’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateCubehelix(color.cubehelix(20, 1.5, 0.5), null)(0.5), "#9a2600"); +tape("interpolateCubehelix(a, b) uses a’s luminance when b’s luminance is undefined", function(test) { + test.equal(interpolate.interpolateCubehelix(color.cubehelix(20, 1.5, 0.5), null)(0.5), "rgb(248, 93, 0)"); test.end(); }); diff --git a/test/cubehelixLong-test.js b/test/cubehelixLong-test.js index 0ae1c8e..be27ca4 100644 --- a/test/cubehelixLong-test.js +++ b/test/cubehelixLong-test.js @@ -9,18 +9,19 @@ tape("interpolateCubehelixLong(a, b) converts a and b to Cubehelix colors", func test.end(); }); -tape("interpolateCubehelixLong(a, b) interpolates in Cubehelix and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateCubehelixLong("steelblue", "#f00")(0.2), "#5864da"); +tape("interpolateCubehelixLong(a, b) interpolates in Cubehelix and returns an RGB string", function(test) { + test.equal(interpolate.interpolateCubehelixLong("steelblue", "#f00")(0.2), "rgb(88, 100, 218)"); + test.equal(interpolate.interpolateCubehelixLong("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(88, 100, 218, 0.84)"); test.end(); }); tape("interpolateCubehelixLong.gamma(3)(a, b) returns the expected values", function(test) { - test.equal(interpolate.interpolateCubehelixLong.gamma(3)("steelblue", "#f00")(0.2), "#606be4"); + test.equal(interpolate.interpolateCubehelixLong.gamma(3)("steelblue", "#f00")(0.2), "rgb(96, 107, 228)"); test.end(); }); tape("interpolateCubehelixLong.gamma(g) coerces the specified gamma to a number", function(test) { - test.equal(interpolate.interpolateCubehelixLong.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "#606be4"); + test.equal(interpolate.interpolateCubehelixLong.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "rgb(96, 107, 228)"); test.end(); }); @@ -37,45 +38,45 @@ tape("interpolateCubehelixLong(a, b) is equivalent to interpolateCubehelixLong.g }); tape("interpolateCubehelixLong(a, b) uses the longest path when interpolating hue difference greater than 180°", function(test) { var i = interpolate.interpolateCubehelixLong("purple", "orange"); - test.equal(i(0.0), "#800080"); - test.equal(i(0.2), "#3f36ea"); - test.equal(i(0.4), "#0097d9"); - test.equal(i(0.6), "#00df53"); - test.equal(i(0.8), "#4fdb00"); - test.equal(i(1.0), "#ffa500"); + test.equal(i(0.0), "rgb(128, 0, 128)"); + test.equal(i(0.2), "rgb(63, 54, 234)"); + test.equal(i(0.4), "rgb(0, 151, 217)"); + test.equal(i(0.6), "rgb(0, 223, 83)"); + test.equal(i(0.8), "rgb(79, 219, 0)"); + test.equal(i(1.0), "rgb(255, 165, 0)"); test.end(); }); tape("interpolateCubehelixLong(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong("#f60", color.hcl(NaN, NaN, 0))(0.5), "#a22900"); - test.equal(interpolate.interpolateCubehelixLong("#6f0", color.hcl(NaN, NaN, 0))(0.5), "#03ad00"); + test.equal(interpolate.interpolateCubehelixLong("#f60", color.hcl(NaN, NaN, 0))(0.5), "rgb(162, 41, 0)"); + test.equal(interpolate.interpolateCubehelixLong("#6f0", color.hcl(NaN, NaN, 0))(0.5), "rgb(3, 173, 0)"); test.end(); }); tape("interpolateCubehelixLong(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#f60")(0.5), "#a22900"); - test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#6f0")(0.5), "#03ad00"); + test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#f60")(0.5), "rgb(162, 41, 0)"); + test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#6f0")(0.5), "rgb(3, 173, 0)"); test.end(); }); tape("interpolateCubehelixLong(a, b) uses a’s chroma when b’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong("#ccc", color.hcl(NaN, NaN, 0))(0.5), "#666666"); - test.equal(interpolate.interpolateCubehelixLong("#f00", color.hcl(NaN, NaN, 0))(0.5), "#930000"); + test.equal(interpolate.interpolateCubehelixLong("#ccc", color.hcl(NaN, NaN, 0))(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateCubehelixLong("#f00", color.hcl(NaN, NaN, 0))(0.5), "rgb(147, 0, 0)"); test.end(); }); tape("interpolateCubehelixLong(a, b) uses b’s chroma when a’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#ccc")(0.5), "#666666"); - test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#f00")(0.5), "#930000"); + test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#ccc")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateCubehelixLong(color.hcl(NaN, NaN, 0), "#f00")(0.5), "rgb(147, 0, 0)"); test.end(); }); tape("interpolateCubehelixLong(a, b) uses b’s luminance when a’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong(null, color.cubehelix(20, 1.5, 0.5))(0.5), "#f85d00"); + test.equal(interpolate.interpolateCubehelixLong(null, color.cubehelix(20, 1.5, 0.5))(0.5), "rgb(248, 93, 0)"); test.end(); }); -tape("interpolateCubehelixLong(a, b) uses zero when b’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateCubehelixLong(color.cubehelix(20, 1.5, 0.5), null)(0.5), "#9a2600"); +tape("interpolateCubehelixLong(a, b) uses a’s luminance when b’s luminance is undefined", function(test) { + test.equal(interpolate.interpolateCubehelixLong(color.cubehelix(20, 1.5, 0.5), null)(0.5), "rgb(248, 93, 0)"); test.end(); }); diff --git a/test/hcl-test.js b/test/hcl-test.js index b267026..4da4499 100644 --- a/test/hcl-test.js +++ b/test/hcl-test.js @@ -9,85 +9,86 @@ tape("interpolateHcl(a, b) converts a and b to HCL colors", function(test) { test.end(); }); -tape("interpolateHcl(a, b) interpolates in HCL and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateHcl("steelblue", "#f00")(.2), "#6978c9"); +tape("interpolateHcl(a, b) interpolates in HCL and returns an RGB string", function(test) { + test.equal(interpolate.interpolateHcl("steelblue", "#f00")(0.2), "rgb(105, 120, 201)"); + test.equal(interpolate.interpolateHcl("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(105, 120, 201, 0.84)"); test.end(); }); tape("interpolateHcl(a, b) uses the shortest path when interpolating hue difference greater than 180°", function(test) { var i = interpolate.interpolateHcl(color.hcl(10, 50, 50), color.hcl(350, 50, 50)); - test.equal(i(0.0), "#c44f6a"); - test.equal(i(0.2), "#c44f70"); - test.equal(i(0.4), "#c34f76"); - test.equal(i(0.6), "#c14f7c"); - test.equal(i(0.8), "#bf5081"); - test.equal(i(1.0), "#bd5187"); + test.equal(i(0.0), "rgb(196, 79, 106)"); + test.equal(i(0.2), "rgb(196, 79, 112)"); + test.equal(i(0.4), "rgb(195, 79, 118)"); + test.equal(i(0.6), "rgb(193, 79, 124)"); + test.equal(i(0.8), "rgb(191, 80, 129)"); + test.equal(i(1.0), "rgb(189, 81, 135)"); test.end(); }); tape("interpolateHcl(a, b) uses the shortest path when interpolating hue difference greater than 360°", function(test) { var i = interpolate.interpolateHcl(color.hcl(10, 50, 50), color.hcl(380, 50, 50)); - test.equal(i(0.0), "#c44f6a"); - test.equal(i(0.2), "#c44f68"); - test.equal(i(0.4), "#c55065"); - test.equal(i(0.6), "#c45062"); - test.equal(i(0.8), "#c4505f"); - test.equal(i(1.0), "#c4515c"); + test.equal(i(0.0), "rgb(196, 79, 106)"); + test.equal(i(0.2), "rgb(196, 79, 104)"); + test.equal(i(0.4), "rgb(197, 80, 101)"); + test.equal(i(0.6), "rgb(196, 80, 98)"); + test.equal(i(0.8), "rgb(196, 80, 95)"); + test.equal(i(1.0), "rgb(196, 81, 92)"); test.end(); }); tape("interpolateHcl(a, b) uses the shortest path when interpolating hue difference greater than 540°", function(test) { var i = interpolate.interpolateHcl(color.hcl(10, 50, 50), color.hcl(710, 50, 50)); - test.equal(i(0.0), "#c44f6a"); - test.equal(i(0.2), "#c44f70"); - test.equal(i(0.4), "#c34f76"); - test.equal(i(0.6), "#c14f7c"); - test.equal(i(0.8), "#bf5081"); - test.equal(i(1.0), "#bd5187"); + test.equal(i(0.0), "rgb(196, 79, 106)"); + test.equal(i(0.2), "rgb(196, 79, 112)"); + test.equal(i(0.4), "rgb(195, 79, 118)"); + test.equal(i(0.6), "rgb(193, 79, 124)"); + test.equal(i(0.8), "rgb(191, 80, 129)"); + test.equal(i(1.0), "rgb(189, 81, 135)"); test.end(); }); tape("interpolateHcl(a, b) uses the shortest path when interpolating hue difference greater than 720°", function(test) { var i = interpolate.interpolateHcl(color.hcl(10, 50, 50), color.hcl(740, 50, 50)); - test.equal(i(0.0), "#c44f6a"); - test.equal(i(0.2), "#c44f68"); - test.equal(i(0.4), "#c55065"); - test.equal(i(0.6), "#c45062"); - test.equal(i(0.8), "#c4505f"); - test.equal(i(1.0), "#c4515c"); + test.equal(i(0.0), "rgb(196, 79, 106)"); + test.equal(i(0.2), "rgb(196, 79, 104)"); + test.equal(i(0.4), "rgb(197, 80, 101)"); + test.equal(i(0.6), "rgb(196, 80, 98)"); + test.equal(i(0.8), "rgb(196, 80, 95)"); + test.equal(i(1.0), "rgb(196, 81, 92)"); test.end(); }); tape("interpolateHcl(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHcl("#f60", color.hcl(NaN, NaN, 0))(.5), "#9b0000"); - test.equal(interpolate.interpolateHcl("#6f0", color.hcl(NaN, NaN, 0))(.5), "#008100"); + test.equal(interpolate.interpolateHcl("#f60", color.hcl(NaN, NaN, 0))(0.5), "rgb(155, 0, 0)"); + test.equal(interpolate.interpolateHcl("#6f0", color.hcl(NaN, NaN, 0))(0.5), "rgb(0, 129, 0)"); test.end(); }); tape("interpolateHcl(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#f60")(.5), "#9b0000"); - test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#6f0")(.5), "#008100"); + test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#f60")(0.5), "rgb(155, 0, 0)"); + test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#6f0")(0.5), "rgb(0, 129, 0)"); test.end(); }); tape("interpolateHcl(a, b) uses a’s chroma when b’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateHcl("#ccc", color.hcl(NaN, NaN, 0))(.5), "#616161"); - test.equal(interpolate.interpolateHcl("#f00", color.hcl(NaN, NaN, 0))(.5), "#a60000"); + test.equal(interpolate.interpolateHcl("#ccc", color.hcl(NaN, NaN, 0))(0.5), "rgb(97, 97, 97)"); + test.equal(interpolate.interpolateHcl("#f00", color.hcl(NaN, NaN, 0))(0.5), "rgb(166, 0, 0)"); test.end(); }); tape("interpolateHcl(a, b) uses b’s chroma when a’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#ccc")(.5), "#616161"); - test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#f00")(.5), "#a60000"); + test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#ccc")(0.5), "rgb(97, 97, 97)"); + test.equal(interpolate.interpolateHcl(color.hcl(NaN, NaN, 0), "#f00")(0.5), "rgb(166, 0, 0)"); test.end(); }); tape("interpolateHcl(a, b) uses b’s luminance when a’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateHcl(null, color.hcl(20, 80, 50))(0.5), "#ea134d"); + test.equal(interpolate.interpolateHcl(null, color.hcl(20, 80, 50))(0.5), "rgb(234, 19, 77)"); test.end(); }); -tape("interpolateHcl(a, b) uses zero when b’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateHcl(color.hcl(20, 80, 50), null)(0.5), "#990019"); +tape("interpolateHcl(a, b) uses a’s luminance when b’s luminance is undefined", function(test) { + test.equal(interpolate.interpolateHcl(color.hcl(20, 80, 50), null)(0.5), "rgb(234, 19, 77)"); test.end(); }); diff --git a/test/hclLong-test.js b/test/hclLong-test.js index f190ef2..ba6310e 100644 --- a/test/hclLong-test.js +++ b/test/hclLong-test.js @@ -9,52 +9,53 @@ tape("interpolateHclLong(a, b) converts a and b to HCL colors", function(test) { test.end(); }); -tape("interpolateHclLong(a, b) interpolates in HCL and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateHclLong("steelblue", "#f00")(.2), "#0090ae"); +tape("interpolateHclLong(a, b) interpolates in HCL and returns an RGB string", function(test) { + test.equal(interpolate.interpolateHclLong("steelblue", "#f00")(0.2), "rgb(0, 144, 174)"); + test.equal(interpolate.interpolateHclLong("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(0, 144, 174, 0.84)"); test.end(); }); tape("interpolateHclLong(a, b) does not use the shortest path when interpolating hue", function(test) { var i = interpolate.interpolateHclLong(color.hcl(10, 50, 50), color.hcl(350, 50, 50)); - test.equal(i(0.0), "#c44f6a"); - test.equal(i(0.2), "#9c6f1d"); - test.equal(i(0.4), "#2e8745"); - test.equal(i(0.6), "#008aa5"); - test.equal(i(0.8), "#4376ca"); - test.equal(i(1.0), "#bd5187"); + test.equal(i(0.0), "rgb(196, 79, 106)"); + test.equal(i(0.2), "rgb(156, 111, 29)"); + test.equal(i(0.4), "rgb(46, 135, 69)"); + test.equal(i(0.6), "rgb(0, 138, 165)"); + test.equal(i(0.8), "rgb(67, 118, 202)"); + test.equal(i(1.0), "rgb(189, 81, 135)"); test.end(); }); tape("interpolateHclLong(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHclLong("#f60", color.hcl(NaN, NaN, 0))(.5), "#9b0000"); - test.equal(interpolate.interpolateHclLong("#6f0", color.hcl(NaN, NaN, 0))(.5), "#008100"); + test.equal(interpolate.interpolateHclLong("#f60", color.hcl(NaN, NaN, 0))(0.5), "rgb(155, 0, 0)"); + test.equal(interpolate.interpolateHclLong("#6f0", color.hcl(NaN, NaN, 0))(0.5), "rgb(0, 129, 0)"); test.end(); }); tape("interpolateHclLong(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#f60")(.5), "#9b0000"); - test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#6f0")(.5), "#008100"); + test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#f60")(0.5), "rgb(155, 0, 0)"); + test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#6f0")(0.5), "rgb(0, 129, 0)"); test.end(); }); tape("interpolateHclLong(a, b) uses a’s chroma when b’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateHclLong("#ccc", color.hcl(NaN, NaN, 0))(.5), "#616161"); - test.equal(interpolate.interpolateHclLong("#f00", color.hcl(NaN, NaN, 0))(.5), "#a60000"); + test.equal(interpolate.interpolateHclLong("#ccc", color.hcl(NaN, NaN, 0))(0.5), "rgb(97, 97, 97)"); + test.equal(interpolate.interpolateHclLong("#f00", color.hcl(NaN, NaN, 0))(0.5), "rgb(166, 0, 0)"); test.end(); }); tape("interpolateHclLong(a, b) uses b’s chroma when a’s chroma is undefined", function(test) { - test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#ccc")(.5), "#616161"); - test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#f00")(.5), "#a60000"); + test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#ccc")(0.5), "rgb(97, 97, 97)"); + test.equal(interpolate.interpolateHclLong(color.hcl(NaN, NaN, 0), "#f00")(0.5), "rgb(166, 0, 0)"); test.end(); }); tape("interpolateHclLong(a, b) uses b’s luminance when a’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateHclLong(null, color.hcl(20, 80, 50))(0.5), "#ea134d"); + test.equal(interpolate.interpolateHclLong(null, color.hcl(20, 80, 50))(0.5), "rgb(234, 19, 77)"); test.end(); }); -tape("interpolateHclLong(a, b) uses zero when b’s luminance is undefined", function(test) { - test.equal(interpolate.interpolateHclLong(color.hcl(20, 80, 50), null)(0.5), "#990019"); +tape("interpolateHclLong(a, b) uses a’s luminance when b’s luminance is undefined", function(test) { + test.equal(interpolate.interpolateHclLong(color.hcl(20, 80, 50), null)(0.5), "rgb(234, 19, 77)"); test.end(); }); diff --git a/test/hsl-test.js b/test/hsl-test.js index db04ead..c6a07b6 100644 --- a/test/hsl-test.js +++ b/test/hsl-test.js @@ -9,52 +9,53 @@ tape("interpolateHsl(a, b) converts a and b to HSL colors", function(test) { test.end(); }); -tape("interpolateHsl(a, b) interpolates in HSL and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateHsl("steelblue", "#f00")(.2), "#383dc3"); +tape("interpolateHsl(a, b) interpolates in HSL and returns an RGB string", function(test) { + test.equal(interpolate.interpolateHsl("steelblue", "#f00")(0.2), "rgb(56, 61, 195)"); + test.equal(interpolate.interpolateHsl("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(56, 61, 195, 0.84)"); test.end(); }); tape("interpolateHsl(a, b) uses the shortest path when interpolating hue", function(test) { var i = interpolate.interpolateHsl("hsl(10,50%,50%)", "hsl(350,50%,50%)"); - test.equal(i(0.0), "#bf5540"); - test.equal(i(0.2), "#bf4d40"); - test.equal(i(0.4), "#bf4440"); - test.equal(i(0.6), "#bf4044"); - test.equal(i(0.8), "#bf404d"); - test.equal(i(1.0), "#bf4055"); + test.equal(i(0.0), "rgb(191, 85, 64)"); + test.equal(i(0.2), "rgb(191, 77, 64)"); + test.equal(i(0.4), "rgb(191, 68, 64)"); + test.equal(i(0.6), "rgb(191, 64, 68)"); + test.equal(i(0.8), "rgb(191, 64, 77)"); + test.equal(i(1.0), "rgb(191, 64, 85)"); test.end(); }); tape("interpolateHsl(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHsl("#f60", "#000")(.5), "#803300"); - test.equal(interpolate.interpolateHsl("#6f0", "#fff")(.5), "#b3ff80"); + test.equal(interpolate.interpolateHsl("#f60", "#000")(0.5), "rgb(128, 51, 0)"); + test.equal(interpolate.interpolateHsl("#6f0", "#fff")(0.5), "rgb(179, 255, 128)"); test.end(); }); tape("interpolateHsl(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHsl("#000", "#f60")(.5), "#803300"); - test.equal(interpolate.interpolateHsl("#fff", "#6f0")(.5), "#b3ff80"); + test.equal(interpolate.interpolateHsl("#000", "#f60")(0.5), "rgb(128, 51, 0)"); + test.equal(interpolate.interpolateHsl("#fff", "#6f0")(0.5), "rgb(179, 255, 128)"); test.end(); }); tape("interpolateHsl(a, b) uses a’s saturation when b’s saturation is undefined", function(test) { - test.equal(interpolate.interpolateHsl("#ccc", "#000")(.5), "#666666"); - test.equal(interpolate.interpolateHsl("#f00", "#000")(.5), "#800000"); + test.equal(interpolate.interpolateHsl("#ccc", "#000")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateHsl("#f00", "#000")(0.5), "rgb(128, 0, 0)"); test.end(); }); tape("interpolateHsl(a, b) uses b’s saturation when a’s saturation is undefined", function(test) { - test.equal(interpolate.interpolateHsl("#000", "#ccc")(.5), "#666666"); - test.equal(interpolate.interpolateHsl("#000", "#f00")(.5), "#800000"); + test.equal(interpolate.interpolateHsl("#000", "#ccc")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateHsl("#000", "#f00")(0.5), "rgb(128, 0, 0)"); test.end(); }); tape("interpolateHsl(a, b) uses b’s lightness when a’s lightness is undefined", function(test) { - test.equal(interpolate.interpolateHsl(null, color.hsl(20, 1.0, 0.5))(0.5), "#ff5500"); + test.equal(interpolate.interpolateHsl(null, color.hsl(20, 1.0, 0.5))(0.5), "rgb(255, 85, 0)"); test.end(); }); -tape("interpolateHsl(a, b) uses zero when b’s lightness is undefined", function(test) { - test.equal(interpolate.interpolateHsl(color.hsl(20, 1.0, 0.5), null)(0.5), "#802b00"); +tape("interpolateHsl(a, b) uses a’s lightness when b’s lightness is undefined", function(test) { + test.equal(interpolate.interpolateHsl(color.hsl(20, 1.0, 0.5), null)(0.5), "rgb(255, 85, 0)"); test.end(); }); diff --git a/test/hslLong-test.js b/test/hslLong-test.js index 281ed6a..c72b031 100644 --- a/test/hslLong-test.js +++ b/test/hslLong-test.js @@ -9,52 +9,53 @@ tape("interpolateHslLong(a, b) converts a and b to HSL colors", function(test) { test.end(); }); -tape("interpolateHslLong(a, b) interpolates in HSL and returns an RGB hexadecimal string", function(test) { - test.equal(interpolate.interpolateHslLong("steelblue", "#f00")(.2), "#38c3a2"); +tape("interpolateHslLong(a, b) interpolates in HSL and returns an RGB string", function(test) { + test.equal(interpolate.interpolateHslLong("steelblue", "#f00")(0.2), "rgb(56, 195, 162)"); + test.equal(interpolate.interpolateHslLong("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(56, 195, 162, 0.84)"); test.end(); }); tape("interpolateHslLong(a, b) does not use the shortest path when interpolating hue", function(test) { var i = interpolate.interpolateHslLong("hsl(10,50%,50%)", "hsl(350,50%,50%)"); - test.equal(i(0.0), "#bf5540"); - test.equal(i(0.2), "#99bf40"); - test.equal(i(0.4), "#40bf77"); - test.equal(i(0.6), "#4077bf"); - test.equal(i(0.8), "#9940bf"); - test.equal(i(1.0), "#bf4055"); + test.equal(i(0.0), "rgb(191, 85, 64)"); + test.equal(i(0.2), "rgb(153, 191, 64)"); + test.equal(i(0.4), "rgb(64, 191, 119)"); + test.equal(i(0.6), "rgb(64, 119, 191)"); + test.equal(i(0.8), "rgb(153, 64, 191)"); + test.equal(i(1.0), "rgb(191, 64, 85)"); test.end(); }); tape("interpolateHslLong(a, b) uses a’s hue when b’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHslLong("#f60", "#000")(.5), "#803300"); - test.equal(interpolate.interpolateHslLong("#6f0", "#fff")(.5), "#b3ff80"); + test.equal(interpolate.interpolateHslLong("#f60", "#000")(0.5), "rgb(128, 51, 0)"); + test.equal(interpolate.interpolateHslLong("#6f0", "#fff")(0.5), "rgb(179, 255, 128)"); test.end(); }); tape("interpolateHslLong(a, b) uses b’s hue when a’s hue is undefined", function(test) { - test.equal(interpolate.interpolateHslLong("#000", "#f60")(.5), "#803300"); - test.equal(interpolate.interpolateHslLong("#fff", "#6f0")(.5), "#b3ff80"); + test.equal(interpolate.interpolateHslLong("#000", "#f60")(0.5), "rgb(128, 51, 0)"); + test.equal(interpolate.interpolateHslLong("#fff", "#6f0")(0.5), "rgb(179, 255, 128)"); test.end(); }); tape("interpolateHslLong(a, b) uses a’s saturation when b’s saturation is undefined", function(test) { - test.equal(interpolate.interpolateHslLong("#ccc", "#000")(.5), "#666666"); - test.equal(interpolate.interpolateHslLong("#f00", "#000")(.5), "#800000"); + test.equal(interpolate.interpolateHslLong("#ccc", "#000")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateHslLong("#f00", "#000")(0.5), "rgb(128, 0, 0)"); test.end(); }); tape("interpolateHslLong(a, b) uses b’s saturation when a’s saturation is undefined", function(test) { - test.equal(interpolate.interpolateHslLong("#000", "#ccc")(.5), "#666666"); - test.equal(interpolate.interpolateHslLong("#000", "#f00")(.5), "#800000"); + test.equal(interpolate.interpolateHslLong("#000", "#ccc")(0.5), "rgb(102, 102, 102)"); + test.equal(interpolate.interpolateHslLong("#000", "#f00")(0.5), "rgb(128, 0, 0)"); test.end(); }); tape("interpolateHslLong(a, b) uses b’s lightness when a’s lightness is undefined", function(test) { - test.equal(interpolate.interpolateHslLong(null, color.hsl(20, 1.0, 0.5))(0.5), "#ff5500"); + test.equal(interpolate.interpolateHslLong(null, color.hsl(20, 1.0, 0.5))(0.5), "rgb(255, 85, 0)"); test.end(); }); -tape("interpolateHslLong(a, b) uses zero when b’s lightness is undefined", function(test) { - test.equal(interpolate.interpolateHslLong(color.hsl(20, 1.0, 0.5), null)(0.5), "#802b00"); +tape("interpolateHslLong(a, b) uses a’s lightness when b’s lightness is undefined", function(test) { + test.equal(interpolate.interpolateHslLong(color.hsl(20, 1.0, 0.5), null)(0.5), "rgb(255, 85, 0)"); test.end(); }); diff --git a/test/lab-test.js b/test/lab-test.js index 7752288..c035f72 100644 --- a/test/lab-test.js +++ b/test/lab-test.js @@ -9,8 +9,9 @@ tape("interpolateLab(a, b) converts a and b to Lab colors", function(test) { test.end(); }); -tape("interpolateLab(a, b) interpolates in Lab and returns a hexadecimal string", function(test) { - test.equal(interpolate.interpolateLab("steelblue", "#f00")(.2), "#8a7793"); +tape("interpolateLab(a, b) interpolates in Lab and returns an RGB string", function(test) { + test.equal(interpolate.interpolateLab("steelblue", "#f00")(0.2), "rgb(138, 119, 147)"); + test.equal(interpolate.interpolateLab("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(138, 119, 147, 0.84)"); test.end(); }); @@ -22,10 +23,10 @@ tape("interpolateLab(a, b) uses b’s channel value when a’s channel value is test.end(); }); -tape("interpolateLab(a, b) uses zero when b’s channel value is undefined", function(test) { - test.equal(interpolate.interpolateLab(color.lab(20, 40, 60), null)(0.5), color.lab(10, 20, 30) + ""); - test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(NaN, 20, 40))(0.5), color.lab(30, 50, 70) + ""); - test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(20, NaN, 40))(0.5), color.lab(40, 40, 70) + ""); - test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(20, 40, NaN))(0.5), color.lab(40, 60, 50) + ""); +tape("interpolateLab(a, b) uses a’s channel value when b’s channel value is undefined", function(test) { + test.equal(interpolate.interpolateLab(color.lab(20, 40, 60), null)(0.5), color.lab(20, 40, 60) + ""); + test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(NaN, 20, 40))(0.5), color.lab(60, 50, 70) + ""); + test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(20, NaN, 40))(0.5), color.lab(40, 80, 70) + ""); + test.equal(interpolate.interpolateLab(color.lab(60, 80, 100), color.lab(20, 40, NaN))(0.5), color.lab(40, 60, 100) + ""); test.end(); }); diff --git a/test/object-test.js b/test/object-test.js index 3f0d61a..fcf98b1 100644 --- a/test/object-test.js +++ b/test/object-test.js @@ -2,45 +2,45 @@ var tape = require("tape"), interpolate = require("../"); tape("interpolateObject(a, b) interpolates defined properties in a and b", function(test) { - test.deepEqual(interpolate.interpolateObject({a: 2, b: 12}, {a: 4, b: 24})(.5), {a: 3, b: 18}); + test.deepEqual(interpolate.interpolateObject({a: 2, b: 12}, {a: 4, b: 24})(0.5), {a: 3, b: 18}); test.end(); }); tape("interpolateObject(a, b) interpolates inherited properties in a and b", function(test) { function a(a) { this.a = a; } a.prototype.b = 12; - test.deepEqual(interpolate.interpolateObject(new a(2), {a: 4, b: 12})(.5), {a: 3, b: 12}); - test.deepEqual(interpolate.interpolateObject({a: 2, b: 12}, new a(4))(.5), {a: 3, b: 12}); - test.deepEqual(interpolate.interpolateObject(new a(4), new a(2))(.5), {a: 3, b: 12}); + test.deepEqual(interpolate.interpolateObject(new a(2), {a: 4, b: 12})(0.5), {a: 3, b: 12}); + test.deepEqual(interpolate.interpolateObject({a: 2, b: 12}, new a(4))(0.5), {a: 3, b: 12}); + test.deepEqual(interpolate.interpolateObject(new a(4), new a(2))(0.5), {a: 3, b: 12}); test.end(); }); tape("interpolateObject(a, b) interpolates color properties as rgb", function(test) { - test.deepEqual(interpolate.interpolateObject({background: "red"}, {background: "green"})(.5), {background: "#804000"}); - test.deepEqual(interpolate.interpolateObject({fill: "red"}, {fill: "green"})(.5), {fill: "#804000"}); - test.deepEqual(interpolate.interpolateObject({stroke: "red"}, {stroke: "green"})(.5), {stroke: "#804000"}); - test.deepEqual(interpolate.interpolateObject({color: "red"}, {color: "green"})(.5), {color: "#804000"}); + test.deepEqual(interpolate.interpolateObject({background: "red"}, {background: "green"})(0.5), {background: "rgb(128, 64, 0)"}); + test.deepEqual(interpolate.interpolateObject({fill: "red"}, {fill: "green"})(0.5), {fill: "rgb(128, 64, 0)"}); + test.deepEqual(interpolate.interpolateObject({stroke: "red"}, {stroke: "green"})(0.5), {stroke: "rgb(128, 64, 0)"}); + test.deepEqual(interpolate.interpolateObject({color: "red"}, {color: "green"})(0.5), {color: "rgb(128, 64, 0)"}); test.end(); }); tape("interpolateObject(a, b) interpolates nested objects and arrays", function(test) { - test.deepEqual(interpolate.interpolateObject({foo: [2, 12]}, {foo: [4, 24]})(.5), {foo: [3, 18]}); - test.deepEqual(interpolate.interpolateObject({foo: {bar: [2, 12]}}, {foo: {bar: [4, 24]}})(.5), {foo: {bar: [3, 18]}}); + test.deepEqual(interpolate.interpolateObject({foo: [2, 12]}, {foo: [4, 24]})(0.5), {foo: [3, 18]}); + test.deepEqual(interpolate.interpolateObject({foo: {bar: [2, 12]}}, {foo: {bar: [4, 24]}})(0.5), {foo: {bar: [3, 18]}}); test.end(); }); tape("interpolateObject(a, b) merges non-shared properties", function(test) { - test.deepEqual(interpolate.interpolateObject({foo: 2}, {foo: 4, bar: 12})(.5), {foo: 3, bar: 12}); - test.deepEqual(interpolate.interpolateObject({foo: 2, bar: 12}, {foo: 4})(.5), {foo: 3, bar: 12}); + test.deepEqual(interpolate.interpolateObject({foo: 2}, {foo: 4, bar: 12})(0.5), {foo: 3, bar: 12}); + test.deepEqual(interpolate.interpolateObject({foo: 2, bar: 12}, {foo: 4})(0.5), {foo: 3, bar: 12}); test.end(); }); tape("interpolateObject(a, b) treats undefined as an empty object", function(test) { - test.deepEqual(interpolate.interpolateObject(NaN, {foo: 2})(.5), {foo: 2}); - test.deepEqual(interpolate.interpolateObject({foo: 2}, undefined)(.5), {foo: 2}); - test.deepEqual(interpolate.interpolateObject(undefined, {foo: 2})(.5), {foo: 2}); - test.deepEqual(interpolate.interpolateObject({foo: 2}, null)(.5), {foo: 2}); - test.deepEqual(interpolate.interpolateObject(null, {foo: 2})(.5), {foo: 2}); - test.deepEqual(interpolate.interpolateObject(null, NaN)(.5), {}); + test.deepEqual(interpolate.interpolateObject(NaN, {foo: 2})(0.5), {foo: 2}); + test.deepEqual(interpolate.interpolateObject({foo: 2}, undefined)(0.5), {foo: 2}); + test.deepEqual(interpolate.interpolateObject(undefined, {foo: 2})(0.5), {foo: 2}); + test.deepEqual(interpolate.interpolateObject({foo: 2}, null)(0.5), {foo: 2}); + test.deepEqual(interpolate.interpolateObject(null, {foo: 2})(0.5), {foo: 2}); + test.deepEqual(interpolate.interpolateObject(null, NaN)(0.5), {}); test.end(); }); diff --git a/test/rgb-test.js b/test/rgb-test.js index 8b78991..d0cf1d0 100644 --- a/test/rgb-test.js +++ b/test/rgb-test.js @@ -9,8 +9,9 @@ tape("interpolateRgb(a, b) converts a and b to RGB colors", function(test) { test.end(); }); -tape("interpolateRgb(a, b) interpolates in RGB and returns a hexadecimal string", function(test) { - test.equal(interpolate.interpolateRgb("steelblue", "#f00")(.2), "#6b6890"); +tape("interpolateRgb(a, b) interpolates in RGB and returns an RGB string", function(test) { + test.equal(interpolate.interpolateRgb("steelblue", "#f00")(0.2), "rgb(107, 104, 144)"); + test.equal(interpolate.interpolateRgb("rgba(70, 130, 180, 1)", "rgba(255, 0, 0, 0.2)")(0.2), "rgba(107, 104, 144, 0.84)"); test.end(); }); @@ -22,21 +23,21 @@ tape("interpolateRgb(a, b) uses b’s channel value when a’s channel value is test.end(); }); -tape("interpolateRgb(a, b) uses zero when b’s channel value is undefined", function(test) { - test.equal(interpolate.interpolateRgb(color.rgb(20, 40, 60), null)(0.5), color.rgb(10, 20, 30) + ""); - test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(NaN, 20, 40))(0.5), color.rgb(30, 50, 70) + ""); - test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(20, NaN, 40))(0.5), color.rgb(40, 40, 70) + ""); - test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(20, 40, NaN))(0.5), color.rgb(40, 60, 50) + ""); +tape("interpolateRgb(a, b) uses a’s channel value when b’s channel value is undefined", function(test) { + test.equal(interpolate.interpolateRgb(color.rgb(20, 40, 60), null)(0.5), color.rgb(20, 40, 60) + ""); + test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(NaN, 20, 40))(0.5), color.rgb(60, 50, 70) + ""); + test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(20, NaN, 40))(0.5), color.rgb(40, 80, 70) + ""); + test.equal(interpolate.interpolateRgb(color.rgb(60, 80, 100), color.rgb(20, 40, NaN))(0.5), color.rgb(40, 60, 100) + ""); test.end(); }); tape("interpolateRgb.gamma(3)(a, b) returns the expected values", function(test) { - test.equal(interpolate.interpolateRgb.gamma(3)("steelblue", "#f00")(0.2), "#9979a7"); + test.equal(interpolate.interpolateRgb.gamma(3)("steelblue", "#f00")(0.2), "rgb(153, 121, 167)"); test.end(); }); tape("interpolateRgb.gamma(g) coerces the specified gamma to a number", function(test) { - test.equal(interpolate.interpolateRgb.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "#9979a7"); + test.equal(interpolate.interpolateRgb.gamma({valueOf: function() { return 3; }})("steelblue", "#f00")(0.2), "rgb(153, 121, 167)"); test.end(); }); diff --git a/test/string-test.js b/test/string-test.js index 65a0e7d..b8abb23 100644 --- a/test/string-test.js +++ b/test/string-test.js @@ -2,60 +2,60 @@ var tape = require("tape"), interpolate = require("../"); tape("interpolateString(a, b) interpolates matching numbers in a and b", function(test) { - test.equal(interpolate.interpolateString(" 10/20 30", "50/10 100 ")(.2), "18/18 44 "); - test.equal(interpolate.interpolateString(" 10/20 30", "50/10 100 ")(.4), "26/16 58 "); + test.equal(interpolate.interpolateString(" 10/20 30", "50/10 100 ")(0.2), "18/18 44 "); + test.equal(interpolate.interpolateString(" 10/20 30", "50/10 100 ")(0.4), "26/16 58 "); test.end(); }); tape("interpolateString(a, b) coerces a and b to strings", function(test) { - test.equal(interpolate.interpolateString({toString: function() { return "2px"; }}, {toString: function() { return "12px"; }})(.25), "4.5px"); + test.equal(interpolate.interpolateString({toString: function() { return "2px"; }}, {toString: function() { return "12px"; }})(0.25), "4.5px"); test.end(); }); tape("interpolateString(a, b) preserves non-numbers in string b", function(test) { - test.equal(interpolate.interpolateString(" 10/20 30", "50/10 foo ")(.2), "18/18 foo "); - test.equal(interpolate.interpolateString(" 10/20 30", "50/10 foo ")(.4), "26/16 foo "); + test.equal(interpolate.interpolateString(" 10/20 30", "50/10 foo ")(0.2), "18/18 foo "); + test.equal(interpolate.interpolateString(" 10/20 30", "50/10 foo ")(0.4), "26/16 foo "); test.end(); }); tape("interpolateString(a, b) preserves non-matching numbers in string b", function(test) { - test.equal(interpolate.interpolateString(" 10/20 foo", "50/10 100 ")(.2), "18/18 100 "); - test.equal(interpolate.interpolateString(" 10/20 bar", "50/10 100 ")(.4), "26/16 100 "); + test.equal(interpolate.interpolateString(" 10/20 foo", "50/10 100 ")(0.2), "18/18 100 "); + test.equal(interpolate.interpolateString(" 10/20 bar", "50/10 100 ")(0.4), "26/16 100 "); test.end(); }); tape("interpolateString(a, b) preserves equal-value numbers in both strings", function(test) { - test.equal(interpolate.interpolateString(" 10/20 100 20", "50/10 100, 20 ")(.2), "18/18 100, 20 "); - test.equal(interpolate.interpolateString(" 10/20 100 20", "50/10 100, 20 ")(.4), "26/16 100, 20 "); + test.equal(interpolate.interpolateString(" 10/20 100 20", "50/10 100, 20 ")(0.2), "18/18 100, 20 "); + test.equal(interpolate.interpolateString(" 10/20 100 20", "50/10 100, 20 ")(0.4), "26/16 100, 20 "); test.end(); }); tape("interpolateString(a, b) interpolates decimal notation correctly", function(test) { - test.equal(interpolate.interpolateString("1.", "2.")(.5), "1.5"); + test.equal(interpolate.interpolateString("1.", "2.")(0.5), "1.5"); test.end(); }); tape("interpolateString(a, b) interpolates exponent notation correctly", function(test) { - test.equal(interpolate.interpolateString("1e+3", "1e+4")(.5), "5500"); - test.equal(interpolate.interpolateString("1e-3", "1e-4")(.5), "0.00055"); - test.equal(interpolate.interpolateString("1.e-3", "1.e-4")(.5), "0.00055"); - test.equal(interpolate.interpolateString("-1.e-3", "-1.e-4")(.5), "-0.00055"); - test.equal(interpolate.interpolateString("+1.e-3", "+1.e-4")(.5), "0.00055"); - test.equal(interpolate.interpolateString(".1e-2", ".1e-3")(.5), "0.00055"); + test.equal(interpolate.interpolateString("1e+3", "1e+4")(0.5), "5500"); + test.equal(interpolate.interpolateString("1e-3", "1e-4")(0.5), "0.00055"); + test.equal(interpolate.interpolateString("1.e-3", "1.e-4")(0.5), "0.00055"); + test.equal(interpolate.interpolateString("-1.e-3", "-1.e-4")(0.5), "-0.00055"); + test.equal(interpolate.interpolateString("+1.e-3", "+1.e-4")(0.5), "0.00055"); + test.equal(interpolate.interpolateString(".1e-2", ".1e-3")(0.5), "0.00055"); test.end(); }); tape("interpolateString(a, b) with no numbers, returns the target string", function(test) { - test.equal(interpolate.interpolateString("foo", "bar")(.5), "bar"); - test.equal(interpolate.interpolateString("foo", "")(.5), ""); - test.equal(interpolate.interpolateString("", "bar")(.5), "bar"); - test.equal(interpolate.interpolateString("", "")(.5), ""); + test.equal(interpolate.interpolateString("foo", "bar")(0.5), "bar"); + test.equal(interpolate.interpolateString("foo", "")(0.5), ""); + test.equal(interpolate.interpolateString("", "bar")(0.5), "bar"); + test.equal(interpolate.interpolateString("", "")(0.5), ""); test.end(); }); tape("interpolateString(a, b) with two numerically-equivalent numbers, returns the default format", function(test) { - test.equal(interpolate.interpolateString("top: 1000px;", "top: 1e3px;")(.5), "top: 1000px;"); - test.equal(interpolate.interpolateString("top: 1e3px;", "top: 1000px;")(.5), "top: 1000px;"); + test.equal(interpolate.interpolateString("top: 1000px;", "top: 1e3px;")(0.5), "top: 1000px;"); + test.equal(interpolate.interpolateString("top: 1e3px;", "top: 1000px;")(0.5), "top: 1000px;"); test.end(); });