Skip to content

Commit e06eacd

Browse files
committed
Ignore case when detecting colors for interpolation.
Fixes d3#2359.
1 parent 3abb001 commit e06eacd

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

d3.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1699,8 +1699,9 @@
16991699
return v < 16 ? "0" + Math.max(0, v).toString(16) : Math.min(255, v).toString(16);
17001700
}
17011701
function d3_rgb_parse(format, rgb, hsl) {
1702+
format = format.toLowerCase();
17021703
var r = 0, g = 0, b = 0, m1, m2, color;
1703-
m1 = /([a-z]+)\((.*)\)/i.exec(format);
1704+
m1 = /([a-z]+)\((.*)\)/.exec(format);
17041705
if (m1) {
17051706
m2 = m1[2].split(",");
17061707
switch (m1[1]) {
@@ -1715,7 +1716,7 @@
17151716
}
17161717
}
17171718
}
1718-
if (color = d3_rgb_names.get(format.toLowerCase())) {
1719+
if (color = d3_rgb_names.get(format)) {
17191720
return rgb(color.r, color.g, color.b);
17201721
}
17211722
if (format != null && format.charAt(0) === "#" && !isNaN(color = parseInt(format.slice(1), 16))) {
@@ -5785,7 +5786,7 @@
57855786
}
57865787
d3.interpolators = [ function(a, b) {
57875788
var t = typeof b;
5788-
return (t === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b);
5789+
return (t === "string" ? d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString : b instanceof d3_color ? d3_interpolateRgb : Array.isArray(b) ? d3_interpolateArray : t === "object" && isNaN(b) ? d3_interpolateObject : d3_interpolateNumber)(a, b);
57895790
} ];
57905791
d3.interpolateArray = d3_interpolateArray;
57915792
function d3_interpolateArray(a, b) {

d3.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/color/rgb.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function d3_rgb_hex(v) {
5656
}
5757

5858
function d3_rgb_parse(format, rgb, hsl) {
59+
format = format.toLowerCase();
5960
var r = 0, // red channel; int in [0, 255]
6061
g = 0, // green channel; int in [0, 255]
6162
b = 0, // blue channel; int in [0, 255]
@@ -64,7 +65,7 @@ function d3_rgb_parse(format, rgb, hsl) {
6465
color;
6566

6667
/* Handle hsl, rgb. */
67-
m1 = /([a-z]+)\((.*)\)/i.exec(format);
68+
m1 = /([a-z]+)\((.*)\)/.exec(format);
6869
if (m1) {
6970
m2 = m1[2].split(",");
7071
switch (m1[1]) {
@@ -86,7 +87,7 @@ function d3_rgb_parse(format, rgb, hsl) {
8687
}
8788

8889
/* Named colors. */
89-
if (color = d3_rgb_names.get(format.toLowerCase())) {
90+
if (color = d3_rgb_names.get(format)) {
9091
return rgb(color.r, color.g, color.b);
9192
}
9293

src/interpolate/interpolate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function d3_interpolate(a, b) {
1717
d3.interpolators = [
1818
function(a, b) {
1919
var t = typeof b;
20-
return (t === "string" ? (d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) ? d3_interpolateRgb : d3_interpolateString)
20+
return (t === "string" ? (d3_rgb_names.has(b.toLowerCase()) || /^(#|rgb\(|hsl\()/i.test(b) ? d3_interpolateRgb : d3_interpolateString)
2121
: b instanceof d3_color ? d3_interpolateRgb
2222
: Array.isArray(b) ? d3_interpolateArray
2323
: t === "object" && isNaN(b) ? d3_interpolateObject

test/interpolate/interpolate-test.js

+9
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,21 @@ suite.addBatch({
2626
"interpolates named colors in RGB": function(d3) {
2727
assert.strictEqual(d3.interpolate("red", "green")(.4), "#993300");
2828
},
29+
"interpolates case-insensitive named colors in RGB": function(d3) {
30+
assert.strictEqual(d3.interpolate("ReD", "GrEeN")(.4), "#993300");
31+
},
2932
"interpolates decimal RGB colors in RGB": function(d3) {
3033
assert.strictEqual(d3.interpolate("rgb(255,0,0)", "rgb(0,128,0)")(.4), "#993300");
3134
},
35+
"interpolates case-insensitive decimal RGB colors in RGB": function(d3) {
36+
assert.strictEqual(d3.interpolate("RgB(255,0,0)", "rGb(0,128,0)")(.4), "#993300");
37+
},
3238
"interpolates decimal HSL colors in RGB": function(d3) {
3339
assert.strictEqual(d3.interpolate("hsl(0,100%,50%)", "hsl(120,100%,25%)")(.4), "#993300");
3440
},
41+
"interpolates case-insensitive decimal HSL colors in RGB": function(d3) {
42+
assert.strictEqual(d3.interpolate("HsL(0,100%,50%)", "hSl(120,100%,25%)")(.4), "#993300");
43+
},
3544
"coerces a to a color": function(d3) {
3645
assert.strictEqual(d3.interpolate({toString: function() { return "red"; }}, "green")(.4), "#993300");
3746
}

0 commit comments

Comments
 (0)