Prevent nan when using color_to_hsv(white)

When r,g,b are equal, delta will be zero and hue will be nan due to
divide by zero.

This is also an issue with the reference C++ code:
https://tio.run/##Sy4o0E3OScxL//9fOTMvOac0JVXBJjO/uKQoNTHXjiszr0QhNzEzT0NToZqLs7gkxcoqOb@0RMHGRkHJIzUnJ19HITy/KCdFUQkkBJZPzUvJseaq/f8fAA

An alternative approach is to always add epsilon or use a different hue
calculation:
    http://web.archive.org/web/20210515082154/http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
This commit is contained in:
David Briscoe 2022-04-01 21:27:56 -07:00
parent 41b1f0b5b9
commit aec3b9fd7f

View File

@ -2,6 +2,7 @@
-- @module color
local modules = (...):gsub('%.[^%.]+$', '') .. "."
local constants = require(modules .. "constants")
local utils = require(modules .. "utils")
local precond = require(modules .. "_private_precond")
local color = {}
@ -75,6 +76,11 @@ local function color_to_hsv(c)
return { h, s, v, 1 }
end
-- Prevent division by zero.
if delta == 0 then
delta = constants.DBL_EPSILON
end
if r == max then
h = ( g - b ) / delta -- yellow/magenta
elseif g == max then