-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcolor_cache.c
668 lines (513 loc) · 14.7 KB
/
color_cache.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include <unistd.h>
#include <stdlib.h>
#include <inttypes.h>
#include <stdio.h>
#include "macros.h"
#include "nc_wrapper.h"
#include "vterm.h"
#include "vterm_private.h"
#include "vterm_buffer.h"
#include "color_math.h"
#include "color_cache.h"
#include "utlist.h"
#define PAIR_FG_R(_pair_ptr) (_pair_ptr->rgb_values[0].r)
#define PAIR_FG_G(_pair_ptr) (_pair_ptr->rgb_values[0].g)
#define PAIR_FG_B(_pair_ptr) (_pair_ptr->rgb_values[0].b)
#define PAIR_BG_R(_pair_ptr) (_pair_ptr->rgb_values[1].r)
#define PAIR_BG_G(_pair_ptr) (_pair_ptr->rgb_values[1].g)
#define PAIR_BG_B(_pair_ptr) (_pair_ptr->rgb_values[1].b)
#define C16(a, b, c, d) b,
short rRGB[] = {
#include "c16_color.def"
};
#undef C16
#define C16(a, b, c, d) c,
short gRGB[] = {
#include "c16_color.def"
};
#undef C16
#define C16(a, b, c, d) d,
short bRGB[] = {
#include "c16_color.def"
};
#undef C16
void
_color_cache_reset_pair(color_pair_t *pair);
void
_color_cache_profile_pair(color_pair_t *pair);
void
color_cache_init(void)
{
extern color_cache_t *color_cache;
/*
color cache has already be initialized by another instance
so just increment the ref count and return.
*/
if(color_cache != NULL)
{
color_cache->ref_count++;
return;
}
color_cache = (color_cache_t *)calloc(1, sizeof(color_cache_t));
color_cache->ref_count = 1;
color_cache->term_colors = tigetnum("colors");
color_cache->term_pairs = tigetnum("pairs");
// clamp max pairs at 0x7FFF
#ifdef NCURSES_EXT_COLORS
if(color_cache->term_pairs > 0x7FFF)
color_cache->term_pairs = 0x7FFF;
#else
if(color_cache->term_pairs > 0xFF)
color_cache->term_pairs = 0xFF;
#endif
// take a snapshot of the current palette
color_cache_save_palette(PALETTE_HOST);
color_cache_copy_palette(PALETTE_HOST, PALETTE_ACTIVE);
return;
}
void
color_cache_copy_palette(int source, int target)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
if(color_cache->head[source] == NULL) return;
// if the target cache is already used, free it
if(color_cache->head[target] != NULL)
{
color_cache_free_palette(target);
}
CDL_FOREACH(color_cache->head[source], pair)
{
tmp1 = (color_pair_t *)malloc(sizeof(color_pair_t));
memcpy(tmp1, pair, sizeof(color_pair_t));
CDL_APPEND(color_cache->head[target], tmp1);
}
return;
}
void
color_cache_save_palette(int cache_id)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
int i;
if(color_cache->head[cache_id] != NULL)
{
// free the saved palette if it exists
color_cache_free_palette(cache_id);
}
color_cache->reserved_pair = -1;
// profile all colors
for(i = 0; i < color_cache->term_pairs; i++)
{
pair = (color_pair_t *)calloc(1, sizeof(color_pair_t));
pair->num = i;
_color_cache_profile_pair(pair);
// reserve one pair where fg 0 and bg 0
if(pair->fg == 0 && pair->bg == 0)
{
if(color_cache->reserved_pair == -1)
{
color_cache->reserved_pair = i;
}
}
CDL_APPEND(color_cache->head[cache_id], pair);
}
return;
}
void
color_cache_load_palette(int cache_id)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
if(color_cache->head[cache_id] != NULL)
{
// free the default palette (the active palette)
color_cache_free_palette(cache_id);
// copy the specified palette into
CDL_FOREACH(color_cache->head[cache_id], pair)
{
tmp1 = (color_pair_t *)malloc(sizeof(color_pair_t));
memcpy(tmp1, pair, sizeof(color_pair_t));
CDL_PREPEND(color_cache->head[cache_id], tmp1);
ncw_init_pair(pair->num, pair->fg, pair->bg);
}
}
return;
}
void
color_cache_free_palette(int cache_id)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
color_pair_t *tmp2;
if(color_cache->head[cache_id] != NULL)
{
CDL_FOREACH_SAFE(color_cache->head[cache_id], pair, tmp1, tmp2)
{
CDL_DELETE(color_cache->head[cache_id], pair);
free(pair);
}
color_cache->head[cache_id] = NULL;
}
return;
}
short
color_cache_find_unused_pair()
{
extern color_cache_t *color_cache;
color_pair_t pair;
short i;
int retval;
i = color_cache->term_pairs - 1;
// start backward looking for an unenumerated pair.
while(i > 0)
{
memset(&pair, 0, sizeof(color_pair_t));
retval = ncw_pair_content(i, &pair.fg, &pair.bg);
// if we can't explode the pair, keep searching
if(retval == -1)
{
i--;
continue;
}
// if this is the reserved pair, skip
if(i == color_cache->reserved_pair)
{
i--;
continue;
}
/*
FG and BG of zero would be a truly odd pair and almost certianly
indicates an unused pair.
*/
if(pair.fg == 0 && pair.bg == 0) break;
i--;
}
return i;
}
short
color_cache_find_lru_pair()
{
extern color_cache_t *color_cache;
color_pair_t *pair;
pair = color_cache->head[PALETTE_ACTIVE];
for(;;)
{
// break out of an infitite loop scenario
if(pair->prev == color_cache->head[PALETTE_ACTIVE])
{
// no free pairs
return -1;
}
pair = pair->prev;
// if this is the reserve pair, keep going
if(pair->num == color_cache->reserved_pair) continue;
/*
From the ncurses man pages:
color pair 0 is special; it denotes “no color”.
Color pair 0 is assumed to be white on black, but is
actually whatever the terminal implements before color is
initialized. It cannot be modified by the application.
*/
if(pair->num == 0) continue;
// make sure the pair is not part of the system palette
if(pair->custom == TRUE) break;
}
return pair->num;
}
// use the pair at the end of the list as it's the lowest risk
int
color_cache_add_pair(vterm_t *origin, short fg, short bg)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
short i;
i = color_cache_find_unused_pair();
if(i <= 0)
{
i = color_cache_find_lru_pair();
}
// we've exhausted all options. return pair 0.
if(i <= 0) return 0;
CDL_SEARCH_SCALAR(color_cache->head[PALETTE_ACTIVE], pair, num, i);
if(pair != NULL)
{
CDL_DELETE(color_cache->head[PALETTE_ACTIVE], pair);
pair->origin = origin;
pair->custom = TRUE;
pair->num = i;
ncw_init_pair(pair->num, fg, bg);
_color_cache_profile_pair(pair);
CDL_PREPEND(color_cache->head[PALETTE_ACTIVE], pair);
return i;
}
return 0;
}
void
color_cache_free_pairs(vterm_t *origin)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
if(color_cache == NULL) return;
if(origin == NULL) return;
CDL_FOREACH(color_cache->head[PALETTE_ACTIVE], pair)
{
// safety check. pair should never be a system pair
if(pair->custom == FALSE) continue;
// only origin matters
if(pair->origin == origin)
{
_color_cache_reset_pair(pair);
}
}
return;
}
void
color_cache_release(void)
{
extern color_cache_t *color_cache;
int i;
color_cache->ref_count--;
// if ref count is not zero return
if(color_cache->ref_count > 0) return;
for(i = 0; i < PALETTE_MAX; i++)
{
color_cache_free_palette(i);
}
free(color_cache);
color_cache = NULL;
return;
}
void
vterm_set_pair_selector(vterm_t *vterm, VtermPairSelect ps)
{
int default_color = 0;
if(vterm == NULL) return;
// todo: in the future, make a NULL value revert to defaults
if(ps == NULL) return;
vterm->pair_select = ps;
/*
if the user has specified NOCURSES, we need to use the
new routine to locate the white on black color pair.
*/
if(vterm->flags & VTERM_FLAG_NOCURSES)
{
default_color = vterm->pair_select(vterm, COLOR_WHITE, COLOR_BLACK);
if(default_color < 0 || default_color > 255)
default_color = 0;
vterm->vterm_desc[0].curattr = (default_color & 0xff) << 8;
}
return;
}
VtermPairSelect
vterm_get_pair_selector(vterm_t *vterm)
{
if(vterm == NULL) return NULL;
return vterm->pair_select;
}
void
vterm_set_pair_splitter(vterm_t *vterm, VtermPairSplit ps)
{
if(vterm == NULL) return;
if(ps == NULL) return;
vterm->pair_split = ps;
return;
}
VtermPairSplit
vterm_get_pair_splitter(vterm_t *vterm)
{
if(vterm == NULL) return NULL;
return vterm->pair_split;
}
int
vterm_set_colors(vterm_t *vterm, short fg, short bg)
{
vterm_desc_t *v_desc = NULL;
long colors;
int idx;
if(vterm == NULL) return -1;
// set vterm description buffer selector
idx = vterm_buffer_get_active(vterm);
v_desc = &vterm->vterm_desc[idx];
#ifdef NOCURSES
if(has_colors() == FALSE) return -1;
#endif
colors = color_cache_find_pair(fg, bg);
if(colors == -1) colors = 0;
v_desc->default_colors = (short)colors;
return 0;
}
long
vterm_get_colors(vterm_t *vterm)
{
vterm_desc_t *v_desc = NULL;
int idx;
if(vterm == NULL) return -1;
// set vterm description buffer selector
idx = vterm_buffer_get_active(vterm);
v_desc = &vterm->vterm_desc[idx];
#ifndef NOCURSES
if(has_colors() == FALSE) return -1;
#endif
return v_desc->default_colors;
}
int
color_cache_find_exact_color(short r, short g, short b)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
color_pair_t *tmp2;
bool fg_found = FALSE;
bool bg_found = FALSE;
CDL_FOREACH_SAFE(color_cache->head[PALETTE_ACTIVE], pair, tmp1, tmp2)
{
/*
we're searching for a color that contains a specific RGB
composition. it doesn't matter whether that color is part
of the foreground or background color of the pair. the
color number is the same no matter what.
*/
// check the foreground color for a match
if(pair->rgb_values[0].r == r &&
pair->rgb_values[0].g == g &&
pair->rgb_values[0].b == b)
{
fg_found = TRUE;
}
else
{
fg_found = FALSE;
continue;
}
// check the background color for a match
if(pair->rgb_values[1].r == r &&
pair->rgb_values[1].g == g &&
pair->rgb_values[1].b == b)
{
bg_found = TRUE;
}
else
{
bg_found = FALSE;
continue;
}
if(fg_found == TRUE && bg_found == TRUE)
{
CDL_DELETE(color_cache->head[PALETTE_ACTIVE], pair);
break;
}
}
if(fg_found == FALSE || bg_found == FALSE) return -1;
/*
push the pair to the front of the list to make subseqent look-ups
faster.
*/
CDL_PREPEND(color_cache->head[PALETTE_ACTIVE], pair);
return pair->num;
}
int
color_cache_find_pair(short fg, short bg)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
color_pair_t *tmp2;
// iterate through the cache looking for a match
CDL_FOREACH_SAFE(color_cache->head[PALETTE_ACTIVE], pair, tmp1, tmp2)
{
if(pair->fg == fg && pair->bg == bg)
{
// unlink the node so we can prepend it
CDL_DELETE(color_cache->head[PALETTE_ACTIVE], pair);
/*
push the pair to the front of the list to make subseqent
look-ups faster.
*/
CDL_PREPEND(color_cache->head[PALETTE_ACTIVE], pair);
return pair->num;
}
}
return -1;
}
int
color_cache_split_pair(int pair_num, short *fg, short *bg)
{
extern color_cache_t *color_cache;
color_pair_t *pair;
color_pair_t *tmp1;
color_pair_t *tmp2;
bool found = FALSE;
CDL_FOREACH_SAFE(color_cache->head[PALETTE_ACTIVE], pair, tmp1, tmp2)
{
if(pair->num == pair_num)
{
CDL_DELETE(color_cache->head[PALETTE_ACTIVE], pair);
found = TRUE;
break;
}
}
if(found == FALSE)
{
*fg = -1;
*bg = -1;
return -1;
}
CDL_PREPEND(color_cache->head[PALETTE_ACTIVE], pair);
*fg = pair->fg;
*bg = pair->bg;
return 0;
}
void
_color_cache_profile_pair(color_pair_t *pair)
{
int r, g, b;
if(pair == NULL) return;
// explode pair
ncw_pair_content(pair->num, &pair->fg, &pair->bg);
// extract foreground RGB
ncw_color_content(pair->fg, &r, &g, &b);
pair->rgb_values[0].r = r;
pair->rgb_values[0].g = g;
pair->rgb_values[0].b = b;
rgb2hsl(RGB_FLOAT(NCURSES_RGB(r), NCURSES_RGB(g), NCURSES_RGB(b)),
&pair->hsl_values[0].h,
&pair->hsl_values[0].s,
&pair->hsl_values[0].l);
// store the CIE2000 lab foreground values
// rgb2lab(RGB_FLOAT(NCURSES_RGB(r), NCURSES_RGB(g), NCURSES_RGB(b)),
// &pair->cie_values[0].l,
// &pair->cie_values[0].a,
// &pair->cie_values[0].b);
// extract background RGB
ncw_color_content(pair->bg, &r, &g, &b);
pair->rgb_values[1].r = r;
pair->rgb_values[1].g = g;
pair->rgb_values[1].b = b;
// store the HLS background values
rgb2hsl(RGB_FLOAT(NCURSES_RGB(r), NCURSES_RGB(g), NCURSES_RGB(b)),
&pair->hsl_values[1].h,
&pair->hsl_values[1].s,
&pair->hsl_values[1].l);
// store the CIE2000 lab background values
// rgb2lab(RGB_FLOAT(NCURSES_RGB(r), NCURSES_RGB(g), NCURSES_RGB(b)),
// &pair->cie_values[1].l,
// &pair->cie_values[1].a,
// &pair->cie_values[1].b);
return;
}
void
_color_cache_reset_pair(color_pair_t *pair)
{
if(pair == NULL) return;
pair->origin = NULL;
pair->fg = 0;
pair->bg = 0;
memset(pair->rgb_values, 0, sizeof(pair->rgb_values));
memset(pair->hsl_values, 0, sizeof(pair->hsl_values));
ncw_init_pair(pair->num, 0, 0);
return;
}