forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.c
106 lines (96 loc) · 2.51 KB
/
colors.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
#include <stdbool.h>
#include <stdlib.h>
#include <curses.h>
#include "editor.h"
#include "util.h"
#ifdef NCURSES_VERSION
# ifndef NCURSES_EXT_COLORS
# define NCURSES_EXT_COLORS 0
# endif
# if !NCURSES_EXT_COLORS
# define MAX_COLOR_PAIRS 256
# endif
#endif
#ifndef MAX_COLOR_PAIRS
# define MAX_COLOR_PAIRS COLOR_PAIRS
#endif
static bool has_default_colors;
static short *color2palette, default_fg, default_bg;
static short color_pairs_reserved, color_pairs_max, color_pair_current;
static unsigned int color_hash(short fg, short bg)
{
if (fg == -1)
fg = COLORS;
if (bg == -1)
bg = COLORS + 1;
return fg * (COLORS + 2) + bg;
}
short editor_color_get(short fg, short bg)
{
if (fg >= COLORS)
fg = default_fg;
if (bg >= COLORS)
bg = default_bg;
if (!has_default_colors) {
if (fg == -1)
fg = default_fg;
if (bg == -1)
bg = default_bg;
}
if (!color2palette || (fg == -1 && bg == -1))
return 0;
unsigned int index = color_hash(fg, bg);
if (color2palette[index] == 0) {
short oldfg, oldbg;
for (;;) {
if (++color_pair_current >= color_pairs_max)
color_pair_current = color_pairs_reserved + 1;
pair_content(color_pair_current, &oldfg, &oldbg);
unsigned int old_index = color_hash(oldfg, oldbg);
if (color2palette[old_index] >= 0) {
if (init_pair(color_pair_current, fg, bg) == OK) {
color2palette[old_index] = 0;
color2palette[index] = color_pair_current;
}
break;
}
}
}
short color_pair = color2palette[index];
return color_pair >= 0 ? color_pair : -color_pair;
}
short editor_color_reserve(short fg, short bg)
{
if (!color2palette)
editor_init();
if (!color2palette || fg >= COLORS || bg >= COLORS)
return 0;
if (!has_default_colors && fg == -1)
fg = default_fg;
if (!has_default_colors && bg == -1)
bg = default_bg;
if (fg == -1 && bg == -1)
return 0;
unsigned int index = color_hash(fg, bg);
if (color2palette[index] >= 0) {
if (init_pair(++color_pairs_reserved, fg, bg) == OK)
color2palette[index] = -color_pairs_reserved;
}
short color_pair = color2palette[index];
return color_pair >= 0 ? color_pair : -color_pair;
}
void editor_init(void)
{
if (color2palette)
return;
pair_content(0, &default_fg, &default_bg);
if (default_fg == -1)
default_fg = COLOR_WHITE;
if (default_bg == -1)
default_bg = COLOR_BLACK;
has_default_colors = (use_default_colors() == OK);
color_pairs_max = MIN(COLOR_PAIRS, MAX_COLOR_PAIRS);
if (COLORS)
color2palette = calloc((COLORS + 2) * (COLORS + 2), sizeof(short));
editor_color_reserve(COLOR_WHITE, COLOR_BLACK);
}