Skip to content

Commit

Permalink
Merge branch 'fix-hcl-lab' into 3.4.13
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Oct 17, 2014
2 parents a18b3f3 + b6b80f7 commit 4c62314
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 260 deletions.
2 changes: 1 addition & 1 deletion d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@
}
d3.lab = d3_lab;
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_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883;
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/color/lab.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ d3.lab = d3_lab;
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)
: (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);
}
Expand Down
134 changes: 68 additions & 66 deletions test/color/hcl-test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
var vows = require("vows"),
_ = require("../../"),
load = require("../load"),
assert = require("../assert");

var suite = vows.describe("d3.hcl");

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

0 comments on commit 4c62314

Please sign in to comment.