Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix color conversion bug lighting-example #27065

Merged
merged 4 commits into from
Jun 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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