-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_utils.c
executable file
·71 lines (64 loc) · 1.46 KB
/
color_utils.c
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
#include "color_utils.h"
/*******************************************************************************
* r, g, b values are from 0..255
* h = [0,360], s = [0,255], v = [0,255]
* modified from: https://www.ruinelli.ch/rgb-to-hsv
******************************************************************************/
void HSV2RGB(uint16_t hi, uint8_t si, uint8_t vi, uint8_t *ro, uint8_t *go,
uint8_t *bo) {
int i;
float f, p, q, t, h, s, v;
h = (float)hi;
s = (float)si;
v = (float)vi;
s /= 255;
if (s == 0) { // achromatic (grey)
*ro = v;
*go = v;
*bo = v;
return;
}
h /= 60; // sector 0 to 5
i = (int)h;
f = h - i; // factorial part of h
p = (unsigned char)(v * (1 - s));
q = (unsigned char)(v * (1 - s * f));
t = (unsigned char)(v * (1 - s * (1 - f)));
switch (i) {
case 0:
*ro = v;
*go = t;
*bo = p;
break;
case 1:
*ro = q;
*go = v;
*bo = p;
break;
case 2:
*ro = p;
*go = v;
*bo = t;
break;
case 3:
*ro = p;
*go = q;
*bo = v;
break;
case 4:
*ro = t;
*go = p;
*bo = v;
break;
default: // case 5:
*ro = v;
*go = p;
*bo = q;
break;
}
}
float color_map(uint16_t in, uint16_t in_min, uint16_t in_max, uint16_t out_min,
uint16_t out_max) {
float i = in;
return (i - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}