2012-08-09 08:22:21 +04:00
|
|
|
var vows = require("vows"),
|
2013-03-15 02:14:12 +04:00
|
|
|
_ = require("../../"),
|
2013-03-14 06:13:38 +04:00
|
|
|
load = require("../load"),
|
2013-03-14 23:59:54 +04:00
|
|
|
assert = require("../assert");
|
2012-08-09 08:22:21 +04:00
|
|
|
|
|
|
|
var suite = vows.describe("d3.hcl");
|
|
|
|
|
|
|
|
suite.addBatch({
|
|
|
|
"hcl": {
|
2013-03-14 20:06:31 +04:00
|
|
|
topic: load("color/hcl").expression("d3.hcl"),
|
|
|
|
"converts string channel values to numbers": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("50", "-4", "32"), 50, -4, 32);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"converts null channel values to zero": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl(null, null, null), 0, 0, 0);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"exposes h, c and l properties": function(hcl) {
|
|
|
|
var color = hcl(50, -4, 32);
|
2012-08-09 08:22:21 +04:00
|
|
|
assert.equal(color.h, 50);
|
|
|
|
assert.equal(color.c, -4);
|
|
|
|
assert.equal(color.l, 32);
|
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"changing h, c or l affects the string format": function(hcl) {
|
|
|
|
var color = hcl(50, -4, 32);
|
2012-08-09 08:22:21 +04:00
|
|
|
assert.equal(color + "", "#444d50");
|
|
|
|
color.h++;
|
|
|
|
assert.equal(color + "", "#444d50");
|
|
|
|
color.c++;
|
|
|
|
assert.equal(color + "", "#464c4f");
|
|
|
|
color.l++;
|
|
|
|
assert.equal(color + "", "#494f51");
|
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"parses hexadecimal format (e.g., \"#abcdef\")": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"parses color names (e.g., \"moccasin\")": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"can convert from RGB": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl(_.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"can convert from HSL": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl(hcl(20, .8, .3)), 20, 0.8, 0.3);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"can convert to RGB": function(hcl) {
|
|
|
|
assert.rgbEqual(hcl("steelblue").rgb(), 70, 130, 180);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"can derive a brighter color": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
|
|
|
|
assert.hclEqual(hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"can derive a darker color": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.hclEqual(hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
|
|
|
|
assert.hclEqual(hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"string coercion returns RGB format": function(hcl) {
|
|
|
|
assert.strictEqual(hcl("hsl(60, 100%, 20%)") + "", "#666600");
|
|
|
|
assert.strictEqual(hcl(hcl(60, -4, 32)) + "", "#454c51");
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"roundtrip to HSL is idempotent": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.deepEqual(_.hsl(hcl("steelblue")), _.hsl("steelblue"));
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"roundtrip to RGB is idempotent": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.deepEqual(_.rgb(hcl("steelblue")), _.rgb("steelblue"));
|
2012-08-09 08:22:21 +04:00
|
|
|
},
|
2013-03-14 20:06:31 +04:00
|
|
|
"roundtrip to Lab is idempotent": function(hcl) {
|
2013-07-03 20:52:28 +04:00
|
|
|
assert.deepEqual(_.lab(hcl("steelblue")), _.lab("steelblue"));
|
2013-04-19 21:20:21 +04:00
|
|
|
},
|
|
|
|
"h is defined for non-black grayscale colors (because of the color profile)": function(hcl) {
|
|
|
|
assert.inDelta(hcl("#ccc").h, 158.1986, 1e-3);
|
|
|
|
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) {
|
|
|
|
assert.strictEqual(hcl(0, 0, 0).h, 0);
|
|
|
|
assert.strictEqual(hcl(42, 0, 0).h, 42);
|
|
|
|
assert.strictEqual(hcl(118, 0, 0).h, 118);
|
|
|
|
},
|
|
|
|
"h is undefined when not explicitly specified for black": function(hcl) {
|
|
|
|
assert.isNaN(hcl("#000").h);
|
|
|
|
assert.isNaN(hcl("black").h);
|
|
|
|
assert.isNaN(hcl(_.rgb("black")).h);
|
|
|
|
},
|
|
|
|
"c is preserved when explicitly specified, even for black": function(hcl) {
|
|
|
|
assert.strictEqual(hcl(0, 0, 0).c, 0);
|
|
|
|
assert.strictEqual(hcl(0, .42, 0).c, .42);
|
|
|
|
assert.strictEqual(hcl(0, 1, 0).c, 1);
|
|
|
|
},
|
|
|
|
"c is undefined when not explicitly specified for black": function(hcl) {
|
|
|
|
assert.isNaN(hcl("#000").c);
|
|
|
|
assert.isNaN(hcl("black").c);
|
|
|
|
assert.isNaN(hcl(_.rgb("black")).c);
|
|
|
|
},
|
|
|
|
"can convert black (with undefined hue and chroma) to RGB": function(hcl) {
|
|
|
|
assert.strictEqual(hcl(NaN, NaN, 0) + "", "#000000");
|
2012-08-09 08:22:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
suite.export(module);
|