-
Notifications
You must be signed in to change notification settings - Fork 0
/
HSV.cpp
74 lines (68 loc) · 2.02 KB
/
HSV.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Arduino.h>
#include "HSV.h"
word ByteHSVtoRGB(byte h, byte s, byte v)
{
// save program space
return ::HSVtoRGB((word)(h << 8), (word)(s << 8), (word)(v << 8));
}
word RGBto565(byte r, byte g, byte b)
{
// 565 rgb
return ((b & 0xF8) >> 3) |
((g & 0xFC) << 3) |
((r & 0xF8) << 8);
}
// convert Hue, Saturation & Value to Red, Green & Blue
// based on web.mit.edu/storborg/Public/hsvtorgb.c, refactored
// * 16-bit integers rather than 8-bit
// * DON'T calculate temp vars (p, q, t), only those needed for the RGB
// * no grayscale, S != 0
// * combine scaling back with building 565
word HSVtoRGB(word h, word s, word v)
{
word region;
unsigned long r, g, b, fpart;
// one of 6 hue regions
region = h / 10923L;
// remainder within region, scaled 0-65535
fpart = (h - (region * 10923L)) * 6L;
switch (region)
{
case 0:
r = v;
g = (unsigned long)(v * (65535L - ((s * (65535L - fpart)) >> 16))) >> 16; // t
b = (unsigned long)(v * (65535L - s)) >> 16; // p
break;
case 1:
r = (unsigned long)(v * (65535L - ((s * fpart) >> 16))) >> 16; // q
g = v;
b = (unsigned long)(v * (65535L - s)) >> 16; // p
break;
case 2:
r = (unsigned long)(v * (65535L - s)) >> 16; // p
g = v;
b = (unsigned long)(v * (65535L - ((s * (65535L - fpart)) >> 16))) >> 16; // t
break;
case 3:
r = (unsigned long)(v * (65535L - s)) >> 16; // p
g = (unsigned long)(v * (65535L - ((s * fpart) >> 16))) >> 16; // q
b = v;
break;
case 4:
r = (unsigned long)(v * (65535L - ((s * (65535L - fpart)) >> 16))) >> 16; // t
g = (unsigned long)(v * (65535L - s)) >> 16; // p
b = v;
break;
case 5:
r = v;
g = (unsigned long)(v * (65535L - s)) >> 16; // p
b = (unsigned long)(v * (65535L - ((s * fpart) >> 16))) >> 16; // q
break;
default:
return 0;
}
// for simplicity, just truncate
return ( b >> 11) |
((g & 0xFC00) >> 5) |
( r & 0xF800);
}