diff --git a/examples/lighting-app/lighting-common/src/ColorFormat.cpp b/examples/lighting-app/lighting-common/src/ColorFormat.cpp index d45d8cdc693291..d30aacd841cc10 100644 --- a/examples/lighting-app/lighting-common/src/ColorFormat.cpp +++ b/examples/lighting-app/lighting-common/src/ColorFormat.cpp @@ -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;