Logic Machine Forum
RGB to HEX convert script - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: RGB to HEX convert script (/showthread.php?tid=1394)



RGB to HEX convert script - Jayce - 15.05.2018

Hello, 

I have LED Dali strip, which I can set RGB values to. I wanted to use a mosaic widget to set color value but it only works with HEX code. I have three seperate group adresses (r, g, b) and I need to convert them to one HEX group address that I can link to that widget. Is this possible?

EDIT: First question has been solved, sorry I did some deeper research and found whole article.

A completely different question. Is is possible to DIM pre-mixed RGB color? Let's say I also have 3 group adresses, which I mixed and got my final color. I now want to dim it so it's the same color but less bright.

Thanks for any help.


RE: RGB to HEX convert script - Daniel - 15.05.2018

Hi
If you didn't use yet then select RGBW widget.
To dim just use the slider.
BR


RE: RGB to HEX convert script - Jayce - 16.05.2018

Thank you very much! Haven't tried that yet but if it works, is there a way to automatize it? I only need a single switch group that switches on already mixed and dimmed RGB strip.


RE: RGB to HEX convert script - Daniel - 16.05.2018

(16.05.2018, 09:04)Jayce Wrote: Thank you very much! Haven't tried that yet but if it works, is there a way to automatize it? I only need a single switch group that switches on already mixed and dimmed RGB strip.

Sorry don't know what you mean by that.


RE: RGB to HEX convert script - Jayce - 16.05.2018

I forgot to mention that the second question was outside of mosaic. Let me explain myself better. Right now, I have created a group (let's call it RGB), where I put a simple script that sets three groups (red, green and blue) to certain value. That means when I set RGB group to 1, my exact color mixture turns on. The problem is it is too bright. I need to dim it. As you said, I might be able to dim it with RGBW widget, but that is widget. I'd need it to be outside of mosaic.


RE: RGB to HEX convert script - Daniel - 16.05.2018

OK bit of calculation is needed. Here is script which converts 3B RGB in to RGBW and saturation. You could convert it to your needs.

Code:
addressred = '1/1/16'
addressgreen = '1/1/17'
addressblue = '1/1/18'
addresswhite = '1/1/19'
addresssaturation = '1/1/20'

-- Get Value of RGB picker
value = event.getvalue()

-- Convert to HEX for splitting to 3 byte values
value = lmcore.inttohex(value, 3) -- 3 = number of bytes inside value

-- Split First 2 byte HEX values from 3 byte HEX
redandgreen = string.sub(value, 1, 4)

-- Split First Value from 2 byte HEX
red = string.sub(redandgreen, 1, 2)

-- Split Last value from 2 byte HEX
green = string.sub(redandgreen, -2)

-- Split Last byte value from 3 byte HEX
blue = string.sub(value, -2)

-- Convert HEX values back to integer
valuered = lmcore.hextoint (red)
valuegreen = lmcore.hextoint (green)
valueblue = lmcore.hextoint (blue)

-- Calculate lowest value of RGB value
low = math.min(unpack({valuered, valuegreen, valueblue}))

-- Calculate highest value of RGB value
high = math.max(unpack({valuered, valuegreen, valueblue}))

-- Calculate Saturation (The saturation is the colorfulness of a color relative to its own brightness) The difference between the last two variables divided by the highest is the saturation.
saturation = math.floor((100 * ((high - low) / high)) + 0.5)
if valuered == 0 and valuegreen == 0 and valueblue == 0 then
 white = 0
else
 white = math.floor(((255 - saturation) / 255 * (valuered + valuegreen + valueblue) / 3)+ 0.5)
end

-- Write integer RGB values and Calculated White & Saturation to KNX
grp.update(addressred, math.floor((valuered / 2.55) + 0.5))
grp.update(addressgreen, math.floor((valuegreen / 2.55) + 0.5))
grp.update(addressblue, math.floor((valueblue / 2.55) + 0.5))
grp.update(addresswhite, math.floor((white / 2.55) + 0.5))
grp.update(addresssaturation, saturation)
BR


RE: RGB to HEX convert script - Jayce - 16.05.2018

Works, thank you a lot.