-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_to_xterm256.h
42 lines (34 loc) · 1.39 KB
/
rgb_to_xterm256.h
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
unsigned char
rgb_to_xterm256(unsigned char r, unsigned char g, unsigned char b) {
unsigned int color_id = 0;
unsigned char half_color = (r >= 0x80 && r < 0x87) << 2 |
(g >= 0x80 && g < 0x87) << 1 |
(b >= 0x80 && b < 0x87);
static const unsigned char gray_brightness[] = {
0x00, 0x08, 0x12, 0x1c, 0x26, 0x30, 0x3a, 0x44, 0x4e, 0x58, 0x5f,
0x62, 0x6c, 0x76, 0x80, 0x87, 0x8a, 0x94, 0x9e, 0xa8, 0xaf, 0xb2,
0xbc, 0xc0, 0xc6, 0xd0, 0xd7, 0xda, 0xe4, 0xee, 0xff
};
static const unsigned char gray_colors[] = {
0x00, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0x3b,
0xf1, 0xf2, 0xf3, 0xf4, 0x66, 0xf5, 0xf6, 0xf7, 0xf8, 0x91, 0xf9,
0xfa, 0x07, 0xfb, 0xfc, 0xbc, 0xfd, 0xfe, 0xff, 0x0f
};
static const unsigned char term_colors[] = { 0x04, 0x02, 0x06,
0x01, 0x05, 0x03 };
if (r == g && g == b) {
unsigned char i = 0;
while (gray_brightness[i] < r) {
i++;
}
color_id = gray_colors[i];
} else if (half_color) {
color_id = term_colors[half_color - 1];
} else {
r = ((r << 2) + r) / 255;
g = ((g << 2) + g) / 255;
b = ((b << 2) + b) / 255;
color_id = 16 + ((r << 5) + (r << 2)) + ((g << 2) + (g << 1)) + b;
}
return color_id;
}