]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/color.c
[Keyboard] Doro67 cleanup (#6514)
[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     v = hsv.v;
40
41     region    = h * 6 / 255;
42     remainder = (h * 2 - region * 85) * 3;
43
44     p = (v * (255 - s)) >> 8;
45     q = (v * (255 - ((s * remainder) >> 8))) >> 8;
46     t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
47
48     switch (region) {
49         case 6:
50         case 0:
51             rgb.r = v;
52             rgb.g = t;
53             rgb.b = p;
54             break;
55         case 1:
56             rgb.r = q;
57             rgb.g = v;
58             rgb.b = p;
59             break;
60         case 2:
61             rgb.r = p;
62             rgb.g = v;
63             rgb.b = t;
64             break;
65         case 3:
66             rgb.r = p;
67             rgb.g = q;
68             rgb.b = v;
69             break;
70         case 4:
71             rgb.r = t;
72             rgb.g = p;
73             rgb.b = v;
74             break;
75         default:
76             rgb.r = v;
77             rgb.g = p;
78             rgb.b = q;
79             break;
80     }
81
82 #ifdef USE_CIE1931_CURVE
83     rgb.r = pgm_read_byte(&CIE1931_CURVE[rgb.r]);
84     rgb.g = pgm_read_byte(&CIE1931_CURVE[rgb.g]);
85     rgb.b = pgm_read_byte(&CIE1931_CURVE[rgb.b]);
86 #endif
87
88     return rgb;
89 }