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 qmk_firmware-ea2d2f5d5841791745c93ef27cd1528a7fd69c97.zip | |
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; | |||
| 55 | 55 | ||
| 56 | 56 | ||
| 57 | void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { | 57 | void sethsv(uint16_t hue, uint8_t sat, uint8_t val, struct cRGB *led1) { |
| 58 | /* Convert hue, saturation and brightness ( HSB/HSV ) to RGB. The DIM_CURVE is | 58 | // Convert hue, saturation, and value (HSV/HSB) to RGB. DIM_CURVE is used only |
| 59 | used only on brightness/value and on saturation (inverted). This looks the | 59 | // on value and saturation (inverted). This looks the most natural. |
| 60 | most natural. */ | 60 | uint8_t r = 0, g = 0, b = 0, base, color; |
| 61 | uint8_t r = 0, g = 0, b = 0; | ||
| 62 | 61 | ||
| 63 | val = pgm_read_byte(&DIM_CURVE[val]); | 62 | val = pgm_read_byte(&DIM_CURVE[val]); |
| 64 | sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]); | 63 | sat = 255 - pgm_read_byte(&DIM_CURVE[255 - sat]); |
| 65 | 64 | ||
| 66 | uint8_t base; | ||
| 67 | |||
| 68 | if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. | 65 | if (sat == 0) { // Acromatic color (gray). Hue doesn't mind. |
| 69 | r = val; | 66 | r = val; |
| 70 | g = val; | 67 | g = val; |
| 71 | b = val; | 68 | b = val; |
| 72 | } else { | 69 | } else { |
| 73 | base = ((255 - sat) * val) >> 8; | 70 | base = ((255 - sat) * val) >> 8; |
| 71 | color = (val - base) * (hue % 60) / 60; | ||
| 74 | 72 | ||
| 75 | switch (hue / 60) { | 73 | switch (hue / 60) { |
| 76 | case 0: | 74 | case 0: |
| 77 | r = val; | 75 | r = val; |
| 78 | g = (((val - base) * hue) / 60) + base; | 76 | g = base + color; |
| 79 | b = base; | 77 | b = base; |
| 80 | break; | 78 | break; |
| 81 | case 1: | 79 | case 1: |
| 82 | r = (((val - base) * (60 - (hue % 60))) / 60) + base; | 80 | r = val - color; |
| 83 | g = val; | 81 | g = val; |
| 84 | b = base; | 82 | b = base; |
| 85 | break; | 83 | break; |
| 86 | case 2: | 84 | case 2: |
| 87 | r = base; | 85 | r = base; |
| 88 | g = val; | 86 | g = val; |
| 89 | b = (((val - base) * (hue % 60)) / 60) + base; | 87 | b = base + color; |
| 90 | break; | 88 | break; |
| 91 | case 3: | 89 | case 3: |
| 92 | r = base; | 90 | r = base; |
| 93 | g = (((val - base) * (60 - (hue % 60))) / 60) + base; | 91 | g = val - color; |
| 94 | b = val; | 92 | b = val; |
| 95 | break; | 93 | break; |
| 96 | case 4: | 94 | case 4: |
| 97 | r = (((val - base) * (hue % 60)) / 60) + base; | 95 | r = base + color; |
| 98 | g = base; | 96 | g = base; |
| 99 | b = val; | 97 | b = val; |
| 100 | break; | 98 | break; |
| 101 | case 5: | 99 | case 5: |
| 102 | r = val; | 100 | r = val; |
| 103 | g = base; | 101 | g = base; |
| 104 | b = (((val - base) * (60 - (hue % 60))) / 60) + base; | 102 | b = val - color; |
| 105 | break; | 103 | break; |
| 106 | } | 104 | } |
| 107 | } | 105 | } |
| 108 | setrgb(r,g,b, led1); | 106 | |
| 107 | setrgb(r, g, b, led1); | ||
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { | 110 | void setrgb(uint8_t r, uint8_t g, uint8_t b, struct cRGB *led1) { |
| @@ -346,7 +345,6 @@ void rgblight_set(void) { | |||
| 346 | } | 345 | } |
| 347 | } | 346 | } |
| 348 | 347 | ||
| 349 | |||
| 350 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) | 348 | #if !defined(AUDIO_ENABLE) && defined(RGBLIGHT_TIMER) |
| 351 | 349 | ||
| 352 | // Animation timer -- AVR Timer3 | 350 | // Animation timer -- AVR Timer3 |
