dynamic keymap sanity check (#8181)
[jackhill/qmk/firmware.git] / quantum / color.c
1 /* Copyright 2017 Jason Williams
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include "color.h"
18 #include "led_tables.h"
19 #include "progmem.h"
20
21 RGB hsv_to_rgb(HSV hsv) {
22 RGB rgb;
23 uint8_t region, remainder, p, q, t;
24 uint16_t h, s, v;
25
26 if (hsv.s == 0) {
27 #ifdef USE_CIE1931_CURVE
28 rgb.r = rgb.g = rgb.b = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
29 #else
30 rgb.r = hsv.v;
31 rgb.g = hsv.v;
32 rgb.b = hsv.v;
33 #endif
34 return rgb;
35 }
36
37 h = hsv.h;
38 s = hsv.s;
39 #ifdef USE_CIE1931_CURVE
40 v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
41 #else
42 v = hsv.v;
43 #endif
44
45 region = h * 6 / 255;
46 remainder = (h * 2 - region * 85) * 3;
47
48 p = (v * (255 - s)) >> 8;
49 q = (v * (255 - ((s * remainder) >> 8))) >> 8;
50 t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
51
52 switch (region) {
53 case 6:
54 case 0:
55 rgb.r = v;
56 rgb.g = t;
57 rgb.b = p;
58 break;
59 case 1:
60 rgb.r = q;
61 rgb.g = v;
62 rgb.b = p;
63 break;
64 case 2:
65 rgb.r = p;
66 rgb.g = v;
67 rgb.b = t;
68 break;
69 case 3:
70 rgb.r = p;
71 rgb.g = q;
72 rgb.b = v;
73 break;
74 case 4:
75 rgb.r = t;
76 rgb.g = p;
77 rgb.b = v;
78 break;
79 default:
80 rgb.r = v;
81 rgb.g = p;
82 rgb.b = q;
83 break;
84 }
85
86 return rgb;
87 }
88
89 #ifdef RGBW
90 # ifndef MIN
91 # define MIN(a, b) ((a) < (b) ? (a) : (b))
92 # endif
93 void convert_rgb_to_rgbw(LED_TYPE *led) {
94 // Determine lowest value in all three colors, put that into
95 // the white channel and then shift all colors by that amount
96 led->w = MIN(led->r, MIN(led->g, led->b));
97 led->r -= led->w;
98 led->g -= led->w;
99 led->b -= led->w;
100 }
101 #endif