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

Fix hsvColor returning a NaN hue for achromatic (grey) colors#1454
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.
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