Skip to content

Commit

Permalink
Saturation is only undefined for black and white.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Apr 30, 2013
1 parent 1d0409e commit 0c7d186
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1442,11 +1442,14 @@ d3 = function() {
return rgb(r, g, b);
}
function d3_rgb_hsl(r, g, b) {
var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h = NaN, s = h, l = (max + min) / 2;
var min = Math.min(r /= 255, g /= 255, b /= 255), max = Math.max(r, g, b), d = max - min, h, s, l = (max + min) / 2;
if (d) {
s = l < .5 ? d / (max + min) : d / (2 - max - min);
if (r == max) h = (g - b) / d + (g < b ? 6 : 0); else if (g == max) h = (b - r) / d + 2; else h = (r - g) / d + 4;
h *= 60;
} else {
h = NaN;
s = l > 0 && l < 1 ? 0 : h;
}
return d3_hsl(h, s, l);
}
Expand Down
2 changes: 1 addition & 1 deletion d3.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/color/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ function d3_rgb_hsl(r, g, b) {
var min = Math.min(r /= 255, g /= 255, b /= 255),
max = Math.max(r, g, b),
d = max - min,
h = NaN,
s = h,
h,
s,
l = (max + min) / 2;
if (d) {
s = l < .5 ? d / (max + min) : d / (2 - max - min);
if (r == max) h = (g - b) / d + (g < b ? 6 : 0);
else if (g == max) h = (b - r) / d + 2;
else h = (r - g) / d + 4;
h *= 60;
} else {
h = NaN;
s = l > 0 && l < 1 ? 0 : h;
}
return d3_hsl(h, s, l);
}
Expand Down
4 changes: 4 additions & 0 deletions test/color/hsl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ suite.addBatch({
assert.strictEqual(hsl(0, .42, 1).s, .42);
assert.strictEqual(hsl(0, 1, 1).s, 1);
},
"s is zero for grayscale colors (but not white and black)": function(hsl) {
assert.strictEqual(hsl("#ccc").s, 0);
assert.strictEqual(hsl("#777").s, 0);
},
"s is undefined when not explicitly specified for white or black": function(hsl) {
assert.isNaN(hsl("#000").s);
assert.isNaN(hsl("black").s);
Expand Down

0 comments on commit 0c7d186

Please sign in to comment.