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