Fix ValueError converting xy color with a tiny nonzero y (#187655)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Raman Gupta
2026-10-20 22:31:24 -04:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 3305f1545c
commit 1c57f2e7b8
2 changed files with 20 additions and 2 deletions
+2 -2
View File
@@ -280,8 +280,8 @@ def color_xy_brightness_to_RGB(
Y = brightness
if vY == 0.0:
vY += 1e-11
# Avoid overflowing Y / vY to inf (and NaN below) for zero or tiny y.
vY = max(vY, 1e-11)
X = (Y / vY) * vX
Z = (Y / vY) * (1 - vX - vY)
+18
View File
@@ -117,6 +117,24 @@ def test_color_xy_to_RGB() -> None:
assert color_util.color_xy_to_RGB(0, 0, GAMUT) == (9, 85, 255)
@pytest.mark.parametrize(
("x", "expected"),
[
pytest.param(1.0, (255, 0, 60), id="x_1"),
pytest.param(0.5, (255, 0, 227), id="x_0.5"),
pytest.param(0.3, (181, 0, 255), id="x_0.3"),
],
)
@pytest.mark.parametrize("y", [5e-324, 1e-320, 5e-309])
def test_color_xy_brightness_to_RGB_tiny_y(
x: float, y: float, expected: tuple[int, int, int]
) -> None:
"""Test a tiny nonzero y is handled like y == 0 instead of producing NaN."""
assert color_util.color_xy_brightness_to_RGB(x, y, 255) == expected
assert color_util.color_xy_brightness_to_RGB(x, 0, 255) == expected
assert color_util.color_xy_to_hs(x, y) == color_util.color_xy_to_hs(x, 0)
def test_color_RGB_to_hsv() -> None:
"""Test color_RGB_to_hsv."""
assert color_util.color_RGB_to_hsv(0, 0, 0) == (0, 0, 0)