Skip to content

Commit

Permalink
Fix color conversion bug lighting-example (#27065)
Browse files Browse the repository at this point in the history
* Fix color conversion bug lighting-example

This commit fixes the bug of issue #27063.
The used code is a copy of `[MATTERROOT]/examples/lighting-app/genio/src/ColorFormat.cpp`

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Sep 22, 2023
1 parent f22de5b commit 1106434
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 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

0 comments on commit 1106434

Please sign in to comment.