Common jinja template color helper functions for
- convert RGB to HEX
- convert HEX to RGB
- convert HS to RGB
- convert HS to HEX
convert RGB to HS
π Hue and Saturation (state_attr('light.x','hs_color')
{%- set h = (state_attr('light.desk','hs_color') | list )[0] -%}
# hue : {{ h }}
{%- set s = (state_attr('light.desk','hs_color') | list )[1] -%}
# saturation : {{ s }}
π Red, Green and Blue state_attr('light.x','rgb_color')
{%- set r = (state_attr('light.desk','rgb_color') | list )[0] -%}
# red : {{ r }}
{%- set g = (state_attr('light.desk','rgb_color') | list )[1] -%}
# green : {{ g }}
{%- set b = (state_attr('light.desk','rgb_color') | list )[2] -%}
# blue : {{ b }}
π example input (0,0,255)
β returns 0000ff
{%- set r = 0 -%}
{%- set g = 0 -%}
{%- set b = 255 -%}
hex : {{ '%02x%02x%02x' | format(r, g, b) }} - or - {{'%02x'%r+'%02x'%g+'%02x'%b}}
π example input ff0000
β returns (255, 0, 0)
{%- set rr = 'ff' -%}
{%- set gg = '00' -%}
{%- set bb = '00' -%}
rgb : {{r | int(base=16), g | int(base=16), b | int(base=16)}}
π example input hsv(240,100,100)
β returns (0, 0, 255)
{%- set h = (state_attr('light.desk','hs_color') | list )[0] -%}
{%- set s = 100 -%}
{%- set v = (state_attr('light.desk','hs_color') | list )[1] -%}
s
stays at 100
. Because you only get 2 values from light in HA. An π―οΈ emitting light does not have saturation, it has π brightness.
{%- set h = 360 -%} {%- set s = 100 -%} {%- set v = 100 -%} {%- set i = (h * 6 ) | round(2,'floor') -%} {%- set f = h * 6 - i -%} {%- set p = v * (1 - s) -%} {%- set q = v * (1 - f * s) -%} {%- set t = v * (1 - (1 - f) * s) -%} {%- if i % 6 == 0 -%} {%- set r = v | int -%} {%- set g = t | int -%} {%- set b = p | int -%} {%- elif i % 6 == 1 -%} {%- set r = q | int -%} {%- set g = v | int -%} {%- set b = p | int -%} {%- elif i % 6 == 2 -%} {%- set r = p | int -%} {%- set g = v | int -%} {%- set b = t | int -%} {%- elif i % 6 == 3 -%} {%- set r = p | int -%} {%- set g = q | int -%} {%- set b = v | int -%} {%- elif i % 6 == 4 -%} {%- set r = t | int -%} {%- set g = p | int -%} {%- set b = v | int -%} {%- elif i % 6 == 5 -%} {%- set r = v | int -%} {%- set g = p | int -%} {%- set b = q | int -%} {%- endif -%} rgb : {{ (r, g, b) | list }}