-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcolor_cache.h
More file actions
157 lines (118 loc) · 4.74 KB
/
Copy pathcolor_cache.h
File metadata and controls
157 lines (118 loc) · 4.74 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef _COLOR_CACHE_H_
#define _COLOR_CACHE_H_
#include <signal.h>
#include "vterm.h"
#define C16(a, b, c, d) a,
enum {
#include "c16_color.def"
};
#undef C16
/*
ncurses RGB values range from 0 to 1000 while the rest of the world
pretty much uses 0 - 255 so we need to scale.
*/
#define NCURSES_RGB(x) (((float)x / 1000.0) * 255.0)
enum
{
PALETTE_HOST = 0x00,
PALETTE_ACTIVE,
PALETTE_MAX // not a real palette. just a counter.
};
struct _rgb_values_s
{
short r;
short g;
short b;
};
typedef struct _rgb_values_s rgb_values_t;
typedef struct _color_pair_s color_pair_t;
struct _color_pair_s
{
int num;
int fg;
int bg;
rgb_values_t rgb_values[2];
vterm_t *origin; /*
indicates which instance
added the pair to the
global table.
*/
bool custom; /*
indicates if the pair was
loaded from the host
palette.
*/
color_pair_t *next;
color_pair_t *prev;
};
struct _color_cache_s
{
sig_atomic_t ref_count; /*
incremented when an new
vterm instance is allocated.
it's decremented when a
vterm instance is destroyed.
when it reaches zero, all
resources are freed.
use atomic type to prevent
race condition.
*/
int pair_count;
int reserved_pair;
int term_colors;
int term_pairs;
color_pair_t *head[PALETTE_MAX];
};
typedef struct _color_cache_s color_cache_t;
void
color_cache_init(void);
void
color_cache_release(void);
void
color_cache_save_palette(int cache_id);
void
color_cache_copy_palette(int source, int target);
void
color_cache_load_palette(int cache_id);
void
color_cache_free_palette(int cache_id);
int
color_cache_add_pair(vterm_t *origin, short fg, short bg);
void
color_cache_free_pairs(vterm_t *origin);
short
color_cache_find_lru_pair(void);
short
color_cache_find_unused_pair(void);
int
color_cache_find_pair(short fg, short bg);
int
color_cache_find_exact_color(short r, short g, short b);
int
color_cache_find_nearest_color(short r, short g, short b);
int
color_cache_split_pair(int pair_num, short *fg, short *bg);
#define DEBUG_COLOR_PAIRS(cache, max) \
{ \
color_pair_t *_pair; \
int _limit = max; \
\
CDL_FOREACH((cache)->head, _pair) \
{ \
if(_limit == 0) break; \
printf("Pair Num: %d\n\r", _pair->num); \
printf(" Fg: %d\n\r", _pair->fg); \
printf(" F RGB: r: %d, g: %d, b: %d\n\r", \
_pair->rgb_values[0].r, \
_pair->rgb_values[0].g, \
_pair->rgb_values[0].b); \
printf(" Bg: %d\n\r", _pair->bg); \
printf(" B RGB: r: %d, g: %d, b: %d\n\r", \
_pair->rgb_values[1].r, \
_pair->rgb_values[1].g, \
_pair->rgb_values[1].b); \
_limit--; \
} \
printf("cached pairs: %d\n\r", (cache)->pair_count); \
}
#endif