Due to the tests using two different copies of d3 (one loaded via require, and
the other via smash), the instanceof check in the d3.lab constructor was
returning false when testing for d3.hcl, and this constructor was therefore
never tested.
This commit is contained in:
Mike Bostock 2014-10-13 14:32:08 -07:00
Родитель 1fc660a6b6
Коммит b6b80f7270
7 изменённых файлов: 245 добавлений и 243 удалений

2
d3.js поставляемый
Просмотреть файл

@ -1548,7 +1548,7 @@
} }
d3.lab = d3_lab; d3.lab = d3_lab;
function d3_lab(l, a, b) { function d3_lab(l, a, b) {
return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.l, l.c, l.h) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b); return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) : arguments.length < 2 ? l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b) : new d3_lab(l, a, b);
} }
var d3_lab_K = 18; var d3_lab_K = 18;
var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883; var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883;

2
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -8,7 +8,7 @@ d3.lab = d3_lab;
function d3_lab(l, a, b) { function d3_lab(l, a, b) {
return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b) return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b)
: arguments.length < 2 ? (l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b) : arguments.length < 2 ? (l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b)
: (l instanceof d3_hcl ? d3_hcl_lab(l.l, l.c, l.h) : (l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l)
: d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b))) : d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b)))
: new d3_lab(l, a, b); : new d3_lab(l, a, b);
} }

Просмотреть файл

@ -1,5 +1,4 @@
var vows = require("vows"), var vows = require("vows"),
_ = require("../../"),
load = require("../load"), load = require("../load"),
assert = require("../assert"); assert = require("../assert");
@ -7,21 +6,21 @@ var suite = vows.describe("d3.hcl");
suite.addBatch({ suite.addBatch({
"hcl": { "hcl": {
topic: load("color/hcl").expression("d3.hcl"), topic: load("color/hcl", "color/lab", "color/rgb").expression("d3"),
"converts string channel values to numbers": function(hcl) { "converts string channel values to numbers": function(d3) {
assert.hclEqual(hcl("50", "-4", "32"), 50, -4, 32); assert.hclEqual(d3.hcl("50", "-4", "32"), 50, -4, 32);
}, },
"converts null channel values to zero": function(hcl) { "converts null channel values to zero": function(d3) {
assert.hclEqual(hcl(null, null, null), 0, 0, 0); assert.hclEqual(d3.hcl(null, null, null), 0, 0, 0);
}, },
"exposes h, c and l properties": function(hcl) { "exposes h, c and l properties": function(d3) {
var color = hcl(50, -4, 32); var color = d3.hcl(50, -4, 32);
assert.equal(color.h, 50); assert.equal(color.h, 50);
assert.equal(color.c, -4); assert.equal(color.c, -4);
assert.equal(color.l, 32); assert.equal(color.l, 32);
}, },
"changing h, c or l affects the string format": function(hcl) { "changing h, c or l affects the string format": function(d3) {
var color = hcl(50, -4, 32); var color = d3.hcl(50, -4, 32);
assert.equal(color + "", "#444d50"); assert.equal(color + "", "#444d50");
color.h++; color.h++;
assert.equal(color + "", "#444d50"); assert.equal(color + "", "#444d50");
@ -30,81 +29,84 @@ suite.addBatch({
color.l++; color.l++;
assert.equal(color + "", "#494f51"); assert.equal(color + "", "#494f51");
}, },
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(hcl) { "parses hexadecimal shorthand format (e.g., \"#abc\")": function(d3) {
assert.hclEqual(hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663); assert.hclEqual(d3.hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
}, },
"parses hexadecimal format (e.g., \"#abcdef\")": function(hcl) { "parses hexadecimal format (e.g., \"#abcdef\")": function(d3) {
assert.hclEqual(hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363); assert.hclEqual(d3.hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
}, },
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(hcl) { "parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(d3) {
assert.hclEqual(hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134); assert.hclEqual(d3.hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
}, },
"parses color names (e.g., \"moccasin\")": function(hcl) { "parses color names (e.g., \"moccasin\")": function(d3) {
assert.hclEqual(hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022); assert.hclEqual(d3.hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
}, },
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(hcl) { "parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(d3) {
assert.hclEqual(hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431); assert.hclEqual(d3.hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
}, },
"can convert from RGB": function(hcl) { "can convert from RGB": function(d3) {
assert.hclEqual(hcl(_.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134); assert.hclEqual(d3.hcl(d3.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
}, },
"can convert from HSL": function(hcl) { "can convert from HSL": function(d3) {
assert.hclEqual(hcl(hcl(20, .8, .3)), 20, 0.8, 0.3); assert.hclEqual(d3.hcl(d3.hcl(20, .8, .3)), 20, 0.8, 0.3);
}, },
"can convert to RGB": function(hcl) { "can convert to RGB": function(d3) {
assert.rgbEqual(hcl("steelblue").rgb(), 70, 130, 180); assert.rgbEqual(d3.hcl("steelblue").rgb(), 70, 130, 180);
}, },
"can derive a brighter color": function(hcl) { "can convert from Lab": function(d3) {
assert.hclEqual(hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575); assert.hclEqual(d3.hcl(d3.lab(59.93, 7.02, -39.63)), -79.95, 40.25, 59.93);
assert.hclEqual(hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
}, },
"can derive a darker color": function(hcl) { "can derive a brighter color": function(d3) {
assert.hclEqual(hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134); assert.hclEqual(d3.hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
assert.hclEqual(hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134); assert.hclEqual(d3.hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
}, },
"string coercion returns RGB format": function(hcl) { "can derive a darker color": function(d3) {
assert.strictEqual(hcl("hsl(60, 100%, 20%)") + "", "#666600"); assert.hclEqual(d3.hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
assert.strictEqual(hcl(hcl(60, -4, 32)) + "", "#454c51"); assert.hclEqual(d3.hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
}, },
"roundtrip to HSL is idempotent": function(hcl) { "string coercion returns RGB format": function(d3) {
assert.deepEqual(_.hsl(hcl("steelblue")), _.hsl("steelblue")); assert.strictEqual(d3.hcl("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(d3.hcl(d3.hcl(60, -4, 32)) + "", "#454c51");
}, },
"roundtrip to RGB is idempotent": function(hcl) { "roundtrip to HSL is idempotent": function(d3) {
assert.deepEqual(_.rgb(hcl("steelblue")), _.rgb("steelblue")); assert.deepEqual(d3.hsl(d3.hcl("steelblue")), d3.hsl("steelblue"));
}, },
"roundtrip to Lab is idempotent": function(hcl) { "roundtrip to RGB is idempotent": function(d3) {
assert.deepEqual(_.lab(hcl("steelblue")), _.lab("steelblue")); assert.deepEqual(d3.rgb(d3.hcl("steelblue")), d3.rgb("steelblue"));
}, },
"h is defined for non-black grayscale colors (because of the color profile)": function(hcl) { "roundtrip to Lab is idempotent": function(d3) {
assert.inDelta(hcl("#ccc").h, 158.1986, 1e-3); assert.labEqual(d3.lab(d3.hcl("steelblue")), 52.47, -4.08, -32.19);
assert.inDelta(hcl("gray").h, 158.1986, 1e-3);
assert.inDelta(hcl(_.rgb("gray")).h, 158.1986, 1e-3);
assert.inDelta(hcl("#fff").h, 158.1986, 1e-3);
assert.inDelta(hcl("white").h, 158.1986, 1e-3);
assert.inDelta(hcl(_.rgb("white")).h, 158.1986, 1e-3);
}, },
"h is preserved when explicitly specified, even for black": function(hcl) { "h is defined for non-black grayscale colors (because of the color profile)": function(d3) {
assert.strictEqual(hcl(0, 0, 0).h, 0); assert.inDelta(d3.hcl("#ccc").h, 158.1986, 1e-3);
assert.strictEqual(hcl(42, 0, 0).h, 42); assert.inDelta(d3.hcl("gray").h, 158.1986, 1e-3);
assert.strictEqual(hcl(118, 0, 0).h, 118); assert.inDelta(d3.hcl(d3.rgb("gray")).h, 158.1986, 1e-3);
assert.inDelta(d3.hcl("#fff").h, 158.1986, 1e-3);
assert.inDelta(d3.hcl("white").h, 158.1986, 1e-3);
assert.inDelta(d3.hcl(d3.rgb("white")).h, 158.1986, 1e-3);
}, },
"h is undefined when not explicitly specified for black": function(hcl) { "h is preserved when explicitly specified, even for black": function(d3) {
assert.isNaN(hcl("#000").h); assert.strictEqual(d3.hcl(0, 0, 0).h, 0);
assert.isNaN(hcl("black").h); assert.strictEqual(d3.hcl(42, 0, 0).h, 42);
assert.isNaN(hcl(_.rgb("black")).h); assert.strictEqual(d3.hcl(118, 0, 0).h, 118);
}, },
"c is preserved when explicitly specified, even for black": function(hcl) { "h is undefined when not explicitly specified for black": function(d3) {
assert.strictEqual(hcl(0, 0, 0).c, 0); assert.isNaN(d3.hcl("#000").h);
assert.strictEqual(hcl(0, .42, 0).c, .42); assert.isNaN(d3.hcl("black").h);
assert.strictEqual(hcl(0, 1, 0).c, 1); assert.isNaN(d3.hcl(d3.rgb("black")).h);
}, },
"c is undefined when not explicitly specified for black": function(hcl) { "c is preserved when explicitly specified, even for black": function(d3) {
assert.isNaN(hcl("#000").c); assert.strictEqual(d3.hcl(0, 0, 0).c, 0);
assert.isNaN(hcl("black").c); assert.strictEqual(d3.hcl(0, .42, 0).c, .42);
assert.isNaN(hcl(_.rgb("black")).c); assert.strictEqual(d3.hcl(0, 1, 0).c, 1);
}, },
"can convert black (with undefined hue and chroma) to RGB": function(hcl) { "c is undefined when not explicitly specified for black": function(d3) {
assert.strictEqual(hcl(NaN, NaN, 0) + "", "#000000"); assert.isNaN(d3.hcl("#000").c);
assert.isNaN(d3.hcl("black").c);
assert.isNaN(d3.hcl(d3.rgb("black")).c);
},
"can convert black (with undefined hue and chroma) to RGB": function(d3) {
assert.strictEqual(d3.hcl(NaN, NaN, 0) + "", "#000000");
} }
} }
}); });

Просмотреть файл

@ -1,5 +1,4 @@
var vows = require("vows"), var vows = require("vows"),
_ = require("../../"),
load = require("../load"), load = require("../load"),
assert = require("../assert"); assert = require("../assert");
@ -7,110 +6,110 @@ var suite = vows.describe("d3.hsl");
suite.addBatch({ suite.addBatch({
"hsl": { "hsl": {
topic: load("color/hsl").expression("d3.hsl"), topic: load("color/hsl", "color/rgb").expression("d3"),
"does not clamp channel values": function(hsl) { "does not clamp channel values": function(d3) {
assert.hslEqual(hsl(-100, -1, -2), -100, -1, -2); assert.hslEqual(d3.hsl(-100, -1, -2), -100, -1, -2);
assert.hslEqual(hsl(400, 2, 3), 400, 2, 3); assert.hslEqual(d3.hsl(400, 2, 3), 400, 2, 3);
}, },
"converts string channel values to numbers": function(hsl) { "converts string channel values to numbers": function(d3) {
assert.hslEqual(hsl("180", ".5", ".6"), 180, .5, .6); assert.hslEqual(d3.hsl("180", ".5", ".6"), 180, .5, .6);
}, },
"converts null channel values to zero": function(hsl) { "converts null channel values to zero": function(d3) {
assert.hslEqual(hsl(null, null, null), 0, 0, 0); assert.hslEqual(d3.hsl(null, null, null), 0, 0, 0);
}, },
"exposes h, s and l properties": function(hsl) { "exposes h, s and l properties": function(d3) {
assert.hslEqual(hsl("hsl(180, 50%, 60%)"), 180, .5, .6); assert.hslEqual(d3.hsl("hsl(180, 50%, 60%)"), 180, .5, .6);
}, },
"changing h, s or l affects the string format": function(hsl) { "changing h, s or l affects the string format": function(d3) {
var color = hsl("hsl(180, 50%, 60%)"); var color = d3.hsl("hsl(180, 50%, 60%)");
color.h++; color.h++;
color.s += .1; color.s += .1;
color.l += .1; color.l += .1;
assert.equal(color + "", "#85dfe0"); assert.equal(color + "", "#85dfe0");
}, },
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(hsl) { "parses hexadecimal shorthand format (e.g., \"#abc\")": function(d3) {
assert.hslEqual(hsl("#abc"), 210, .25, .733333); assert.hslEqual(d3.hsl("#abc"), 210, .25, .733333);
}, },
"parses hexadecimal format (e.g., \"#abcdef\")": function(hsl) { "parses hexadecimal format (e.g., \"#abcdef\")": function(d3) {
assert.hslEqual(hsl("#abcdef"), 210, .68, .803922); assert.hslEqual(d3.hsl("#abcdef"), 210, .68, .803922);
}, },
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(hsl) { "parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(d3) {
assert.hslEqual(hsl("hsl(210, 64.7058%, 13.33333%)"), 210, .647058, .133333); assert.hslEqual(d3.hsl("hsl(210, 64.7058%, 13.33333%)"), 210, .647058, .133333);
}, },
"parses color names (e.g., \"moccasin\")": function(hsl) { "parses color names (e.g., \"moccasin\")": function(d3) {
assert.hslEqual(hsl("moccasin"), 38.108108, 1, .854902); assert.hslEqual(d3.hsl("moccasin"), 38.108108, 1, .854902);
assert.hslEqual(hsl("aliceblue"), 208, 1, .970588); assert.hslEqual(d3.hsl("aliceblue"), 208, 1, .970588);
assert.hslEqual(hsl("yellow"), 60, 1, .5); assert.hslEqual(d3.hsl("yellow"), 60, 1, .5);
}, },
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(hsl) { "parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(d3) {
assert.hslEqual(hsl("rgb(102, 102, 0)"), 60, 1, .2); assert.hslEqual(d3.hsl("rgb(102, 102, 0)"), 60, 1, .2);
}, },
"can convert from RGB": function(hsl) { "can convert from RGB": function(d3) {
assert.hslEqual(hsl(_.rgb(12, 34, 56)), 210, .647058, .133333); assert.hslEqual(d3.hsl(d3.rgb(12, 34, 56)), 210, .647058, .133333);
}, },
"can convert from HSL": function(hsl) { "can convert from HSL": function(d3) {
assert.hslEqual(hsl(hsl(20, .8, .3)), 20, .8, .3); assert.hslEqual(d3.hsl(d3.hsl(20, .8, .3)), 20, .8, .3);
}, },
"can convert to RGB": function(hsl) { "can convert to RGB": function(d3) {
assert.rgbEqual(hsl("steelblue").rgb(), 70, 130, 180); assert.rgbEqual(d3.hsl("steelblue").rgb(), 70, 130, 180);
}, },
"can derive a brighter color": function(hsl) { "can derive a brighter color": function(d3) {
assert.hslEqual(hsl("steelblue").brighter(), 207.272727, .44, .7002801); assert.hslEqual(d3.hsl("steelblue").brighter(), 207.272727, .44, .7002801);
assert.hslEqual(hsl("steelblue").brighter(.5), 207.272727, .44, .5858964); assert.hslEqual(d3.hsl("steelblue").brighter(.5), 207.272727, .44, .5858964);
assert.hslEqual(hsl("steelblue").brighter(1), 207.272727, .44, .7002801); assert.hslEqual(d3.hsl("steelblue").brighter(1), 207.272727, .44, .7002801);
assert.hslEqual(hsl("steelblue").brighter(2), 207.272727, .44, 1.0004002); assert.hslEqual(d3.hsl("steelblue").brighter(2), 207.272727, .44, 1.0004002);
}, },
"can derive a darker color": function(hsl) { "can derive a darker color": function(d3) {
assert.hslEqual(hsl("lightsteelblue").darker(), 213.913043, .4107143, .5462745); assert.hslEqual(d3.hsl("lightsteelblue").darker(), 213.913043, .4107143, .5462745);
assert.hslEqual(hsl("lightsteelblue").darker(.5), 213.913043, .4107143, .6529229); assert.hslEqual(d3.hsl("lightsteelblue").darker(.5), 213.913043, .4107143, .6529229);
assert.hslEqual(hsl("lightsteelblue").darker(1), 213.913043, .4107143, .5462745); assert.hslEqual(d3.hsl("lightsteelblue").darker(1), 213.913043, .4107143, .5462745);
assert.hslEqual(hsl("lightsteelblue").darker(2), 213.913043, .4107143, .38239216); assert.hslEqual(d3.hsl("lightsteelblue").darker(2), 213.913043, .4107143, .38239216);
}, },
"string coercion returns RGB format": function(hsl) { "string coercion returns RGB format": function(d3) {
assert.strictEqual(hsl("hsl(60, 100%, 20%)") + "", "#666600"); assert.strictEqual(d3.hsl("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(hsl(hsl(60, 1, .2)) + "", "#666600"); assert.strictEqual(d3.hsl(d3.hsl(60, 1, .2)) + "", "#666600");
}, },
"h is preserved when explicitly specified, even for grayscale colors": function(hsl) { "h is preserved when explicitly specified, even for grayscale colors": function(d3) {
assert.hslEqual(hsl(0, 0, 0), 0, 0, 0); assert.hslEqual(d3.hsl(0, 0, 0), 0, 0, 0);
assert.hslEqual(hsl(42, 0, .5), 42, 0, .5); assert.hslEqual(d3.hsl(42, 0, .5), 42, 0, .5);
assert.hslEqual(hsl(118, 0, 1), 118, 0, 1); assert.hslEqual(d3.hsl(118, 0, 1), 118, 0, 1);
}, },
"h is undefined when not explicitly specified for grayscale colors": function(hsl) { "h is undefined when not explicitly specified for grayscale colors": function(d3) {
assert.hslEqual(hsl("#000"), NaN, NaN, 0); assert.hslEqual(d3.hsl("#000"), NaN, NaN, 0);
assert.hslEqual(hsl("black"), NaN, NaN, 0); assert.hslEqual(d3.hsl("black"), NaN, NaN, 0);
assert.hslEqual(hsl(_.rgb("black")), NaN, NaN, 0); assert.hslEqual(d3.hsl(d3.rgb("black")), NaN, NaN, 0);
assert.hslEqual(hsl("#ccc"), NaN, 0, .8); assert.hslEqual(d3.hsl("#ccc"), NaN, 0, .8);
assert.hslEqual(hsl("gray"), NaN, 0, .5); assert.hslEqual(d3.hsl("gray"), NaN, 0, .5);
assert.hslEqual(hsl(_.rgb("gray")), NaN, 0, .5); assert.hslEqual(d3.hsl(d3.rgb("gray")), NaN, 0, .5);
assert.hslEqual(hsl("#fff"), NaN, NaN, 1); assert.hslEqual(d3.hsl("#fff"), NaN, NaN, 1);
assert.hslEqual(hsl("white"), NaN, NaN, 1); assert.hslEqual(d3.hsl("white"), NaN, NaN, 1);
assert.hslEqual(hsl(_.rgb("white")), NaN, NaN, 1); assert.hslEqual(d3.hsl(d3.rgb("white")), NaN, NaN, 1);
}, },
"s is preserved when explicitly specified, even for white or black": function(hsl) { "s is preserved when explicitly specified, even for white or black": function(d3) {
assert.hslEqual(hsl(0, 0, 0), 0, 0, 0); assert.hslEqual(d3.hsl(0, 0, 0), 0, 0, 0);
assert.hslEqual(hsl(0, .18, 0), 0, .18, 0); assert.hslEqual(d3.hsl(0, .18, 0), 0, .18, 0);
assert.hslEqual(hsl(0, .42, 1), 0, .42, 1); assert.hslEqual(d3.hsl(0, .42, 1), 0, .42, 1);
assert.hslEqual(hsl(0, 1, 1), 0, 1, 1); assert.hslEqual(d3.hsl(0, 1, 1), 0, 1, 1);
}, },
"s is zero for grayscale colors (but not white and black)": function(hsl) { "s is zero for grayscale colors (but not white and black)": function(d3) {
assert.hslEqual(hsl("#ccc"), NaN, 0, .8); assert.hslEqual(d3.hsl("#ccc"), NaN, 0, .8);
assert.hslEqual(hsl("#777"), NaN, 0, .47); assert.hslEqual(d3.hsl("#777"), NaN, 0, .47);
}, },
"s is undefined when not explicitly specified for white or black": function(hsl) { "s is undefined when not explicitly specified for white or black": function(d3) {
assert.hslEqual(hsl("#000"), NaN, NaN, 0); assert.hslEqual(d3.hsl("#000"), NaN, NaN, 0);
assert.hslEqual(hsl("black"), NaN, NaN, 0); assert.hslEqual(d3.hsl("black"), NaN, NaN, 0);
assert.hslEqual(hsl(_.rgb("black")), NaN, NaN, 0); assert.hslEqual(d3.hsl(d3.rgb("black")), NaN, NaN, 0);
assert.hslEqual(hsl("#fff"), NaN, NaN, 1); assert.hslEqual(d3.hsl("#fff"), NaN, NaN, 1);
assert.hslEqual(hsl("white"), NaN, NaN, 1); assert.hslEqual(d3.hsl("white"), NaN, NaN, 1);
assert.hslEqual(hsl(_.rgb("white")), NaN, NaN, 1); assert.hslEqual(d3.hsl(d3.rgb("white")), NaN, NaN, 1);
}, },
"can convert grayscale colors (with undefined hue) to RGB": function(hsl) { "can convert grayscale colors (with undefined hue) to RGB": function(d3) {
assert.strictEqual(hsl(NaN, 0, .2) + "", "#333333"); assert.strictEqual(d3.hsl(NaN, 0, .2) + "", "#333333");
assert.strictEqual(hsl(NaN, 0, .6) + "", "#999999"); assert.strictEqual(d3.hsl(NaN, 0, .6) + "", "#999999");
}, },
"can convert white and black (with undefined hue and saturation) to RGB": function(hsl) { "can convert white and black (with undefined hue and saturation) to RGB": function(d3) {
assert.strictEqual(hsl(NaN, NaN, 0) + "", "#000000"); assert.strictEqual(d3.hsl(NaN, NaN, 0) + "", "#000000");
assert.strictEqual(hsl(NaN, NaN, 1) + "", "#ffffff"); assert.strictEqual(d3.hsl(NaN, NaN, 1) + "", "#ffffff");
} }
} }
}); });

Просмотреть файл

@ -1,5 +1,4 @@
var vows = require("vows"), var vows = require("vows"),
_ = require("../../"),
load = require("../load"), load = require("../load"),
assert = require("../assert"); assert = require("../assert");
@ -7,21 +6,21 @@ var suite = vows.describe("d3.lab");
suite.addBatch({ suite.addBatch({
"lab": { "lab": {
topic: load("color/lab").expression("d3.lab"), topic: load("color/lab", "color/hcl", "color/rgb").expression("d3"),
"converts string channel values to numbers": function(lab) { "converts string channel values to numbers": function(d3) {
assert.labEqual(lab("50", "-4", "-32"), 50, -4, -32); assert.labEqual(d3.lab("50", "-4", "-32"), 50, -4, -32);
}, },
"converts null channel values to zero": function(lab) { "converts null channel values to zero": function(d3) {
assert.labEqual(lab(null, null, null), 0, 0, 0); assert.labEqual(d3.lab(null, null, null), 0, 0, 0);
}, },
"exposes l, a and b properties": function(lab) { "exposes l, a and b properties": function(d3) {
var color = lab(50, -4, -32); var color = d3.lab(50, -4, -32);
assert.equal(color.l, 50); assert.equal(color.l, 50);
assert.equal(color.a, -4); assert.equal(color.a, -4);
assert.equal(color.b, -32); assert.equal(color.b, -32);
}, },
"changing l, a or b affects the string format": function(lab) { "changing l, a or b affects the string format": function(d3) {
var color = lab(50, -4, -32); var color = d3.lab(50, -4, -32);
assert.equal(color + "", "#3f7cad"); assert.equal(color + "", "#3f7cad");
color.l++; color.l++;
assert.equal(color + "", "#427eb0"); assert.equal(color + "", "#427eb0");
@ -30,50 +29,53 @@ suite.addBatch({
color.b++; color.b++;
assert.equal(color + "", "#487eae"); assert.equal(color + "", "#487eae");
}, },
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(lab) { "parses hexadecimal shorthand format (e.g., \"#abc\")": function(d3) {
assert.labEqual(lab("#abc"), 75.10497524893663, -2.292114632248876, -10.528266458853786); assert.labEqual(d3.lab("#abc"), 75.10497524893663, -2.292114632248876, -10.528266458853786);
}, },
"parses hexadecimal format (e.g., \"#abcdef\")": function(lab) { "parses hexadecimal format (e.g., \"#abcdef\")": function(d3) {
assert.labEqual(lab("#abcdef"), 81.04386565274363, -3.6627002800885267, -20.442705201854984); assert.labEqual(d3.lab("#abcdef"), 81.04386565274363, -3.6627002800885267, -20.442705201854984);
}, },
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(lab) { "parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(d3) {
assert.labEqual(lab("hsl(210, 64.7058%, 13.33333%)"), 12.65624852526134, 0.12256520883417721, -16.833209795877284); assert.labEqual(d3.lab("hsl(210, 64.7058%, 13.33333%)"), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
}, },
"parses color names (e.g., \"moccasin\")": function(lab) { "parses color names (e.g., \"moccasin\")": function(d3) {
assert.labEqual(lab("moccasin"), 91.72317744746022, 2.4393469358685027, 26.359832514614844); assert.labEqual(d3.lab("moccasin"), 91.72317744746022, 2.4393469358685027, 26.359832514614844);
}, },
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(lab) { "parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(d3) {
assert.labEqual(lab("rgb(102, 102, 0)"), 41.73251953866431, -10.998411255098816, 48.21006600604577); assert.labEqual(d3.lab("rgb(102, 102, 0)"), 41.73251953866431, -10.998411255098816, 48.21006600604577);
}, },
"can convert from RGB": function(lab) { "can convert from RGB": function(d3) {
assert.labEqual(lab(_.rgb(12, 34, 56)), 12.65624852526134, 0.12256520883417721, -16.833209795877284); assert.labEqual(d3.lab(d3.rgb(12, 34, 56)), 12.65624852526134, 0.12256520883417721, -16.833209795877284);
}, },
"can convert from HSL": function(lab) { "can convert from HSL": function(d3) {
assert.labEqual(lab(lab(20, .8, .3)), 20, 0.8, 0.3); assert.labEqual(d3.lab(d3.lab(20, .8, .3)), 20, 0.8, 0.3);
}, },
"can convert to RGB": function(lab) { "can convert to RGB": function(d3) {
assert.rgbEqual(lab("steelblue").rgb(), 70, 130, 180); assert.rgbEqual(d3.lab("steelblue").rgb(), 70, 130, 180);
}, },
"can derive a brighter color": function(lab) { "can convert from HCL": function(d3) {
assert.labEqual(lab("steelblue").brighter(), 70.46551718768575, -4.0774710123572255, -32.19186122981343); assert.labEqual(d3.lab(d3.hcl(-79.96, 40.25, 59.93)), 59.93, 7.02, -39.63);
assert.labEqual(lab("steelblue").brighter(.5), 61.46551718768575, -4.0774710123572255, -32.19186122981343);
}, },
"can derive a darker color": function(lab) { "can derive a brighter color": function(d3) {
assert.labEqual(lab("lightsteelblue").darker(), 60.45157936968134, -1.2815839134120433, -15.210996213841522); assert.labEqual(d3.lab("steelblue").brighter(), 70.46551718768575, -4.0774710123572255, -32.19186122981343);
assert.labEqual(lab("lightsteelblue").darker(.5), 69.45157936968134, -1.2815839134120433, -15.210996213841522); assert.labEqual(d3.lab("steelblue").brighter(.5), 61.46551718768575, -4.0774710123572255, -32.19186122981343);
}, },
"string coercion returns RGB format": function(lab) { "can derive a darker color": function(d3) {
assert.strictEqual(lab("hsl(60, 100%, 20%)") + "", "#666600"); assert.labEqual(d3.lab("lightsteelblue").darker(), 60.45157936968134, -1.2815839134120433, -15.210996213841522);
assert.strictEqual(lab(lab(60, -4, -32)) + "", "#5d95c8"); assert.labEqual(d3.lab("lightsteelblue").darker(.5), 69.45157936968134, -1.2815839134120433, -15.210996213841522);
}, },
"roundtrip to HSL is idempotent": function(lab) { "string coercion returns RGB format": function(d3) {
assert.deepEqual(_.hsl(lab("steelblue")), _.hsl("steelblue")); assert.strictEqual(d3.lab("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(d3.lab(d3.lab(60, -4, -32)) + "", "#5d95c8");
}, },
"roundtrip to RGB is idempotent": function(lab) { "roundtrip to HSL is idempotent": function(d3) {
assert.deepEqual(_.rgb(lab("steelblue")), _.rgb("steelblue")); assert.deepEqual(d3.hsl(d3.lab("steelblue")), d3.hsl("steelblue"));
}, },
"roundtrip to HCL is idempotent": function(lab) { "roundtrip to RGB is idempotent": function(d3) {
assert.deepEqual(_.hcl(lab("steelblue")), _.hcl("steelblue")); assert.deepEqual(d3.rgb(d3.lab("steelblue")), d3.rgb("steelblue"));
},
"roundtrip to HCL is idempotent": function(d3) {
assert.deepEqual(d3.hcl(d3.lab("steelblue")), d3.hcl("steelblue"));
} }
} }
}); });

Просмотреть файл

@ -1,5 +1,4 @@
var vows = require("vows"), var vows = require("vows"),
_ = require("../../"),
load = require("../load"), load = require("../load"),
assert = require("../assert"); assert = require("../assert");
@ -7,83 +6,83 @@ var suite = vows.describe("d3.rgb");
suite.addBatch({ suite.addBatch({
"rgb": { "rgb": {
topic: load("color/rgb").expression("d3.rgb"), topic: load("color/rgb", "color/hsl").expression("d3"),
"floors channel values": function(rgb) { "floors channel values": function(d3) {
assert.rgbEqual(rgb(1.2, 2.6, 42.9), 1, 2, 42); assert.rgbEqual(d3.rgb(1.2, 2.6, 42.9), 1, 2, 42);
}, },
"defaults to black for invalid inputs": function(rgb) { "defaults to black for invalid inputs": function(d3) {
assert.rgbEqual(rgb("invalid"), 0, 0, 0); assert.rgbEqual(d3.rgb("invalid"), 0, 0, 0);
assert.rgbEqual(rgb("hasOwnProperty"), 0, 0, 0); assert.rgbEqual(d3.rgb("hasOwnProperty"), 0, 0, 0);
assert.rgbEqual(rgb("__proto__"), 0, 0, 0); assert.rgbEqual(d3.rgb("__proto__"), 0, 0, 0);
}, },
"does not clamp channel values": function(rgb) { "does not clamp channel values": function(d3) {
assert.rgbEqual(rgb(-10, -20, -30), -10, -20, -30); assert.rgbEqual(d3.rgb(-10, -20, -30), -10, -20, -30);
assert.rgbEqual(rgb(300, 400, 500), 300, 400, 500); assert.rgbEqual(d3.rgb(300, 400, 500), 300, 400, 500);
}, },
"converts string channel values to numbers": function(rgb) { "converts string channel values to numbers": function(d3) {
assert.rgbEqual(rgb("12", "34", "56"), 12, 34, 56); assert.rgbEqual(d3.rgb("12", "34", "56"), 12, 34, 56);
}, },
"converts null channel values to zero": function(rgb) { "converts null channel values to zero": function(d3) {
assert.rgbEqual(rgb(null, null, null), 0, 0, 0); assert.rgbEqual(d3.rgb(null, null, null), 0, 0, 0);
}, },
"exposes r, g and b properties": function(rgb) { "exposes r, g and b properties": function(d3) {
var color = rgb("#abc"); var color = d3.rgb("#abc");
assert.equal(color.r, 170); assert.equal(color.r, 170);
assert.equal(color.g, 187); assert.equal(color.g, 187);
assert.equal(color.b, 204); assert.equal(color.b, 204);
}, },
"changing r, g or b affects the string format": function(rgb) { "changing r, g or b affects the string format": function(d3) {
var color = rgb("#abc"); var color = d3.rgb("#abc");
color.r++; color.r++;
color.g++; color.g++;
color.b++; color.b++;
assert.equal(color + "", "#abbccd"); assert.equal(color + "", "#abbccd");
}, },
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(rgb) { "parses hexadecimal shorthand format (e.g., \"#abc\")": function(d3) {
assert.rgbEqual(rgb("#abc"), 170, 187, 204); assert.rgbEqual(d3.rgb("#abc"), 170, 187, 204);
}, },
"parses hexadecimal format (e.g., \"#abcdef\")": function(rgb) { "parses hexadecimal format (e.g., \"#abcdef\")": function(d3) {
assert.rgbEqual(rgb("#abcdef"), 171, 205, 239); assert.rgbEqual(d3.rgb("#abcdef"), 171, 205, 239);
}, },
"parses RGB format (e.g., \"rgb(12, 34, 56)\")": function(rgb) { "parses RGB format (e.g., \"rgb(12, 34, 56)\")": function(d3) {
assert.rgbEqual(rgb("rgb(12, 34, 56)"), 12, 34, 56); assert.rgbEqual(d3.rgb("rgb(12, 34, 56)"), 12, 34, 56);
}, },
"parses color names (e.g., \"moccasin\")": function(rgb) { "parses color names (e.g., \"moccasin\")": function(d3) {
assert.rgbEqual(rgb("moccasin"), 255, 228, 181); assert.rgbEqual(d3.rgb("moccasin"), 255, 228, 181);
assert.rgbEqual(rgb("aliceblue"), 240, 248, 255); assert.rgbEqual(d3.rgb("aliceblue"), 240, 248, 255);
assert.rgbEqual(rgb("yellow"), 255, 255, 0); assert.rgbEqual(d3.rgb("yellow"), 255, 255, 0);
}, },
"parses and converts HSL format (e.g., \"hsl(60, 100%, 20%)\")": function(rgb) { "parses and converts HSL format (e.g., \"hsl(60, 100%, 20%)\")": function(d3) {
assert.rgbEqual(rgb("hsl(60, 100%, 20%)"), 102, 102, 0); assert.rgbEqual(d3.rgb("hsl(60, 100%, 20%)"), 102, 102, 0);
}, },
"can convert from RGB": function(rgb) { "can convert from RGB": function(d3) {
assert.rgbEqual(rgb(rgb(12, 34, 56)), 12, 34, 56); assert.rgbEqual(d3.rgb(d3.rgb(12, 34, 56)), 12, 34, 56);
}, },
"can convert from HSL": function(rgb) { "can convert from HSL": function(d3) {
assert.rgbEqual(rgb(_.hsl(0, 1, .5)), 255, 0, 0); assert.rgbEqual(d3.rgb(d3.hsl(0, 1, .5)), 255, 0, 0);
}, },
"can convert to HSL": function(rgb) { "can convert to HSL": function(d3) {
assert.hslEqual(rgb("red").hsl(), 0, 1, .5); assert.hslEqual(d3.rgb("red").hsl(), 0, 1, .5);
}, },
"can derive a brighter color": function(rgb) { "can derive a brighter color": function(d3) {
assert.rgbEqual(rgb("brown").brighter(), 235, 60, 60); assert.rgbEqual(d3.rgb("brown").brighter(), 235, 60, 60);
assert.rgbEqual(rgb("brown").brighter(.5), 197, 50, 50); assert.rgbEqual(d3.rgb("brown").brighter(.5), 197, 50, 50);
assert.rgbEqual(rgb("brown").brighter(1), 235, 60, 60); assert.rgbEqual(d3.rgb("brown").brighter(1), 235, 60, 60);
assert.rgbEqual(rgb("brown").brighter(2), 255, 85, 85); assert.rgbEqual(d3.rgb("brown").brighter(2), 255, 85, 85);
}, },
"can derive a darker color": function(rgb) { "can derive a darker color": function(d3) {
assert.rgbEqual(rgb("coral").darker(), 178, 88, 56); assert.rgbEqual(d3.rgb("coral").darker(), 178, 88, 56);
assert.rgbEqual(rgb("coral").darker(.5), 213, 106, 66); assert.rgbEqual(d3.rgb("coral").darker(.5), 213, 106, 66);
assert.rgbEqual(rgb("coral").darker(1), 178, 88, 56); assert.rgbEqual(d3.rgb("coral").darker(1), 178, 88, 56);
assert.rgbEqual(rgb("coral").darker(2), 124, 62, 39); assert.rgbEqual(d3.rgb("coral").darker(2), 124, 62, 39);
}, },
"string coercion returns hexadecimal format": function(rgb) { "string coercion returns hexadecimal format": function(d3) {
assert.strictEqual(rgb("#abcdef") + "", "#abcdef"); assert.strictEqual(d3.rgb("#abcdef") + "", "#abcdef");
assert.strictEqual(rgb("moccasin") + "", "#ffe4b5"); assert.strictEqual(d3.rgb("moccasin") + "", "#ffe4b5");
assert.strictEqual(rgb("hsl(60, 100%, 20%)") + "", "#666600"); assert.strictEqual(d3.rgb("hsl(60, 100%, 20%)") + "", "#666600");
assert.strictEqual(rgb("rgb(12, 34, 56)") + "", "#0c2238"); assert.strictEqual(d3.rgb("rgb(12, 34, 56)") + "", "#0c2238");
assert.strictEqual(rgb(rgb(12, 34, 56)) + "", "#0c2238"); assert.strictEqual(d3.rgb(d3.rgb(12, 34, 56)) + "", "#0c2238");
assert.strictEqual(rgb(_.hsl(60, 1, .2)) + "", "#666600"); assert.strictEqual(d3.rgb(d3.hsl(60, 1, .2)) + "", "#666600");
} }
} }
}); });