Conversion from RGB to HSV result in a NaN hue
See original GitHub issueI’m using your library to build an HSV style color picker which inputs and outputs RGB colors, thus the need for conversions. I’m running into an issue where converting a valid RGB color to HSV results in a NaN
for the hue (RGB("#A9A9A9").toHSV()
results in HSV(h=NaN, s=0.0, v=0.6627451, alpha=1.0)
. If I check the colormath color converter on the website, it converts it to HSV 0, 0, 0.66275
.
Debugging the code, I see down in RGB.srgbHueMinMaxChroma
calculations, min == max
, so chroma
is assigned 0 and thus h
is set to NaN
.
Is this a bug? or should I assume if a value is NaN
to make it 0f
This is running on an Android emulator, Kotlin 1.5.31.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Convert RGB value to HSV - java - Stack Overflow
I've found a method on the Internet to convert RGB values to HSV values. Unfortunately, when the values are R=G=B, I'm getting a...
Read more >Colour space conversion - Part 2 - RGB to HSV - YouTube
Colour space conversion - Part 2 - RGB to HSVPart 2 of the two-part mini-series on colour space conversion. In this episode, I...
Read more >RGB to HSV Conversion in MATLAB code - YouTube
In this example we explained how to convert a RGB color image into HSV Color space with the help of matlab rgb2hsv function....
Read more >RGB to HSV color conversion - RapidTables.com
HSV to RGB conversion ▻. RGB to HSV conversion formula. The R,G,B values are divided by 255 to change the range from 0..255...
Read more >RGB <-> HSV - Jens Kafitz
a Color Lookup and then converted back to RGB. Since the Red, Green and Blue Channels now represent Hue, Saturation and Value, by...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you, I now better appreciate the complexities of color math! And thanks again for building this great library.
Monochromatic colors don’t have a defined hue. Assigning one arbitrarily may be desirable for some applications, like your color picker, but causes problems in others.
For example: 0° hue in HSV is red. So if try to draw a gradient in HSV from white to blue, if use 0° hue for white, you’ll see that the gradient will travel through red and magenta (or green, depending on the direction around the hue circle it travels), rather than just changing the lightness like you’d want:
So we need a separate representation for undefined hues. Colormath uses NaN for this, since it makes it easier to write generic
map
functions on colors without performance penalty of boxing intoFloat?
.As for your example,
HSV.toSRGB()
should never result in NaN values in theRGB
instance. Can you share the actual values you are using that caused that?RGB("#ffffff").toHSV().toSRGB().toHex() == "#ffffff"
. Certainly all hex values should round-trip.I’ll definitely add an extension like you suggest.