Skip to content

Commit b6b80f7

Browse files
committed
Fix d3.lab(d3.hcl(…)).
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.
1 parent 1fc660a commit b6b80f7

7 files changed

Lines changed: 262 additions & 260 deletions

File tree

d3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@
15481548
}
15491549
d3.lab = d3_lab;
15501550
function d3_lab(l, a, b) {
1551-
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);
1551+
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);
15521552
}
15531553
var d3_lab_K = 18;
15541554
var d3_lab_X = .95047, d3_lab_Y = 1, d3_lab_Z = 1.08883;

d3.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/color/lab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ d3.lab = d3_lab;
88
function d3_lab(l, a, b) {
99
return this instanceof d3_lab ? void (this.l = +l, this.a = +a, this.b = +b)
1010
: arguments.length < 2 ? (l instanceof d3_lab ? new d3_lab(l.l, l.a, l.b)
11-
: (l instanceof d3_hcl ? d3_hcl_lab(l.l, l.c, l.h)
11+
: (l instanceof d3_hcl ? d3_hcl_lab(l.h, l.c, l.l)
1212
: d3_rgb_lab((l = d3_rgb(l)).r, l.g, l.b)))
1313
: new d3_lab(l, a, b);
1414
}

test/color/hcl-test.js

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
var vows = require("vows"),
2-
_ = require("../../"),
32
load = require("../load"),
43
assert = require("../assert");
54

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

87
suite.addBatch({
98
"hcl": {
10-
topic: load("color/hcl").expression("d3.hcl"),
11-
"converts string channel values to numbers": function(hcl) {
12-
assert.hclEqual(hcl("50", "-4", "32"), 50, -4, 32);
9+
topic: load("color/hcl", "color/lab", "color/rgb").expression("d3"),
10+
"converts string channel values to numbers": function(d3) {
11+
assert.hclEqual(d3.hcl("50", "-4", "32"), 50, -4, 32);
1312
},
14-
"converts null channel values to zero": function(hcl) {
15-
assert.hclEqual(hcl(null, null, null), 0, 0, 0);
13+
"converts null channel values to zero": function(d3) {
14+
assert.hclEqual(d3.hcl(null, null, null), 0, 0, 0);
1615
},
17-
"exposes h, c and l properties": function(hcl) {
18-
var color = hcl(50, -4, 32);
16+
"exposes h, c and l properties": function(d3) {
17+
var color = d3.hcl(50, -4, 32);
1918
assert.equal(color.h, 50);
2019
assert.equal(color.c, -4);
2120
assert.equal(color.l, 32);
2221
},
23-
"changing h, c or l affects the string format": function(hcl) {
24-
var color = hcl(50, -4, 32);
22+
"changing h, c or l affects the string format": function(d3) {
23+
var color = d3.hcl(50, -4, 32);
2524
assert.equal(color + "", "#444d50");
2625
color.h++;
2726
assert.equal(color + "", "#444d50");
@@ -30,81 +29,84 @@ suite.addBatch({
3029
color.l++;
3130
assert.equal(color + "", "#494f51");
3231
},
33-
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(hcl) {
34-
assert.hclEqual(hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
32+
"parses hexadecimal shorthand format (e.g., \"#abc\")": function(d3) {
33+
assert.hclEqual(d3.hcl("#abc"), -102.28223831811077, 10.774886733325554, 75.10497524893663);
3534
},
36-
"parses hexadecimal format (e.g., \"#abcdef\")": function(hcl) {
37-
assert.hclEqual(hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
35+
"parses hexadecimal format (e.g., \"#abcdef\")": function(d3) {
36+
assert.hclEqual(d3.hcl("#abcdef"), -100.15785184209284, 20.768234621934273, 81.04386565274363);
3837
},
39-
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(hcl) {
40-
assert.hclEqual(hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
38+
"parses HSL format (e.g., \"hsl(210, 64%, 13%)\")": function(d3) {
39+
assert.hclEqual(d3.hcl("hsl(210, 64.7058%, 13.33333%)"), -89.58282792342067, 16.833655998102003, 12.65624852526134);
4140
},
42-
"parses color names (e.g., \"moccasin\")": function(hcl) {
43-
assert.hclEqual(hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
41+
"parses color names (e.g., \"moccasin\")": function(d3) {
42+
assert.hclEqual(d3.hcl("moccasin"), 84.71288921124494, 26.472460854104156, 91.72317744746022);
4443
},
45-
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(hcl) {
46-
assert.hclEqual(hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
44+
"parses and converts RGB format (e.g., \"rgb(102, 102, 0)\")": function(d3) {
45+
assert.hclEqual(d3.hcl("rgb(102, 102, 0)"), 102.85124420310271, 49.44871600399321, 41.73251953866431);
4746
},
48-
"can convert from RGB": function(hcl) {
49-
assert.hclEqual(hcl(_.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
47+
"can convert from RGB": function(d3) {
48+
assert.hclEqual(d3.hcl(d3.rgb(12, 34, 56)), -89.58282792342067, 16.833655998102003, 12.65624852526134);
5049
},
51-
"can convert from HSL": function(hcl) {
52-
assert.hclEqual(hcl(hcl(20, .8, .3)), 20, 0.8, 0.3);
50+
"can convert from HSL": function(d3) {
51+
assert.hclEqual(d3.hcl(d3.hcl(20, .8, .3)), 20, 0.8, 0.3);
5352
},
54-
"can convert to RGB": function(hcl) {
55-
assert.rgbEqual(hcl("steelblue").rgb(), 70, 130, 180);
53+
"can convert to RGB": function(d3) {
54+
assert.rgbEqual(d3.hcl("steelblue").rgb(), 70, 130, 180);
5655
},
57-
"can derive a brighter color": function(hcl) {
58-
assert.hclEqual(hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
59-
assert.hclEqual(hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
56+
"can convert from Lab": function(d3) {
57+
assert.hclEqual(d3.hcl(d3.lab(59.93, 7.02, -39.63)), -79.95, 40.25, 59.93);
6058
},
61-
"can derive a darker color": function(hcl) {
62-
assert.hclEqual(hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
63-
assert.hclEqual(hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
59+
"can derive a brighter color": function(d3) {
60+
assert.hclEqual(d3.hcl("steelblue").brighter(), -97.21873224090723, 32.44906314974561, 70.46551718768575);
61+
assert.hclEqual(d3.hcl("steelblue").brighter(.5), -97.21873224090723, 32.44906314974561, 61.46551718768575);
6462
},
65-
"string coercion returns RGB format": function(hcl) {
66-
assert.strictEqual(hcl("hsl(60, 100%, 20%)") + "", "#666600");
67-
assert.strictEqual(hcl(hcl(60, -4, 32)) + "", "#454c51");
63+
"can derive a darker color": function(d3) {
64+
assert.hclEqual(d3.hcl("lightsteelblue").darker(), -94.8160116310511, 15.26488988314746, 60.45157936968134);
65+
assert.hclEqual(d3.hcl("lightsteelblue").darker(.5), -94.8160116310511, 15.26488988314746, 69.45157936968134);
6866
},
69-
"roundtrip to HSL is idempotent": function(hcl) {
70-
assert.deepEqual(_.hsl(hcl("steelblue")), _.hsl("steelblue"));
67+
"string coercion returns RGB format": function(d3) {
68+
assert.strictEqual(d3.hcl("hsl(60, 100%, 20%)") + "", "#666600");
69+
assert.strictEqual(d3.hcl(d3.hcl(60, -4, 32)) + "", "#454c51");
7170
},
72-
"roundtrip to RGB is idempotent": function(hcl) {
73-
assert.deepEqual(_.rgb(hcl("steelblue")), _.rgb("steelblue"));
71+
"roundtrip to HSL is idempotent": function(d3) {
72+
assert.deepEqual(d3.hsl(d3.hcl("steelblue")), d3.hsl("steelblue"));
7473
},
75-
"roundtrip to Lab is idempotent": function(hcl) {
76-
assert.deepEqual(_.lab(hcl("steelblue")), _.lab("steelblue"));
74+
"roundtrip to RGB is idempotent": function(d3) {
75+
assert.deepEqual(d3.rgb(d3.hcl("steelblue")), d3.rgb("steelblue"));
7776
},
78-
"h is defined for non-black grayscale colors (because of the color profile)": function(hcl) {
79-
assert.inDelta(hcl("#ccc").h, 158.1986, 1e-3);
80-
assert.inDelta(hcl("gray").h, 158.1986, 1e-3);
81-
assert.inDelta(hcl(_.rgb("gray")).h, 158.1986, 1e-3);
82-
assert.inDelta(hcl("#fff").h, 158.1986, 1e-3);
83-
assert.inDelta(hcl("white").h, 158.1986, 1e-3);
84-
assert.inDelta(hcl(_.rgb("white")).h, 158.1986, 1e-3);
77+
"roundtrip to Lab is idempotent": function(d3) {
78+
assert.labEqual(d3.lab(d3.hcl("steelblue")), 52.47, -4.08, -32.19);
8579
},
86-
"h is preserved when explicitly specified, even for black": function(hcl) {
87-
assert.strictEqual(hcl(0, 0, 0).h, 0);
88-
assert.strictEqual(hcl(42, 0, 0).h, 42);
89-
assert.strictEqual(hcl(118, 0, 0).h, 118);
80+
"h is defined for non-black grayscale colors (because of the color profile)": function(d3) {
81+
assert.inDelta(d3.hcl("#ccc").h, 158.1986, 1e-3);
82+
assert.inDelta(d3.hcl("gray").h, 158.1986, 1e-3);
83+
assert.inDelta(d3.hcl(d3.rgb("gray")).h, 158.1986, 1e-3);
84+
assert.inDelta(d3.hcl("#fff").h, 158.1986, 1e-3);
85+
assert.inDelta(d3.hcl("white").h, 158.1986, 1e-3);
86+
assert.inDelta(d3.hcl(d3.rgb("white")).h, 158.1986, 1e-3);
9087
},
91-
"h is undefined when not explicitly specified for black": function(hcl) {
92-
assert.isNaN(hcl("#000").h);
93-
assert.isNaN(hcl("black").h);
94-
assert.isNaN(hcl(_.rgb("black")).h);
88+
"h is preserved when explicitly specified, even for black": function(d3) {
89+
assert.strictEqual(d3.hcl(0, 0, 0).h, 0);
90+
assert.strictEqual(d3.hcl(42, 0, 0).h, 42);
91+
assert.strictEqual(d3.hcl(118, 0, 0).h, 118);
9592
},
96-
"c is preserved when explicitly specified, even for black": function(hcl) {
97-
assert.strictEqual(hcl(0, 0, 0).c, 0);
98-
assert.strictEqual(hcl(0, .42, 0).c, .42);
99-
assert.strictEqual(hcl(0, 1, 0).c, 1);
93+
"h is undefined when not explicitly specified for black": function(d3) {
94+
assert.isNaN(d3.hcl("#000").h);
95+
assert.isNaN(d3.hcl("black").h);
96+
assert.isNaN(d3.hcl(d3.rgb("black")).h);
10097
},
101-
"c is undefined when not explicitly specified for black": function(hcl) {
102-
assert.isNaN(hcl("#000").c);
103-
assert.isNaN(hcl("black").c);
104-
assert.isNaN(hcl(_.rgb("black")).c);
98+
"c is preserved when explicitly specified, even for black": function(d3) {
99+
assert.strictEqual(d3.hcl(0, 0, 0).c, 0);
100+
assert.strictEqual(d3.hcl(0, .42, 0).c, .42);
101+
assert.strictEqual(d3.hcl(0, 1, 0).c, 1);
105102
},
106-
"can convert black (with undefined hue and chroma) to RGB": function(hcl) {
107-
assert.strictEqual(hcl(NaN, NaN, 0) + "", "#000000");
103+
"c is undefined when not explicitly specified for black": function(d3) {
104+
assert.isNaN(d3.hcl("#000").c);
105+
assert.isNaN(d3.hcl("black").c);
106+
assert.isNaN(d3.hcl(d3.rgb("black")).c);
107+
},
108+
"can convert black (with undefined hue and chroma) to RGB": function(d3) {
109+
assert.strictEqual(d3.hcl(NaN, NaN, 0) + "", "#000000");
108110
}
109111
}
110112
});

0 commit comments

Comments
 (0)