fix(color): avoid NaN saturation in _hslaToHSBA when lightness is 0#8805
Open
Chessing234 wants to merge 1 commit into
Open
fix(color): avoid NaN saturation in _hslaToHSBA when lightness is 0#8805Chessing234 wants to merge 1 commit into
Chessing234 wants to merge 1 commit into
Conversation
…s is 0 When the HSL lightness is 0, the branch 'val = (1 + sat) * li' on line 110 makes val = 0, and the next line computes 'sat = 2 * (val - li) / val' which divides by zero and yields NaN. Pure-black HSL colors then round-tripped through HSB with sat = NaN. The inverse helper _hsbaToHSLA already guards its corresponding division with 'if (li !== 0)'; apply the symmetric guard here so val === 0 short-circuits to sat = 0 (the correct saturation for a black color).
Collaborator
perminder-17
left a comment
There was a problem hiding this comment.
hi, can you please link the issue number in your PR? Also, if possible, can you also share the sketch for the reference? I am unable to understand the issue. Thanks :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
`p5.ColorConversion._hslaToHSBA` returns `NaN` for the saturation channel whenever the HSL lightness is `0` (i.e. any pure-black HSL color converted to HSB). The downstream HSB color and anything derived from it (`color.toString()`, getters, brightness/saturation accessors) then carries that `NaN` forward.
Root cause
`src/color/color_conversion.js`:
```js
// Calculate brightness.
let val;
if (li < 0.5) {
val = (1 + sat) * li;
} else {
val = li + sat - li * sat;
}
// Convert saturation.
sat = 2 * (val - li) / val;
```
When `li === 0` the first branch picks `val = (1 + sat) * 0 === 0`, so the next line evaluates `2 * (0 - 0) / 0 === NaN`. No earlier branch shields the division, even though `val` is mathematically guaranteed to be 0 at that input.
Why the fix is correct
The sibling helper `_hsbaToHSLA` in the same file already guards its analogous division:
```js
// Convert saturation.
if (li !== 0) {
...
}
```
— leaving saturation at `0` when the achromatic axis collapses, which is the correct value for a black/gray color. Applying the symmetric guard to `_hslaToHSBA`:
```js
sat = val === 0 ? 0 : 2 * (val - li) / val;
```
short-circuits the divide-by-zero to `sat = 0` and produces `[hue, 0, 0, alpha]` for black, matching the convention used everywhere else in the conversion module. No behavior change for any non-zero `val` input.
One-line change.