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