Saturation is only undefined for black and white.

This commit is contained in:
Mike Bostock 2013-04-30 16:09:40 -07:00
Родитель 1d0409e903
Коммит 0c7d1864f2
4 изменённых файлов: 14 добавлений и 4 удалений

5
d3.js поставляемый
Просмотреть файл

@ -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);
}

2
d3.min.js поставляемый

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -117,8 +117,8 @@ 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);
@ -126,6 +126,9 @@ function d3_rgb_hsl(r, g, b) {
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);
}

Просмотреть файл

@ -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);