Update Dvorak, Colemak and Workman keycode aliases (#8217)
[jackhill/qmk/firmware.git] / quantum / color.c
CommitLineData
14b7602a
JH
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
14b7602a
JH
17#include "color.h"
18#include "led_tables.h"
19#include "progmem.h"
20
b624f32f 21RGB hsv_to_rgb(HSV hsv) {
22 RGB rgb;
23 uint8_t region, remainder, p, q, t;
24 uint16_t h, s, v;
14b7602a 25
b624f32f 26 if (hsv.s == 0) {
f11fde9b 27#ifdef USE_CIE1931_CURVE
b624f32f 28 rgb.r = rgb.g = rgb.b = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
f11fde9b 29#else
b624f32f 30 rgb.r = hsv.v;
31 rgb.g = hsv.v;
32 rgb.b = hsv.v;
f11fde9b 33#endif
b624f32f 34 return rgb;
35 }
14b7602a 36
b624f32f 37 h = hsv.h;
38 s = hsv.s;
095b88bc
X
39#ifdef USE_CIE1931_CURVE
40 v = pgm_read_byte(&CIE1931_CURVE[hsv.v]);
41#else
b624f32f 42 v = hsv.v;
095b88bc 43#endif
14b7602a 44
b624f32f 45 region = h * 6 / 255;
46 remainder = (h * 2 - region * 85) * 3;
14b7602a 47
b624f32f 48 p = (v * (255 - s)) >> 8;
49 q = (v * (255 - ((s * remainder) >> 8))) >> 8;
50 t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
14b7602a 51
b624f32f 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 }
14b7602a 85
b624f32f 86 return rgb;
14b7602a 87}
7ba6456c
DJ
88
89#ifdef RGBW
667045b4
JC
90# ifndef MIN
91# define MIN(a, b) ((a) < (b) ? (a) : (b))
92# endif
7ba6456c
DJ
93void 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