Skip to content

Commit

Permalink
Merge pull request #15 from rickyvetter/hsl-nan
Browse files Browse the repository at this point in the history
Check for 0 on hsl2hsv and tests
  • Loading branch information
MoOx committed Jun 2, 2015
2 parents 3e79182 + f18e41f commit ce5ee4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions conversions.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ function hsl2hsv(hsl) {
s = hsl[1] / 100,
l = hsl[2] / 100,
sv, v;

if(l === 0) {
// no need to do calc on black
// also avoids divide by 0 error
return [0, 0, 0];
}

l *= 2;
s *= (l <= 1) ? l : 2 - l;
v = (l + s) / 2;
Expand Down
8 changes: 8 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ assert.deepEqual(convert.hwb2rgb([240, 0, 0]), [0,0,255]);
assert.deepEqual(convert.hwb2rgb([240, 20, 40]), [51, 51, 153]);
assert.deepEqual(convert.hwb2rgb([240, 40, 40]), [102, 102, 153]);
assert.deepEqual(convert.hwb2rgb([240, 40, 20]), [102, 102, 204]);


// black should always stay black
val = [0, 0, 0];
assert.deepEqual(convert.hsl2hsv(val), val);
assert.deepEqual(convert.hsl2rgb(val), val);
assert.deepEqual(convert.hsl2hwb(val), [0, 0, 100]);
assert.deepEqual(convert.hsl2cmyk(val), [0, 0, 0, 100]);

0 comments on commit ce5ee4b

Please sign in to comment.