-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.cpp
44 lines (37 loc) · 1.13 KB
/
color.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "HamClock.h"
/* HSV to RGB conversion function with only integer math.
* http://web.mit.edu/storborg/Public/hsvtorgb.c
*/
void hsvtorgb(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t h, uint8_t s, uint8_t v)
{
uint8_t region, fpart, p, q, t;
if(s == 0) {
/* color is grayscale */
*r = *g = *b = v;
return;
}
/* make hue 0-5 */
region = h / 43;
/* find remainder part, make it from 0-255 */
fpart = (h - (region * 43)) * 6;
/* calculate temp vars, doing integer multiplication */
p = (v * (255 - s)) >> 8;
q = (v * (255 - ((s * fpart) >> 8))) >> 8;
t = (v * (255 - ((s * (255 - fpart)) >> 8))) >> 8;
/* assign temp vars based on color cone region */
switch(region) {
case 0:
*r = v; *g = t; *b = p; break;
case 1:
*r = q; *g = v; *b = p; break;
case 2:
*r = p; *g = v; *b = t; break;
case 3:
*r = p; *g = q; *b = v; break;
case 4:
*r = t; *g = p; *b = v; break;
default:
*r = v; *g = p; *b = q; break;
}
return;
}