Skip to content

Commit ae1fed7

Browse files
committed
Use shortest path for hue in d3.interpolateHsl.
Fixes d3#688.
1 parent e71de33 commit ae1fed7

9 files changed

Lines changed: 22 additions & 20 deletions

File tree

d3.v2.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ d3.interpolateHsl = function(a, b) {
10891089
h1 = b.h - h0,
10901090
s1 = b.s - s0,
10911091
l1 = b.l - l0;
1092+
if (h1 > 180) h1 -= 360; else if (h1 < -180) h1 += 360; // shortest path
10921093
return function(t) {
10931094
return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
10941095
};

d3.v2.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/interpolate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ d3.interpolateHsl = function(a, b) {
171171
h1 = b.h - h0,
172172
s1 = b.s - s0,
173173
l1 = b.l - l0;
174+
if (h1 > 180) h1 -= 360; else if (h1 < -180) h1 += 360; // shortest path
174175
return function(t) {
175176
return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
176177
};

test/core/interpolate-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,22 @@ suite.addBatch({
143143
return d3.interpolateHsl;
144144
},
145145
"parses string input": function(interpolate) {
146-
assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");
147-
assert.equal(interpolate("steelblue", "#f00")(.6), "#96e11c");
146+
assert.equal(interpolate("steelblue", "#f00")(.2), "#383dc3");
147+
assert.equal(interpolate("steelblue", "#f00")(.6), "#dd1ce1");
148148
},
149149
"parses d3.hsl input": function(interpolate) {
150-
assert.equal(interpolate(d3.hsl("steelblue"), "#f00")(.2), "#38c3a2");
151-
assert.equal(interpolate("steelblue", d3.hsl(0, 1, .5))(.6), "#96e11c");
150+
assert.equal(interpolate(d3.hsl("steelblue"), "#f00")(.2), "#383dc3");
151+
assert.equal(interpolate("steelblue", d3.hsl(0, 1, .5))(.6), "#dd1ce1");
152152
},
153153
"parses d3.rgb input": function(interpolate) {
154-
assert.equal(interpolate(d3.rgb("steelblue"), "#f00")(.2), "#38c3a2");
155-
assert.equal(interpolate("steelblue", d3.rgb(255, 0, 0))(.6), "#96e11c");
154+
assert.equal(interpolate(d3.rgb("steelblue"), "#f00")(.2), "#383dc3");
155+
assert.equal(interpolate("steelblue", d3.rgb(255, 0, 0))(.6), "#dd1ce1");
156156
},
157157
"interpolates in HSL color space": function(interpolate) {
158-
assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");
158+
assert.equal(interpolate("steelblue", "#f00")(.2), "#383dc3");
159159
},
160160
"outputs a hexadecimal string": function(interpolate) {
161-
assert.equal(interpolate("steelblue", "#f00")(.2), "#38c3a2");
161+
assert.equal(interpolate("steelblue", "#f00")(.2), "#383dc3");
162162
}
163163
}
164164
});

test/scale/linear-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ suite.addBatch({
9797
},
9898
"can specify a custom interpolator": function(linear) {
9999
var x = linear().range(["red", "blue"]).interpolate(d3.interpolateHsl);
100-
assert.equal(x(.5), "#00ff00");
100+
assert.equal(x(.5), "#ff00ff");
101101
}
102102
},
103103

@@ -252,7 +252,7 @@ suite.addBatch({
252252
"changes to the interpolator are isolated": function(linear) {
253253
var x = linear().range(["red", "blue"]), y = x.copy();
254254
x.interpolate(d3.interpolateHsl);
255-
assert.equal(x(0.5), "#00ff00");
255+
assert.equal(x(0.5), "#ff00ff");
256256
assert.equal(y(0.5), "#800080");
257257
assert.equal(y.interpolate(), d3.interpolate);
258258
},

test/scale/log-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ suite.addBatch({
9292
},
9393
"can specify a custom interpolator": function(log) {
9494
var x = log().range(["red", "blue"]).interpolate(d3.interpolateHsl);
95-
assert.equal(x(5), "#00ffcb");
95+
assert.equal(x(5), "#9a00ff");
9696
}
9797
},
9898

@@ -269,7 +269,7 @@ suite.addBatch({
269269
"changes to the interpolator are isolated": function(log) {
270270
var x = log().range(["red", "blue"]), y = x.copy();
271271
x.interpolate(d3.interpolateHsl);
272-
assert.equal(x(5), "#00ffcb");
272+
assert.equal(x(5), "#9a00ff");
273273
assert.equal(y(5), "#4d00b2");
274274
assert.equal(y.interpolate(), d3.interpolate);
275275
},

test/scale/pow-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ suite.addBatch({
127127
},
128128
"can specify a custom interpolator": function(pow) {
129129
var x = pow().range(["red", "blue"]).interpolate(d3.interpolateHsl);
130-
assert.equal(x(.5), "#00ff00");
130+
assert.equal(x(.5), "#ff00ff");
131131
}
132132
},
133133

@@ -240,7 +240,7 @@ suite.addBatch({
240240
"changes to the interpolator are isolated": function(pow) {
241241
var x = pow().range(["red", "blue"]), y = x.copy();
242242
x.interpolate(d3.interpolateHsl);
243-
assert.equal(x(0.5), "#00ff00");
243+
assert.equal(x(0.5), "#ff00ff");
244244
assert.equal(y(0.5), "#800080");
245245
assert.equal(y.interpolate(), d3.interpolate);
246246
},

test/scale/sqrt-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ suite.addBatch({
124124
},
125125
"can specify a custom interpolator": function(sqrt) {
126126
var x = sqrt().range(["red", "blue"]).interpolate(d3.interpolateHsl);
127-
assert.equal(x(.25), "#00ff00");
127+
assert.equal(x(.25), "#ff00ff");
128128
}
129129
},
130130

@@ -233,7 +233,7 @@ suite.addBatch({
233233
"changes to the interpolator are isolated": function(sqrt) {
234234
var x = sqrt().range(["red", "blue"]), y = x.copy();
235235
x.interpolate(d3.interpolateHsl);
236-
assert.equal(x(0.5), "#00ffd3");
236+
assert.equal(x(0.5), "#9500ff");
237237
assert.equal(y(0.5), "#4b00b4");
238238
assert.equal(y.interpolate(), d3.interpolate);
239239
},

test/time/scale-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ suite.addBatch({
4242
"changes to the interpolator are isolated": function(scale) {
4343
var x = scale().domain([local(2009, 0, 1), local(2010, 0, 1)]).range(["red", "blue"]), y = x.copy();
4444
x.interpolate(d3.interpolateHsl);
45-
assert.equal(x(local(2009, 6, 1)), "#04ff00");
45+
assert.equal(x(local(2009, 6, 1)), "#ff00fd");
4646
assert.equal(y(local(2009, 6, 1)), "#81007e");
4747
assert.equal(y.interpolate(), d3.interpolate);
4848
},

0 commit comments

Comments
 (0)