esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
285 stars 34 forks source link

Overflow in color_uncorrect_red(), color_uncorrect_green(), color_uncorrect_blue() and color_uncorrect_white() methods of ESPColorCorrection object #5725

Open g-kiss opened 3 weeks ago

g-kiss commented 3 weeks ago

The problem

There are some brightness values at which "it" lambda parameter returns wrong values.

This issue is occured by the color_uncorrect_red(), color_uncorrect_green(), color_uncorrect_blue() and color_uncorrect_white() methods of ESPColorCorrection object. In some cases there are overflow in calculations. This is an examle: If red = 255, brightness = 104 and gamma = 2.8, then then value of the pixel will be 21 (this is correct). If i read this value with it[1].get().red() the color_uncorrect_red() method of ESPColorCorrection object calclutates the following (in esp_color_correction.h):

    uint16_t uncorrected = this->gamma_reverse_table_[red] * 255UL;
    uint8_t res = ((uncorrected / this->max_brightness_.red) * 255UL) / this->local_brightness_;
    return res;

The gamma_reversetable[21] returns value 105 (instead of 104), because 104 and 105 have the same gamma value (21). The calculation is the following: 105 (reverse gamma) 255 (const) / 255 (maxbrightness.red) 255 (const) / 104 (localbrightness) = 257.45. --> The res variable overflows and returns value 1 instead of 255.

Test yaml:

- platform: esp32_rmt_led_strip
    id: teszt_led_mst
    name: ${device_name}_teszt_led_mst
    rgb_order: GRB
    pin: GPIO23
    num_leds: 60
    rmt_channel: 7
    chipset: ws2812
    effects:
      - addressable_lambda:
          name: "Test"
          update_interval: 1s
          lambda: |-
            ESP_LOGD("main", "Red: %d", it[1].get().red);
            ESP_LOGD("main", "Green: %d", it[1].get().green);
            ESP_LOGD("main", "Blue: %d", it[1].get().blue);

Teszt case 1: Service call in HomeAssistant (brightness = 103):

service: light.turn_on
data:
  brightness: 103
  entity_id: light.teszt_esp32_teszt_led_mst
  rgb_color:
    - 255
    - 255
    - 255

After starting "Test" effect, returns good values:

[20:31:54][D][main:126]: Red: 255
[20:31:54][D][main:127]: Green: 255
[20:31:54][D][main:128]: Blue: 255

Teszt case 2: Service call in HomeAssistant (brightness = 102):

service: light.turn_on
data:
  brightness: 102
  entity_id: light.teszt_esp32_teszt_led_mst
  rgb_color:
    - 255
    - 255
    - 255

After starting "Test" effect, returns wrong values:

[21:54:04][D][main:126]: Red: 1
[21:54:04][D][main:127]: Green: 1
[21:54:04][D][main:128]: Blue: 1

Which version of ESPHome has the issue?

2024.4.0

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

2024.4.3

What platform are you using?

ESP32

Board

nodemcu

Component causing the issue

Light

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response