Skip to content

Commit 0c85539

Browse files
Refine xyY conversions for better robustness and code clarity
Improve floating-point safety by using epsilon-based checks (< 1e-10) instead of exact zero comparisons in division guards. Use clamp_rgb for intermediate RGB clamping in xyy_to_rgb for consistency and cleaner code.
1 parent 035fdcf commit 0c85539

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

color/core/rgb_to_xyy.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ local function rgb_to_xyy(red, green, blue)
5454

5555
-- Fix: Check for X + Y + Z == 0 to avoid division by zero (ST bug)
5656
local sum = X + Y + Z
57-
if sum == 0 then
57+
if sum < 1e-10 then -- Use epsilon for floating-point safety
5858
return 0, 0, 0
5959
end
6060

color/core/xyy_to_rgb.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ local function xyy_to_rgb(x, y, Y)
5050
local b = X * M[3][1] + Y * M[3][2] + Z * M[3][3]
5151

5252
-- Clamp to [0,1] for gamut
53-
r = r < 0 and 0 or r > 1 and 1 or r
54-
g = g < 0 and 0 or g > 1 and 1 or g
55-
b = b < 0 and 0 or b > 1 and 1 or b
53+
r, g, b = clamp_rgb(r, g, b)
5654

5755
-- Fix: Check for max_rgb == 0 to avoid division by zero (ST bug)
5856
local max_rgb = math.max(r, g, b)
59-
if max_rgb == 0 then
57+
if max_rgb < 1e-10 then -- Use epsilon for floating-point safety
6058
return 0, 0, 0
6159
end
6260

0 commit comments

Comments
 (0)