Commit ce6526cf authored by Mike Bostock's avatar Mike Bostock
Browse files

Color interpolators return color instances.

Fixes #1029. Previously, these interpolators returned RGB hexadecimal strings,
as the intent was for the color to be used to set attributes and styles.
However, since #333, all colors return RGB hexadecimal strings when coerced to a
string, so we can get the equivalent behavior by returning a color instance
rather than a string. Returning a color instance is also more useful in the case
where additional color manipulation (such as brighter/darker) is desired.
parent 747c523e
......@@ -4896,7 +4896,10 @@ d3 = function() {
b = d3.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) {
return "#" + d3_rgb_hex(Math.round(ar + br * t)) + d3_rgb_hex(Math.round(ag + bg * t)) + d3_rgb_hex(Math.round(ab + bb * t));
a.r = Math.round(ar + br * t);
a.g = Math.round(ag + bg * t);
a.b = Math.round(ab + bb * t);
return a;
};
}
d3.interpolateObject = d3_interpolateObject;
......@@ -5121,7 +5124,10 @@ d3 = function() {
if (isNaN(bc)) bc = 0, ac = isNaN(ac) ? b.c : ac;
if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360;
return function(t) {
return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + "";
a.h = ah + bh * t;
a.c = ac + bc * t;
a.l = al + bl * t;
return a;
};
}
d3.interpolateHsl = d3_interpolateHsl;
......@@ -5132,7 +5138,10 @@ d3 = function() {
if (isNaN(bs)) bs = 0, as = isNaN(as) ? b.s : as;
if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah; else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360;
return function(t) {
return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + "";
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * t;
return a;
};
}
d3.interpolateLab = d3_interpolateLab;
......@@ -5141,7 +5150,10 @@ d3 = function() {
b = d3.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) {
return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + "";
a.l = al + bl * t;
a.a = aa + ba * t;
a.b = ab + bb * t;
return a;
};
}
d3.interpolateRound = d3_interpolateRound;
......
This diff is collapsed.
......@@ -15,6 +15,9 @@ function d3_interpolateHcl(a, b) {
if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah;
else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; // shortest path
return function(t) {
return d3_hcl_lab(ah + bh * t, ac + bc * t, al + bl * t) + "";
a.h = ah + bh * t;
a.c = ac + bc * t;
a.l = al + bl * t;
return a;
};
}
......@@ -2,8 +2,6 @@ import "../color/hsl";
d3.interpolateHsl = d3_interpolateHsl;
// interpolates HSL space, but outputs RGB string (for compatibility)
function d3_interpolateHsl(a, b) {
a = d3.hsl(a);
b = d3.hsl(b);
......@@ -17,6 +15,9 @@ function d3_interpolateHsl(a, b) {
if (isNaN(bh)) bh = 0, ah = isNaN(ah) ? b.h : ah;
else if (bh > 180) bh -= 360; else if (bh < -180) bh += 360; // shortest path
return function(t) {
return d3_hsl_rgb(ah + bh * t, as + bs * t, al + bl * t) + "";
a.h = ah + bh * t;
a.s = as + bs * t;
a.l = al + bl * t;
return a;
};
}
......@@ -12,6 +12,9 @@ function d3_interpolateLab(a, b) {
ba = b.a - aa,
bb = b.b - ab;
return function(t) {
return d3_lab_rgb(al + bl * t, aa + ba * t, ab + bb * t) + "";
a.l = al + bl * t;
a.a = aa + ba * t;
a.b = ab + bb * t;
return a;
};
}
......@@ -12,9 +12,9 @@ function d3_interpolateRgb(a, b) {
bg = b.g - ag,
bb = b.b - ab;
return function(t) {
return "#"
+ d3_rgb_hex(Math.round(ar + br * t))
+ d3_rgb_hex(Math.round(ag + bg * t))
+ d3_rgb_hex(Math.round(ab + bb * t));
a.r = Math.round(ar + br * t);
a.g = Math.round(ag + bg * t);
a.b = Math.round(ab + bb * t);
return a;
};
}
var assert = require("assert");
var assert = require("assert"),
_ = require("../");
assert = module.exports = Object.create(assert);
......@@ -27,14 +28,62 @@ assert.domEqual = function(actual, expected, message) {
};
assert.rgbEqual = function(actual, r, g, b, message) {
if (actual.r !== r || actual.g !== g || actual.b !== b) {
assert.fail("rgb(" + actual.r + "," + actual.g + "," + actual.b + ")", "rgb(" + r + ", " + g + ", " + b + ")", message || "expected {expected}, got {actual}", "===", assert.rgbEqual);
var ar = Math.round(actual.r),
ag = Math.round(actual.g),
ab = Math.round(actual.b),
er = Math.round(r),
eg = Math.round(g),
eb = Math.round(b);
if (ar !== er || ag !== eg || ab !== eb) {
assert.fail(
"rgb(" + ar + "," + ag + "," + ab + ")",
"rgb(" + er + ", " + eg + ", " + eb + ")",
message || "expected {expected}, got {actual}", "===", assert.rgbEqual);
}
};
assert.hslEqual = function(actual, h, s, l, message) {
if (Math.abs(actual.h - h) > 1e-6 || Math.abs(actual.s - s) > 1e-6 || Math.abs(actual.l - l) > 1e-6) {
assert.fail("hsl(" + actual.h + "," + (actual.s * 100) + "%," + (actual.l * 100) + "%)", "hsl(" + h + "," + (s * 100) + "%," + (l * 100) + "%)", message || "expected {expected}, got {actual}", null, assert.hslEqual);
var ah = _.round(actual.h, 2),
as = _.round(actual.s, 2),
al = _.round(actual.l, 2),
eh = _.round(h, 2),
es = _.round(s, 2),
el = _.round(l, 2);
if ((!(isNaN(ah) && isNaN(eh)) && ah !== eh) || (!(isNaN(as) && isNaN(es)) && as !== es) || al !== el) {
assert.fail(
"hsl(" + ah + "," + as + "," + al + ")",
"hsl(" + eh + "," + es + "," + el + ")",
message || "expected {expected}, got {actual}", null, assert.hslEqual);
}
};
assert.hclEqual = function(actual, h, c, l, message) {
var ah = _.round(actual.h, 2),
ac = _.round(actual.c, 2),
al = _.round(actual.l, 2),
eh = _.round(h, 2),
ec = _.round(c, 2),
el = _.round(l, 2);
if ((!(isNaN(ah) && isNaN(eh)) && ah !== eh) || ac !== ec || al !== el) {
assert.fail(
"hcl(" + ah + "," + ac + "," + al + ")",
"hcl(" + eh + "," + ec + "," + el + ")",
message || "expected {expected}, got {actual}", null, assert.hclEqual);
}
};
assert.labEqual = function(actual, l, a, b, message) {
var al = _.round(actual.l, 2),
aa = _.round(actual.a, 2),
ab = _.round(actual.b, 2),
el = _.round(l, 2),
ea = _.round(a, 2),
eb = _.round(b, 2);
if (al !== el || aa !== ea || ab !== eb) {
assert.fail(
"lab(" + al + ", " + aa + ", " + ab + ")",
"lab(" + el + ", " + ea + ", " + eb + ")",
message || "expected {expected}, got {actual}", null, assert.labEqual);
}
};
......
......@@ -9,10 +9,10 @@ suite.addBatch({
"hcl": {
topic: load("color/hcl").expression("d3.hcl"),
"converts string channel values to numbers": function(hcl) {
assertHclEqual(hcl("50", "-4", "32"), 50, -4, 32);
assert.hclEqual(hcl("50", "-4", "32"), 50, -4, 32);
},
"converts null channel values to zero": function(hcl) {
assertHclEqual(hcl(null, null, null), 0, 0, 0);
assert.hclEqual(hcl(null, null, null), 0, 0, 0);
},
"exposes h, c and l properties": function(hcl) {
var color = hcl(50, -4, 32);
......@@ -31,49 +31,49 @@ suite.addBatch({
assert.equal(color + "", "#494f51");
},
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(hcl) {
assertHclEqual(hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
assert.hclEqual(hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
},
"parses hexadecimal format (e.g., \"#abcdef\")": function(hcl) {
assertHclEqual(hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
assert.hclEqual(hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
},
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(hcl) {
assertHclEqual(hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
assert.hclEqual(hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
},
"parses color names (e.g., \"moccasin\")": function(hcl) {
assertHclEqual(hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
assert.hclEqual(hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
},
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(hcl) {
assertHclEqual(hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
assert.hclEqual(hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
},
"can convert from RGB": function(hcl) {
assertHclEqual(hcl(_.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
assert.hclEqual(hcl(_.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
},
"can convert from HSL": function(hcl) {
assertHclEqual(hcl(hcl(20, .8, .3)), 20, 0.8, 0.3);
assert.hclEqual(hcl(hcl(20, .8, .3)), 20, 0.8, 0.3);
},
"can convert to RGB": function(hcl) {
assert.rgbEqual(hcl("steelblue").rgb(), 70, 130, 180);
},
"can derive a brighter color": function(hcl) {
assertHclEqual(hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
assertHclEqual(hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
assert.hclEqual(hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
assert.hclEqual(hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
},
"can derive a darker color": function(hcl) {
assertHclEqual(hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
assertHclEqual(hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
assert.hclEqual(hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
assert.hclEqual(hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
},
"string coercion returns RGB format": function(hcl) {
assert.strictEqual(hcl("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(hcl(hcl(60, -4, 32)) + "", "#454c51");
},
"roundtrip to HSL is idempotent": function(hcl) {
assert.hslEqual(_.hsl(hcl("steelblue")), _.hsl("steelblue"));
assert.deepEqual(_.hsl(hcl("steelblue")), _.hsl("steelblue"));
},
"roundtrip to RGB is idempotent": function(hcl) {
assert.hslEqual(_.rgb(hcl("steelblue")), _.rgb("steelblue"));
assert.deepEqual(_.rgb(hcl("steelblue")), _.rgb("steelblue"));
},
"roundtrip to Lab is idempotent": function(hcl) {
assert.hslEqual(_.lab(hcl("steelblue")), _.lab("steelblue"));
assert.deepEqual(_.lab(hcl("steelblue")), _.lab("steelblue"));
},
"h is defined for non-black grayscale colors (because of the color profile)": function(hcl) {
assert.inDelta(hcl("#ccc").h, 158.1986, 1e-3);
......@@ -110,9 +110,3 @@ suite.addBatch({
});
suite.export(module);
function assertHclEqual(actual, h, c, l, message) {
if (Math.abs(actual.h - h) > 1e-6 || Math.abs(actual.c - c) > 1e-6 || Math.abs(actual.l - l) > 1e-6) {
assert.fail("hcl(" + actual.h + ", " + actual.c + ", " + actual.l + ")", "hcl(" + h + ", " + c + ", " + l + ")", message || "expected {expected}, got {actual}", null, assertHclEqual);
}
}
......@@ -19,10 +19,7 @@ suite.addBatch({
assert.hslEqual(hsl(null, null, null), 0, 0, 0);
},
"exposes h, s and l properties": function(hsl) {
var color = hsl("hsl(180, 50%, 60%)");
assert.equal(color.h, 180);
assert.equal(color.s, .5);
assert.equal(color.l, .6);
assert.hslEqual(hsl("hsl(180, 50%, 60%)"), 180, .5, .6);
},
"changing h, s or l affects the string format": function(hsl) {
var color = hsl("hsl(180, 50%, 60%)");
......@@ -74,38 +71,38 @@ suite.addBatch({
assert.strictEqual(hsl(hsl(60, 1, .2)) + "", "#666600");
},
"h is preserved when explicitly specified, even for grayscale colors": function(hsl) {
assert.strictEqual(hsl(0, 0, 0).h, 0);
assert.strictEqual(hsl(42, 0, .5).h, 42);
assert.strictEqual(hsl(118, 0, 1).h, 118);
assert.hslEqual(hsl(0, 0, 0), 0, 0, 0);
assert.hslEqual(hsl(42, 0, .5), 42, 0, .5);
assert.hslEqual(hsl(118, 0, 1), 118, 0, 1);
},
"h is undefined when not explicitly specified for grayscale colors": function(hsl) {
assert.isNaN(hsl("#000").h);
assert.isNaN(hsl("black").h);
assert.isNaN(hsl(_.rgb("black")).h);
assert.isNaN(hsl("#ccc").h);
assert.isNaN(hsl("gray").h);
assert.isNaN(hsl(_.rgb("gray")).h);
assert.isNaN(hsl("#fff").h);
assert.isNaN(hsl("white").h);
assert.isNaN(hsl(_.rgb("white")).h);
assert.hslEqual(hsl("#000"), NaN, NaN, 0);
assert.hslEqual(hsl("black"), NaN, NaN, 0);
assert.hslEqual(hsl(_.rgb("black")), NaN, NaN, 0);
assert.hslEqual(hsl("#ccc"), NaN, 0, .8);
assert.hslEqual(hsl("gray"), NaN, 0, .5);
assert.hslEqual(hsl(_.rgb("gray")), NaN, 0, .5);
assert.hslEqual(hsl("#fff"), NaN, NaN, 1);
assert.hslEqual(hsl("white"), NaN, NaN, 1);
assert.hslEqual(hsl(_.rgb("white")), NaN, NaN, 1);
},
"s is preserved when explicitly specified, even for white or black": function(hsl) {
assert.strictEqual(hsl(0, 0, 0).s, 0);
assert.strictEqual(hsl(0, .18, 0).s, .18);
assert.strictEqual(hsl(0, .42, 1).s, .42);
assert.strictEqual(hsl(0, 1, 1).s, 1);
assert.hslEqual(hsl(0, 0, 0), 0, 0, 0);
assert.hslEqual(hsl(0, .18, 0), 0, .18, 0);
assert.hslEqual(hsl(0, .42, 1), 0, .42, 1);
assert.hslEqual(hsl(0, 1, 1), 0, 1, 1);
},
"s is zero for grayscale colors (but not white and black)": function(hsl) {
assert.strictEqual(hsl("#ccc").s, 0);
assert.strictEqual(hsl("#777").s, 0);
assert.hslEqual(hsl("#ccc"), NaN, 0, .8);
assert.hslEqual(hsl("#777"), NaN, 0, .47);
},
"s is undefined when not explicitly specified for white or black": function(hsl) {
assert.isNaN(hsl("#000").s);
assert.isNaN(hsl("black").s);
assert.isNaN(hsl(_.rgb("black")).s);
assert.isNaN(hsl("#fff").s);
assert.isNaN(hsl("white").s);
assert.isNaN(hsl(_.rgb("white")).s);
assert.hslEqual(hsl("#000"), NaN, NaN, 0);
assert.hslEqual(hsl("black"), NaN, NaN, 0);
assert.hslEqual(hsl(_.rgb("black")), NaN, NaN, 0);
assert.hslEqual(hsl("#fff"), NaN, NaN, 1);
assert.hslEqual(hsl("white"), NaN, NaN, 1);
assert.hslEqual(hsl(_.rgb("white")), NaN, NaN, 1);
},
"can convert grayscale colors (with undefined hue) to RGB": function(hsl) {
assert.strictEqual(hsl(NaN, 0, .2) + "", "#333333");
......
......@@ -9,10 +9,10 @@ suite.addBatch({
"lab": {
topic: load("color/lab").expression("d3.lab"),
"converts string channel values to numbers": function(lab) {
assertLabEqual(lab("50", "-4", "-32"), 50, -4, -32);
assert.labEqual(lab("50", "-4", "-32"), 50, -4, -32);
},
"converts null channel values to zero": function(lab) {
assertLabEqual(lab(null, null, null), 0, 0, 0);
assert.labEqual(lab(null, null, null), 0, 0, 0);
},
"exposes l, a and b properties": function(lab) {
var color = lab(50, -4, -32);
......@@ -31,57 +31,51 @@ suite.addBatch({
assert.equal(color + "", "#487eae");
},
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(lab) {
assertLabEqual(lab("#abc"), 75.10497524893663, -2.292114632248876, -10.528266458853786);
assert.labEqual(lab("#abc"), 75.10497524893663, -2.292114632248876, -10.528266458853786);
},
"parses hexadecimal format (e.g., \"#abcdef\")": function(lab) {
assertLabEqual(lab("#abcdef"), 81.04386565274363, -3.6627002800885267, -20.442705201854984);
assert.labEqual(lab("#abcdef"), 81.04386565274363, -3.6627002800885267, -20.442705201854984);
},
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(lab) {
assertLabEqual(lab("hsl(210, 64.7058%, 13.33333%)"), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
assert.labEqual(lab("hsl(210, 64.7058%, 13.33333%)"), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
},
"parses color names (e.g., \"moccasin\")": function(lab) {
assertLabEqual(lab("moccasin"), 91.72317744746022, 2.4393469358685027, 26.359832514614844);
assert.labEqual(lab("moccasin"), 91.72317744746022, 2.4393469358685027, 26.359832514614844);
},
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(lab) {
assertLabEqual(lab("rgb(102, 102, 0)"), 41.73251953866431, -10.998411255098816, 48.21006600604577);
assert.labEqual(lab("rgb(102, 102, 0)"), 41.73251953866431, -10.998411255098816, 48.21006600604577);
},
"can convert from RGB": function(lab) {
assertLabEqual(lab(_.rgb(12, 34, 56)), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
assert.labEqual(lab(_.rgb(12, 34, 56)), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
},
"can convert from HSL": function(lab) {
assertLabEqual(lab(lab(20, .8, .3)), 20, 0.8, 0.3);
assert.labEqual(lab(lab(20, .8, .3)), 20, 0.8, 0.3);
},
"can convert to RGB": function(lab) {
assert.rgbEqual(lab("steelblue").rgb(), 70, 130, 180);
},
"can derive a brighter color": function(lab) {
assertLabEqual(lab("steelblue").brighter(), 70.46551718768575, -4.0774710123572255, -32.19186122981343);
assertLabEqual(lab("steelblue").brighter(.5), 61.46551718768575, -4.0774710123572255, -32.19186122981343);
assert.labEqual(lab("steelblue").brighter(), 70.46551718768575, -4.0774710123572255, -32.19186122981343);
assert.labEqual(lab("steelblue").brighter(.5), 61.46551718768575, -4.0774710123572255, -32.19186122981343);
},
"can derive a darker color": function(lab) {
assertLabEqual(lab("lightsteelblue").darker(), 60.45157936968134, -1.2815839134120433, -15.210996213841522);
assertLabEqual(lab("lightsteelblue").darker(.5), 69.45157936968134, -1.2815839134120433, -15.210996213841522);
assert.labEqual(lab("lightsteelblue").darker(), 60.45157936968134, -1.2815839134120433, -15.210996213841522);
assert.labEqual(lab("lightsteelblue").darker(.5), 69.45157936968134, -1.2815839134120433, -15.210996213841522);
},
"string coercion returns RGB format": function(lab) {
assert.strictEqual(lab("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(lab(lab(60, -4, -32)) + "", "#5d95c8");
},
"roundtrip to HSL is idempotent": function(lab) {
assert.hslEqual(_.hsl(lab("steelblue")), _.hsl("steelblue"));
assert.deepEqual(_.hsl(lab("steelblue")), _.hsl("steelblue"));
},
"roundtrip to RGB is idempotent": function(lab) {
assert.hslEqual(_.rgb(lab("steelblue")), _.rgb("steelblue"));
assert.deepEqual(_.rgb(lab("steelblue")), _.rgb("steelblue"));
},
"roundtrip to HCL is idempotent": function(lab) {
assert.hslEqual(_.hcl(lab("steelblue")), _.hcl("steelblue"));
assert.deepEqual(_.hcl(lab("steelblue")), _.hcl("steelblue"));
}
}
});
suite.export(module);
function assertLabEqual(actual, l, a, b, message) {
if (Math.abs(actual.l - l) > 1e-6 || Math.abs(actual.a - a) > 1e-6 || Math.abs(actual.b - b) > 1e-6) {
assert.fail("lab(" + actual.l + ", " + actual.a + ", " + actual.b + ")", "lab(" + l + ", " + a + ", " + b + ")", message || "expected {expected}, got {actual}", null, assertLabEqual);
}
}
......@@ -8,38 +8,38 @@ suite.addBatch({
"interpolateHcl": {
topic: load("interpolate/hcl"), // beware instanceof d3_Color
"parses string input": function(d3) {
assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");
assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.6), "#e034a2");
assert.hclEqual(d3.interpolateHcl("steelblue", "#f00")(.2), -69.78, 46.87, 52.62);
assert.hclEqual(d3.interpolateHcl("steelblue", "#f00")(.6), -14.89, 75.71, 52.93);
},
"parses d3.hsl input": function(d3) {
assert.strictEqual(d3.interpolateHcl(d3.hsl("steelblue"), "#f00")(.2), "#6978c9");
assert.strictEqual(d3.interpolateHcl("steelblue", d3.hsl(0, 1, .5))(.6), "#e034a2");
assert.hclEqual(d3.interpolateHcl(d3.hsl("steelblue"), "#f00")(.2), -69.78, 46.87, 52.62);
assert.hclEqual(d3.interpolateHcl("steelblue", d3.hsl(0, 1, .5))(.6), -14.89, 75.71, 52.93);
},
"parses d3.rgb input": function(d3) {
assert.strictEqual(d3.interpolateHcl(d3.rgb("steelblue"), "#f00")(.2), "#6978c9");
assert.strictEqual(d3.interpolateHcl("steelblue", d3.rgb(255, 0, 0))(.6), "#e034a2");
assert.hclEqual(d3.interpolateHcl(d3.rgb("steelblue"), "#f00")(.2), -69.78, 46.87, 52.62);
assert.hclEqual(d3.interpolateHcl("steelblue", d3.rgb(255, 0, 0))(.6), -14.89, 75.71, 52.93);
},
"interpolates in HSL color space": function(d3) {
assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");
assert.hclEqual(d3.interpolateHcl("steelblue", "#f00")(.2), -69.78, 46.87, 52.62);
},
"uses source hue when destination hue is undefined": function(d3) {
assert.equal(d3.interpolateHcl("#f60", "#000")(.5), "#9b0000");
assert.equal(d3.interpolateHcl("#6f0", "#000")(.5), "#008100");
assert.hclEqual(d3.interpolateHcl("#f60", "#000")(.5), 52.36, 90.07, 31.16);
assert.hclEqual(d3.interpolateHcl("#6f0", "#000")(.5), 131.26, 112.84, 44.54);
},
"uses destination hue when source hue is undefined": function(d3) {
assert.equal(d3.interpolateHcl("#000", "#f60")(.5), "#9b0000");
assert.equal(d3.interpolateHcl("#000", "#6f0")(.5), "#008100");
assert.hclEqual(d3.interpolateHcl("#000", "#f60")(.5), 52.36, 90.07, 31.16);
assert.hclEqual(d3.interpolateHcl("#000", "#6f0")(.5), 131.26, 112.84, 44.54);
},
"uses source chroma when destination chroma is undefined": function(d3) {
assert.equal(d3.interpolateHcl("#ccc", "#000")(.5), "#616161");
assert.equal(d3.interpolateHcl("#f00", "#000")(.5), "#a60000");
assert.hclEqual(d3.interpolateHcl("#ccc", "#000")(.5), 158.20, 0.00, 41.02);
assert.hclEqual(d3.interpolateHcl("#f00", "#000")(.5), 40.00, 104.55, 26.62);
},
"uses destination chroma when source chroma is undefined": function(d3) {
assert.equal(d3.interpolateHcl("#000", "#ccc")(.5), "#616161");
assert.equal(d3.interpolateHcl("#000", "#f00")(.5), "#a60000");
assert.hclEqual(d3.interpolateHcl("#000", "#ccc")(.5), 158.20, 0.00, 41.02);
assert.hclEqual(d3.interpolateHcl("#000", "#f00")(.5), 40.00, 104.55, 26.62);
},
"outputs a hexadecimal string": function(d3) {
assert.strictEqual(d3.interpolateHcl("steelblue", "#f00")(.2), "#6978c9");
"returns an instanceof d3.hcl": function(d3) {
assert.hclEqual(d3.interpolateHcl("steelblue", "#f00")(.2), -69.78, 46.87, 52.62);
}
}
});
......
......@@ -8,38 +8,38 @@ suite.addBatch({
"interpolateHsl": {
topic: load("interpolate/hsl"), // beware instanceof d3_Color
"parses string input": function(d3) {
assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.2), "#383dc3");
assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.6), "#dd1ce1");
assert.hslEqual(d3.interpolateHsl("steelblue", "#f00")(.2), 237.82, 0.5538, 0.4922);
assert.hslEqual(d3.interpolateHsl("steelblue", "#f00")(.6), 298.91, 0.7787, 0.4961);
},
"parses d3.hsl input": function(d3) {
assert.strictEqual(d3.interpolateHsl(d3.hsl("steelblue"), "#f00")(.2), "#383dc3");
assert.strictEqual(d3.interpolateHsl("steelblue", d3.hsl(0, 1, .5))(.6), "#dd1ce1");
assert.hslEqual(d3.interpolateHsl(d3.hsl("steelblue"), "#f00")(.2), 237.82, 0.5538, 0.4922);
assert.hslEqual(d3.interpolateHsl("steelblue", d3.hsl(0, 1, .5))(.6), 298.91, 0.7787, 0.4961);
},
"parses d3.rgb input": function(d3) {
assert.strictEqual(d3.interpolateHsl(d3.rgb("steelblue"), "#f00")(.2), "#383dc3");
assert.strictEqual(d3.interpolateHsl("steelblue", d3.rgb(255, 0, 0))(.6), "#dd1ce1");
assert.hslEqual(d3.interpolateHsl(d3.rgb("steelblue"), "#f00")(.2), 237.82, 0.5538, 0.4922);
assert.hslEqual(d3.interpolateHsl("steelblue", d3.rgb(255, 0, 0))(.6), 298.91, 0.7787, 0.4961);
},
"interpolates in HSL color space": function(d3) {
assert.strictEqual(d3.interpolateHsl("steelblue", "#f00")(.2), "#383dc3");
assert.hslEqual(d3.interpolateHsl("steelblue", "#f00")(.2), 237.82, 0.5538, 0.4922);
},
"uses source hue when destination hue is undefined": function(d3) {
assert.equal(d3.interpolateHsl("#f60", "#000")(.5), "#803300");
assert.equal(d3.interpolateHsl("#6f0", "#fff")(.5), "#b3ff80");
assert.hslEqual(d3.interpolateHsl("#f60", "#000")(.5), 24, 1, 0.2510);
assert.hslEqual(d3.interpolateHsl("#6f0", "#fff")(.5), 96, 1, 0.7510);
},
"uses destination hue when source hue is undefined": function(d3) {
assert.equal(d3.interpolateHsl("#000", "#f60")(.5), "#803300");
assert.equal(d3.interpolateHsl("#fff", "#6f0")(.5), "#b3ff80");
assert.hslEqual(d3.interpolateHsl("#000", "#f60")(.5), 24, 1, 0.2510);
assert.hslEqual(d3.interpolateHsl("#fff", "#6f0")(.5), 96, 1, 0.7510);
},
"uses source saturation when destination saturation is undefined": function(d3) {
assert.equal(d3.interpolateHsl("#ccc", "#000")(.5), "#666666");
assert.equal(d3.interpolateHsl("#f00", "#000")(.5), "#800000");
assert.hslEqual(d3.interpolateHsl("#ccc", "#000")(.5), NaN, 0, 0.4);
assert.hslEqual(d3.interpolateHsl("#f00", "#000")(.5), 0, 1, 0.2510);
},