question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conversion from RGB to HSV result in a NaN hue

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ggrellcommented, Dec 7, 2021

Thank you, I now better appreciate the complexities of color math! And thanks again for building this great library.

0reactions
ajaltcommented, Nov 24, 2021

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:

HSV(0, 0, 1).interpolate(HSV(235, 1, 1), .5)
// #7d4080, dark magenta

HSV(NaN, 0, 1).interpolate(HSV(235, 1, 1), .5)
// #404580, dark blue

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 into Float?.

As for your example, HSV.toSRGB() should never result in NaN values in the RGB 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found