RGB color values over-saturated on ESP32 C6
First of all: Thank you for putting this repo together. It helped my a lot to figure out my Zigbee troubles.
I got the example configuration example_esp32c6.yaml to run on a C6 dev board, but I noticed that the colors where not showing up correctly on the on-board LED. The LED was mostly white for nearly all color targets I tried.
On the log output I see, that the RGB values calculated from the x and y values are clipped, because they are above 1.00. Here the output for (x,y)=(11141,6554), which should correspond roughly to (r,g,b)=(58,74,255) (on a scale 0-255)
[17:17:47][W][light:222][Zigbee_main]: 'light_1' - Green value 1.02 is out of range [0.0 - 1.0]!
[17:17:47][W][light:223][Zigbee_main]: 'light_1' - Blue value 2.62 is out of range [0.0 - 1.0]!
[17:17:47][D][light:036][Zigbee_main]: 'light_1' Setting:
[17:17:47][D][light:058][Zigbee_main]: Red: 58%, Green: 100%, Blue: 100%
[17:17:47][D][light:085][Zigbee_main]: Transition length: 0.2s
By playing around with the x and y values it became apparent that the resulting RGB values are in the wrong range namely 0-2.55 instead of 0-1. Therefore, the correct scaling can be applied in the configuration:
- id: COLOR_CONTROL
attributes:
- attribute_id: 3
type: U16
on_value:
then:
- lambda: id(color_x) = (float)x/65536;
- light.control:
id: light_1
red: !lambda "return ((float)zigbee::get_r_from_xy(id(color_x), id(color_y)))/2.55;"
green: !lambda "return ((float)zigbee::get_g_from_xy(id(color_x), id(color_y)))/2.55;"
blue: !lambda "return ((float)zigbee::get_b_from_xy(id(color_x), id(color_y)))/2.55;"
- attribute_id: 4
type: U16
on_value:
then:
- lambda: id(color_y) = (float)x/65536;
- light.control:
id: light_1
red: !lambda "return ((float)zigbee::get_r_from_xy(id(color_x), id(color_y)))/2.55;"
green: !lambda "return ((float)zigbee::get_g_from_xy(id(color_x), id(color_y)))/2.55;"
blue: !lambda "return ((float)zigbee::get_b_from_xy(id(color_x), id(color_y)))/2.55;"
Is that somehow an issue isolated to the specific C6 module I am using or should that be applied to the example yaml?
BTW: my setup is a ZB-GW03 controller running Zigbee2Tasmota, but I also tested the color settings running ZHA and got the same wrong colors.
The values should be between 0 and 1 if the xy value is within the RGB color space. If it is outside then there will be clipping. I don't know much about color spaces, but there are plenty of possibilities to convert them: http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html Right now, Wide Gamut RGB is used, but it seems that many projects (e.q. Arduino) use sRGB. You could try to replace the values in line 11, 19, and 28 here: https://github.com/luar123/zigbee_esphome/blob/master/components/zigbee/automation.cpp Here is another description: https://github.com/johnciech/PhilipsHueSDK/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
Thanks for your answer. I used an online converter to choose X and Y values, which translate into valid RGB values (https://viereck.ch/hue-xy-rgb/). Indeed I used sRGB not wide Gamit RGB, but the xy values I tried should have also been valid in wide gamit space. Anyway, I am having similar issues when choosing colors in the HomeAssistant color picker (using my ZigBee controller via ZHA). That leaves me somewhat confident that the xy values I tested with should yield valid RGB values
Also, thank you for your suggestions on how to fix it. I had briefly looked into the code you mentioned, which converts between the color spaces, but then I found the lower effort solution (meaning without touching the c code) via the fixed scale-down by 2.55 in the yaml-configuration. I was just wondering, whether this is somehow specific to my setup, be it the ZigBee controller or the ESP C6 board, or whether I could contribute an up-stream fix to the example yaml for others. If you do not have any issues with the RGB values on your end, then it probably is due to the setup differences.
#66 fixes this
Fixed in 6240102 Thanks to @louiecaulfield