Color Conversion

Posted on
Tue Jan 14, 2020 11:15 am
DaveL17 offline
User avatar
Posts: 6755
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Color Conversion

I'm struggling with something that perhaps one of you legitimate developers can shed some light on--color conversion. Through the colorpicker field type, Indigo provides hooks into the Mac color picker and the result is a hex string representation of the color (like 'FF 00 00'). The Inovelli dimmer I just bought (and likely many, many other devices) store the color value as a hue (0-255, with 255 apparently reserved for white). But because the hue value can be 0-360, valid entries must be scaled: hue/360 * 255. I can't even figure out whether the hue value is an HSV or HSB (or if there is a difference). I want to convert using Python and my research leads me to believe that in order to make the conversion, I have to go hex --> rgb --> hue, but all my attempts have so far been fails. colorsys() seemingly gets me part of the way there, but I'm still coming up short. I did come across some discussions of converting to opencv (whatever that is) but most of that was in C+ or Java.

I can work around this by just using a textfield limited to values from 0-255, but what I'd like is to be able to select a color in the Mac color picker, calculate the nearest hue value, and blow that up to the device for setting parms.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jan 14, 2020 12:48 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Color Conversion


Image

Posted on
Tue Jan 14, 2020 12:49 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Color Conversion

https://docs.python.org/2/library/colorsys.html

I use this in the LIFX Bridge plugin. Here's some code from there:

Code: Select all
                red =   iDev.redLevel   / 100.0          # normalize first
                green = iDev.greenLevel / 100.0
                blue =  iDev.blueLevel  / 100.0
                hsv_color = colorsys.rgb_to_hsv(red, green, blue)
               
                adj_hue = int(hsv_color[0] * 65535) # convert to LIFX
                adj_sat = int(hsv_color[1] * 65535)
                adj_val = int(hsv_color[2] * 65535)


That takes the RGB levels from an Indigo device, converts them to the range needed by the library, then converts the output from the library to the range that LIFX uses.

Post the code you has for using colorsys, I can probably help you get it working.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Jan 14, 2020 1:12 pm
DaveL17 offline
User avatar
Posts: 6755
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Color Conversion

matt (support) wrote:

Thanks Matt. That's the best explanation I've seen so far (and interestingly, that one didn't come up in my searches).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jan 14, 2020 1:13 pm
DaveL17 offline
User avatar
Posts: 6755
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Color Conversion

FlyingDiver wrote:
https://docs.python.org/2/library/colorsys.html

I use this in the LIFX Bridge plugin. Here's some code from there:

Code: Select all
                red =   iDev.redLevel   / 100.0          # normalize first
                green = iDev.greenLevel / 100.0
                blue =  iDev.blueLevel  / 100.0
                hsv_color = colorsys.rgb_to_hsv(red, green, blue)
               
                adj_hue = int(hsv_color[0] * 65535) # convert to LIFX
                adj_sat = int(hsv_color[1] * 65535)
                adj_val = int(hsv_color[2] * 65535)


That takes the RGB levels from an Indigo device, converts them to the range needed by the library, then converts the output from the library to the range that LIFX uses.

Post the code you has for using colorsys, I can probably help you get it working.

Thanks for this, too. I think I have what I need to get going. I'll ping back if I still can't work it out.

I must admit that I do feel a bit better about not being able to land on the solution on my own... :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jan 14, 2020 3:33 pm
DaveL17 offline
User avatar
Posts: 6755
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Color Conversion

There are no guarantees that this is entirely right, but initial testing shows a lot of promise. It's based on the source that Matt sent.

Suggested refinements most welcome.

Code: Select all
def convert_color(color):
# Source: https://stackoverflow.com/questions/23090019/fastest-formula-to-get-hue-from-rgb
# Source: https://community.inovelli.com/t/red-series-dimmer-hue-color-circle/1013
# Source: https://nathanfiscus.github.io/inovelli-notification-calc/

    color_rgb = [int('0x{0}'.format(_), 16) for _ in color.split(' ')]
   
    color_max = color_rgb.index(max(color_rgb))
    color_min = color_rgb.index(min(color_rgb))
   
    red   = color_rgb[0]
    green = color_rgb[1]
    blue  = color_rgb[2]
   
    if color_max == 0:
        hue = (green - blue) / (color_rgb[color_max] - color_rgb[color_min])
       
    elif color_max == 1:
        hue = 2.0 + (blue - red) / (color_rgb[color_max] - color_rgb[color_min])
       
    else:
        hue = 4.0 + (red - green) / (color_rgb[color_max] - color_rgb[color_min])
   
    adjusted_hue = hue * 60.0
   
    if adjusted_hue < 0:
        adjusted_hue += 360
       
    final_hue = int(adjusted_hue / 360.0 * 255.0)
   
    if final_hue == 0:
        final_hue = 1
   
    return final_hue

hue = convert_color('46 34 D4')

print(hue)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests