simonwep / pickr

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap, Materialize. Supports alpha channel, rgba, hsla, hsva and more!
https://simonwep.github.io/pickr
MIT License
4.31k stars 285 forks source link

Fix `hslToHsv` with 0 saturation and lightness #206

Closed DEfusion closed 4 years ago

DEfusion commented 4 years ago

When given a hsl value with both 0 for the saturation and lightness (i.e. black; hsl(0, 0%, 0%)) that would result in a division by 0 in the conversion resulting in NaN for the saturation for the hsv result.

This would most easily manifest itself if a swatch were set with the hsl value for black, which would generate an invalid color e.g. #00NANNAN for the hex representation:

Screenshot 2020-04-23 at 11 47 04

This change fixes that by setting it to 0 when the new saturation results in NaN.

simonwep commented 4 years ago

Thanks for the fix! Theoretically it could by done even simpler as NaN is falsy a val || 0 would've been sufficient :D

Note that normally you'd use Number.isNaN because window.IsNaN only checks whether the value is not a number and Number.isNaN actually checks for NaN, see this SO Answer.