Skip to content

Commit ae634a9

Browse files
authored
fix(zigbee): Fix RGB color calculation (#11624)
1 parent f08efa1 commit ae634a9

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

cores/esp32/ColorFormat.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ espHsvColor_t espRgbColorToHsvColor(espRgbColor_t rgb) {
119119
}
120120

121121
espRgbColor_t espXYColorToRgbColor(uint8_t Level, espXyColor_t xy) {
122-
return espXYToRgbColor(Level, xy.x, xy.y);
122+
return espXYToRgbColor(Level, xy.x, xy.y, true);
123123
}
124124

125-
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y) {
125+
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y, bool addXYZScaling) {
126126
// convert xyY color space to RGB
127127

128128
// https://www.easyrgb.com/en/math.php
@@ -156,9 +156,11 @@ espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t curren
156156
// X, Y and Z input refer to a D65/2° standard illuminant.
157157
// sR, sG and sB (standard RGB) output range = 0 ÷ 255
158158
// convert XYZ to RGB - CIE XYZ to sRGB
159-
X = X / 100.0f;
160-
Y = Y / 100.0f;
161-
Z = Z / 100.0f;
159+
if (addXYZScaling) {
160+
X = X / 100.0f;
161+
Y = Y / 100.0f;
162+
Z = Z / 100.0f;
163+
}
162164

163165
r = (X * 3.2406f) - (Y * 1.5372f) - (Z * 0.4986f);
164166
g = -(X * 0.9689f) + (Y * 1.8758f) + (Z * 0.0415f);

cores/esp32/ColorFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#pragma once
2020

2121
#include <stdint.h>
22+
#include <stdbool.h>
2223
#ifdef __cplusplus
2324
extern "C" {
2425
#endif
@@ -49,7 +50,7 @@ typedef struct HsvColor_t espHsvColor_t;
4950
typedef struct XyColor_t espXyColor_t;
5051
typedef struct CtColor_t espCtColor_t;
5152

52-
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y);
53+
espRgbColor_t espXYToRgbColor(uint8_t Level, uint16_t current_X, uint16_t current_Y, bool addXYZScaling);
5354
espRgbColor_t espXYColorToRgb(uint8_t Level, espXyColor_t xy);
5455
espXyColor_t espRgbColorToXYColor(espRgbColor_t rgb);
5556
espXyColor_t espRgbToXYColor(uint8_t r, uint8_t g, uint8_t b);

libraries/Zigbee/src/ep/ZigbeeColorDimmableLight.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,21 @@ void ZigbeeColorDimmableLight::zbAttributeSet(const esp_zb_zcl_set_attr_value_me
7676
return;
7777
} else {
7878
log_w("Received message ignored. Attribute ID: %d not supported for Level Control", message->attribute.id);
79-
//TODO: implement more attributes -> includes/zcl/esp_zigbee_zcl_level.h
8079
}
8180
} else if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_COLOR_CONTROL) {
8281
if (message->attribute.id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_X_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
8382
uint16_t light_color_x = (*(uint16_t *)message->attribute.data.value);
8483
uint16_t light_color_y = getCurrentColorY();
8584
//calculate RGB from XY and call setColor()
86-
_current_color = espXYToRgbColor(255, light_color_x, light_color_y); //TODO: Check if level is correct
85+
_current_color = espXYToRgbColor(255, light_color_x, light_color_y, false);
8786
lightChanged();
8887
return;
8988

9089
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_Y_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U16) {
9190
uint16_t light_color_x = getCurrentColorX();
9291
uint16_t light_color_y = (*(uint16_t *)message->attribute.data.value);
9392
//calculate RGB from XY and call setColor()
94-
_current_color = espXYToRgbColor(255, light_color_x, light_color_y); //TODO: Check if level is correct
93+
_current_color = espXYToRgbColor(255, light_color_x, light_color_y, false);
9594
lightChanged();
9695
return;
9796
} else if (message->attribute.id == ESP_ZB_ZCL_ATTR_COLOR_CONTROL_CURRENT_HUE_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_U8) {

0 commit comments

Comments
 (0)