-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HSL algorithm can divide by zero #74
Comments
I've found that simply checking for whether Based on the algorithm of > (1.0 - Math.abs(2.0 * 5.204170427930421e-18 - 1))
0 As > Math.min(5.204170427930421e-18, 1 - 5.204170427930421e-18)
5.204170427930421e-18 Coupled with the saturation check ( |
Yes, that does seem like a nice refactoring. Please feel free to make a PR! This change will also impact HWB, right? |
I'm not sure it makes much of a difference, but I personally do in my library.
In my library it did as I specifically, when going from HWB to HSL, go HWB -> sRGB -> HSL. A matter of fact, basically all my HWB conversions go through sRGB when going to/from HSL or HWB. But Colorjs.io goes from HWB -> HSV -> HSL and that path doesn't have a problem with divide by zero as far as I can tell. With that said, Colorjs.io does take a path mine does not which looks like it might have issues, particularly this line hsv (hwb) {
let [h, w, b] = hwb;
// Now convert percentages to [0..1]
w /= 100;
b /= 100;
// Achromatic check (white plus black >= 1)
let sum = w + b;
if (sum >= 1) {
let gray = w / sum;
return [h, 0, gray];
}
let v = 1 - b;
let s = 100 - (100 * w) / (100 - b);
return [h, s, v * 100];
}, To pull off the divide by zero, the case is pretty contrived, but possible: > new Color("hwb(0 -10000% 10000%)").to('hsv').coords
[0,Infinity,-9900] Because I pass through HWB -> sRGB -> HSV, I don't see this issue, but with that said, after you asked this, I ran a quick test, and I don't know that the HWB to HSV conversion is correct in Colorjs.io. Ignoring hue, you can see that > new Color("hwb(37 10% 100%)").to('hsv').coords;
[37,0,0.091]
> new Color("hwb(37 10% 100%)").to('srgb').to('hsv').coords
[300,0,9.091] |
Yeah, I think this confirms HWB to HSV is not right. Roundtrip is broken. > new Color("hwb(37 20% 80%)").to('hsv').to('hwb').coords
[37,0.2,99.8] |
I think I know what the issue is with HWB as well. I can probably fix it and address its divide by zero case as well. I'll submit some pulls tomorrow. |
All known issues have been fixed in #79. Changes can be reviewed there. I made sure, as discussed, that R, G, B are in the range [0, 1] as well. |
Is there a reason the HSL algorithm does not implement the part relating to when
L == 0 or L == 1
found on the wiki: https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB?The
L == 1
is the part that is of particular concern as it causes a divide by zero:Will yield:
I'm not sure if this is an oversight or purposeful, and a clean methodology for handling the case has not been outlined yet.
EDIT: Correction, both these cases can cause division by zero.
The text was updated successfully, but these errors were encountered: