Skip to content

Fix hsvColor returning a NaN hue for achromatic (grey) colors - #1454

Open
Sreekant13 wants to merge 1 commit into
g-truc:masterfrom
Sreekant13:fix/hsvcolor-nan-hue-for-grey
Open

Sreekant13 wants to merge 1 commit into
g-truc:masterfrom
Sreekant13:fix/hsvcolor-nan-hue-for-grey

Conversation

@Sreekant13

Copy link
Copy Markdown

hsvColor() computes the hue by dividing by Delta = max - min, but it only guards against max == 0, not Delta == 0. For any achromatic color where r == g == b > 0 (a grey), Delta is 0, so the hue becomes 60 * 0 / 0 = NaN:

glm::vec3 hsv = glm::hsvColor(glm::vec3(0.5f)); // hsv.x is NaN, expected 0

The reverse function rgbColor() already special-cases achromatic colors (if(hsv.y == 0)), so this makes hsvColor() consistent: when Delta == 0 the hue is undefined and set to 0 (saturation is already 0). Colored inputs are unchanged.

Added a regression test in test/gtx/gtx_color_space.cpp covering a grey input (hue must be 0, not NaN) and its round-trip.

hsvColor() divides by Delta (max - min) when computing the hue, but only
guarded against max == 0, not Delta == 0. For any grey where r == g == b > 0,
Delta is 0, so the hue became 60 * 0 / 0 = NaN (e.g. hsvColor(vec3(0.5)) gave
a NaN hue instead of 0).

Guard the achromatic case and set the hue to 0, consistent with the reverse
rgbColor() which already special-cases achromatic colors. Add a regression
test covering a grey input and its round-trip.
@Sreekant13

Copy link
Copy Markdown
Author

Hi @Groovounet, gentle nudge on this one. It fixes a NaN hue returned for achromatic colours, with a test covering it. No rush. Thank you for maintaining glm.

@Sreekant13

Copy link
Copy Markdown
Author

Hi @Groovounet, a gentle check-in on this one whenever you have a moment.

hsvColor() divides by Delta = max - min for the hue but only guards max == 0, so any achromatic colour returns a NaN hue: hsvColor(vec3(0.5)) gives NaN rather than 0. The fix guards the achromatic case and returns hue 0, which is what the reverse conversion rgbColor() already assumes, with a test covering it.

The PR is mergeable and green. No rush, and thank you for GLM.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant