Search code examples
pythoncolor-space

Converting hsv to rgb and back


I've been having a heck of a time getting the proper values. In this example I have a device sending me a purple color where the data it sends to me is:

Saturation: 68
Brightness: 100
ColorTemperature: 4049
Hue: 0

And I need to convert that to an RGB. When I try the built-in method:

import colorsys

hue = 0
saturation = 68
brightness = 100
colortemp = 4049

r, g, b = colorsys.hsv_to_rgb(hue, saturation, brightness)

I get a result of:

Red: 100 | Green: -6700.0 | Blue: -6700.0

So I did some research and found this SO article that explains that the numbers need to be decimal values and tried it with their writeup when it is stated that

That function expects decimal for s (saturation) and v (value), not percent. Divide by 100.

Making the following changes to the code:

hue = 0
saturation = .68
brightness = 1.0
colortemp = 4049

Results in:

Red: 1.0 | Green: 0.32 | Blue: 0.32

Or when multiplied by 255:

Red: 255.0 | Green: 81.6 | Blue: 81.6

That color is red.

What am I doing wrong? I know, from using an eyedropper to grab the color, that the results should be around 102, 60, 250 for RGB but I'm not getting anywhere near that value.

To make this even more complicated I will need to, at some point, convert from RGB back to HSV again. If the solution is because I'm just using incorrect values or something then I assume the Python method to convert back will be correct but I'm just stuck now.


Solution

  • I think your device is sending you incorrect information. The values you provide for HSV are red.

    colorpicker