diff options
author | Jordi Orlando <0xdec@users.noreply.github.com> | 2016-07-26 15:31:22 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-26 15:31:22 -0500 |
commit | ea2d2f5d5841791745c93ef27cd1528a7fd69c97 (patch) | |
tree | e902f3ea83255c2ada2b79e50f11507563308725 /quantum/rgblight.c | |
parent | 899c88cd8bf024792760fcf3ee8be6fed13fb315 (diff) | |
download | qmk_firmware-ea2d2f5d5841791745c93ef27cd1528a7fd69c97.tar.gz |
Simplify HSV->RGB calculation
De-dupe repeated code
Diffstat (limited to 'quantum/rgblight.c')
-rw-r--r-- | quantum/rgblight.c | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/quantum/rgblight.c b/quantum/rgblight.c index c2841b851..f82e3ec55 100644 --- a/quantum/rgblight.c +++ b/quantum/rgblight.c @@ -55,57 +55,56 @@ uint8_t rgblight_inited = 0; void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { - /* Convert hue, saturation and brightness ( HSB/HSV ) to RGB. The DIM_CURVE is - used only on brightness/value and on saturation (inverted). This looks the - most natural. */ - uint8_t r = 0, g = 0, b = 0; + // Convert hue, saturation, and value (HSV/HSB) to RGB. DIM_CURVE is used only + // on value and saturation (inverted). This looks the most natural. + uint8_t r = 0, g = 0, b = 0, base, color; val = pgm_read_byte(&DIM_CURVE[val]); sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]); - uint8_t base; - if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. r = val; g = val; b = val; } else { base = ((255 - sat) * val) >> 8; + color = (val - base) * (hue % 60) / 60; switch (hue / 60) { case 0: r = val; - g = (((val - base) * hue) / 60) + base; + g = base + color; b = base; break; case 1: - r = (((val - base) * (60 - (hue % 60))) / 60) + base; + r = val - color; g = val; b = base; break; case 2: r = base; g = val; - b = (((val - base) * (hue % 60)) / 60) + base; + b = base + color; break; case 3: r = base; - g = (((val - base) * (60 - (hue % 60))) / 60) + base; + g = val - color; b = val; break; case 4: - r = (((val - base) * (hue % 60)) / 60) + base; + r = base + color; g = base; b = val; break; case 5: r = val; g = base; - b = (((val - base) * (60 - (hue % 60))) / 60) + base; + b = val - color; break; } } - setrgb(r,g,b, led1); + + setrgb(r, g, b, led1); } void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { @@ -346,7 +345,6 @@ void rgblight_set(void) { } } - #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) // Animation timer -- AVR Timer3 |