Skip to content

Commit

Permalink
Fix color conversion bug lighting-example
Browse files Browse the repository at this point in the history
This commit fixes the bug of issue #27063.
The used code is a copy of `[MATTERROOT]/examples/lighting-app/genio/src/ColorFormat.cpp`
  • Loading branch information
Sickeroni committed Jun 5, 2023
1 parent 275341e commit d9da036
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions examples/lighting-app/lighting-common/src/ColorFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,46 @@ RgbColor_t HsvToRgb(HsvColor_t hsv)
{
RgbColor_t rgb;

uint16_t i = hsv.h / 60;
uint16_t rgb_max = hsv.v;
uint16_t rgb_min = (uint16_t)(rgb_max * (100 - hsv.s)) / 100;
uint16_t diff = hsv.h % 60;
uint16_t rgb_adj = (uint16_t)((rgb_max - rgb_min) * diff) / 60;
uint8_t region, p, q, t;
uint32_t h, s, v, remainder;

switch (i)
if (hsv.s == 0)
{
case 0:
rgb.r = (uint8_t) rgb_max;
rgb.g = (uint8_t)(rgb_min + rgb_adj);
rgb.b = (uint8_t) rgb_min;
break;
case 1:
rgb.r = (uint8_t)(rgb_max - rgb_adj);
rgb.g = (uint8_t) rgb_max;
rgb.b = (uint8_t) rgb_min;
break;
case 2:
rgb.r = (uint8_t) rgb_min;
rgb.g = (uint8_t) rgb_max;
rgb.b = (uint8_t)(rgb_min + rgb_adj);
break;
case 3:
rgb.r = (uint8_t) rgb_min;
rgb.g = (uint8_t)(rgb_max - rgb_adj);
rgb.b = (uint8_t) rgb_max;
break;
case 4:
rgb.r = (uint8_t)(rgb_min + rgb_adj);
rgb.g = (uint8_t) rgb_min;
rgb.b = (uint8_t) rgb_max;
break;
default:
rgb.r = (uint8_t) rgb_max;
rgb.g = (uint8_t) rgb_min;
rgb.b = (uint8_t)(rgb_max - rgb_adj);
break;
rgb.r = rgb.g = rgb.b = hsv.v;
}
else
{
h = hsv.h;
s = hsv.s;
v = hsv.v;

region = h / 43;
remainder = (h - (region * 43)) * 6;
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * remainder) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
rgb.r = v, rgb.g = t, rgb.b = p;
break;
case 1:
rgb.r = q, rgb.g = v, rgb.b = p;
break;
case 2:
rgb.r = p, rgb.g = v, rgb.b = t;
break;
case 3:
rgb.r = p, rgb.g = q, rgb.b = v;
break;
case 4:
rgb.r = t, rgb.g = p, rgb.b = v;
break;
case 5:
default:
rgb.r = v, rgb.g = p, rgb.b = q;
break;
}
}

return rgb;
Expand Down Expand Up @@ -120,9 +122,9 @@ RgbColor_t XYToRgb(uint8_t Level, uint16_t currentX, uint16_t currentY)
b = clamp(b, 0, 1);

// these rgb values are in the range of 0 to 1, convert to limit of HW specific LED
rgb.r = (uint8_t)(r * 255);
rgb.g = (uint8_t)(g * 255);
rgb.b = (uint8_t)(b * 255);
rgb.r = (uint8_t) (r * 255);
rgb.g = (uint8_t) (g * 255);
rgb.b = (uint8_t) (b * 255);

return rgb;
}
Expand Down

0 comments on commit d9da036

Please sign in to comment.