-
Notifications
You must be signed in to change notification settings - Fork 2
/
ansicol.c
308 lines (267 loc) · 7.48 KB
/
ansicol.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
/* ANSI color stuff.
* Copyright (C) 2001, Guido Flohr <[email protected]>,
* all rights reserved.
*/
#include <gem.h>
#include "ansicol.h"
/* ANSI<->VDI Color's index table
* ANSI VDI
* Black 0 1
* Red 1 10
* Green 2 11
* Yellow 3 14
* Blue 4 12
* Magenta 5 15
* Cyan 6 13
* White 7 8
* Black (bright) 0 BOLD 9
* Red (bright) 1 BOLD 2
* Green (bright) 2 BOLD 3
* Yellow (bright) 3 BOLD 6
* Blue (bright) 4 BOLD 4
* Magenta (bright) 5 BOLD 7
* Cyan (bright) 6 BOLD 5
* White (bright) 7 BOLD 0
*/
const int ansi2vdi[8] = { 1, 10, 11, 14, 12, 15, 13, 8 };
const int vdi2ansi[8] = { 7, 0, 1, 2, 4, 6, 3, 5 };
const int ansibright2vdi[8] = { 9, 2, 3, 6, 4, 7, 5, 0 };
struct rgb {
short int red, green, blue;
};
struct renderer {
int normal;
int bright;
int hbright;
unsigned long bright_effects;
unsigned long hbright_effects;
} renderer[8];
char* tw52_env = "tw52";
char* tw100_env = "tw100";
char* colorterm_env = "1";
static struct rgb ansi_colors[8 * 3] = {
{ 500, 500, 500 }, /* Bright black. */
{ 0, 0, 0 }, /* Black. */
{ 408, 408, 408 }, /* Half-bright (dimmed) black. */
{ 1000, 94, 94 }, /* Bright red. */
{ 698, 94, 94 }, /* Red. */
{ 408, 94, 94 }, /* Half-bright (dimmed) red. */
{ 94, 1000, 94 }, /* Bright green. */
{ 94, 698, 94 }, /* Green. */
{ 94, 408, 94 }, /* Half-bright (dimmed) green. */
#if 1
/* This is the Linux console "yellow". */
{ 1000, 1000, 94 }, /* Bright yellow. */
{ 698, 408, 94 }, /* Yellow. */
{ 408, 329, 94 }, /* Half-bright (dimmed) yellow. */
#else
/* This is what it would be when applying the
* rules for the other colors.
*/
{ 1000, 1000, 94 }, /* Bright yellow. */
{ 698, 698, 94 }, /* Yellow. */
{ 408, 408, 94 }, /* Half-bright (dimmed) yellow. */
#endif
{ 94, 94, 1000 }, /* Bright blue. */
{ 94, 94, 698 }, /* Blue. */
{ 94, 94, 408 }, /* Half-bright (dimmed) blue. */
{ 1000, 94, 1000 }, /* Bright magenta. */
{ 612, 94, 612 }, /* Magenta. */
{ 408, 94, 408 }, /* Half-bright (dimmed) magenta. */
{ 94, 1000, 1000 }, /* Bright cyan. */
{ 94, 578, 578 }, /* Cyan. */
{ 94, 408, 408 }, /* Half-bright (dimmed) cyan. */
{ 1000, 1000, 1000 }, /* Bright white. */
{ 698, 698, 698 }, /* White. */
{ 408, 408, 408 }, /* Half-bright (dimmed) white. */
};
static int ncolors;
static unsigned long int color_diff (const short int color1[3],
const short int color2[3]);
void
init_ansi_colors (const short* work_out)
{
int i;
struct color_diffs {
unsigned long diff;
int index;
} diffs[8 * 3];
/* First inquire all colors from vdi. */
ncolors = work_out[13];
for (i = 0; i < 8 * 3; diffs[i].index = 0,
diffs[i++].diff = 0xffffffff);
if (ncolors > 256)
ncolors = 256;
if (ncolors < 8) {
tw52_env = "tw52-m";
tw100_env = "tw100-m";
colorterm_env = "0";
}
for (i = 0; i < ncolors; ++i) {
struct rgb rgb;
int ansi_color;
vq_color (vdi_handle, i, 1, (short*) &rgb);
for (ansi_color = 0; ansi_color < 8 * 3; ++ansi_color) {
unsigned long diff =
color_diff ((short*) &rgb,
(short*)
&ansi_colors[ansi_color]);
if (diff < diffs[ansi_color].diff) {
diffs[ansi_color].index = i;
diffs[ansi_color].diff = diff;
}
}
}
/* Now fill up our renderer. */
for (i = 0; i < 8 && i < ncolors; ++i) {
renderer[i].bright = diffs[3 * i].index;
renderer[i].normal = diffs[3 * i + 1].index;
if (renderer[i].bright == renderer[i].normal)
renderer[i].bright_effects = CE_BOLD;
else
renderer[i].bright_effects = 0;
renderer[i].hbright = diffs[3 * i + 2].index;
if (renderer[i].hbright == renderer[i].normal)
renderer[i].hbright_effects = CE_LIGHT;
else
renderer[i].hbright_effects = 0;
}
for (i = 0; i < 8; ++i) {
struct rgb rgb;
/* debug ("ANSI color #%d:\n", i); */
vq_color (vdi_handle, renderer[i].bright, 1, (short*) &rgb);
/* debug (" Bright: (%04d|%04d|%04d), effects: 0x%08x\n",
rgb.red, rgb.green, rgb.blue, renderer[i].bright_effects); */
vq_color (vdi_handle, renderer[i].normal, 1, (short*) &rgb);
/* debug (" Normal: (%04d|%04d|%04d), effects: 0x%08x\n",
rgb.red, rgb.green, rgb.blue, 0); */
vq_color (vdi_handle, renderer[i].hbright, 1, (short*) &rgb);
/* debug (" Half-bright: (%04d|%04d|%04d), effects: 0x%08x\n",
rgb.red, rgb.green, rgb.blue, renderer[i].hbright_effects); */
}
}
void
set_ansi_fg_color (TEXTWIN* tw, int color)
{
color -= 48;
if (color == ANSI_DEFAULT) {
tw->curr_cattr = (tw->curr_cattr & ~CFGCOL) |
(tw->cfg->fg_color << 4);
} else if (color >= 0 && color < 8) {
tw->curr_cattr = (tw->curr_cattr & ~CFGCOL) |
(color << 4);
} else if (color == 'M') {
tw->curr_cattr = (tw->curr_cattr & ~CE_ANSI_EFFECTS) |
CE_BOLD;
} else if (color == 'N') {
tw->curr_cattr = (tw->curr_cattr & ~CE_ANSI_EFFECTS) |
CE_LIGHT;
}
}
void
set_ansi_bg_color (TEXTWIN* tw, int color)
{
color -= 48;
if (color == ANSI_DEFAULT || (color >= 0 && color < 8))
tw->curr_cattr = (tw->curr_cattr & ~CBGCOL) | color;
}
/* Calculate the difference between two colors. */
static unsigned long int
color_diff (const short int color1[3], const short int color2[3])
{
unsigned long int diff = 0;
unsigned long int r1 = color1[0];
unsigned long int r2 = color2[0];
unsigned long int g1 = color1[1];
unsigned long int g2 = color2[1];
unsigned long int b1 = color1[2];
unsigned long int b2 = color2[2];
short max1 = 0;
short max2 = 0;
short min1 = 1000;
short min2 = 1000;
unsigned long int s1, s2;
int i;
diff += (r2 - r1) * (r2 - r1);
diff += (g2 - g1) * (g2 - g1);
diff += (b2 - b1) * (b2 - b1);
for (i = 0; i < 3; i++) {
if (color1[i] > max1)
max1 = color1[i];
if (color2[i] > max2)
max2 = color2[i];
if (color1[i] < min1)
min1 = color1[i];
if (color2[i] < min2)
min2 = color2[i];
}
s1 = max1 - min1;
s2 = max2 - min2;
diff += (s2 - s1) * (s2 - s1);
return diff;
}
void
use_ansi_colors (TEXTWIN* tw, unsigned long flag,
int* fgcolor, int* bgcolor,
int* texteffects)
{
*bgcolor = flag & CBGCOL;
*fgcolor = (flag & CFGCOL) >> 4;
*texteffects = flag & CEFFECTS;
if (!tw->vdi_colors) {
*texteffects &= ~CE_ANSI_EFFECTS;
if (*fgcolor == ANSI_DEFAULT) {
*fgcolor = tw->cfg->fg_color;
if (tw->cfg->fg_effects & CE_BOLD)
flag |= CE_BOLD;
else if (tw->cfg->fg_effects & CE_LIGHT)
flag |= CE_LIGHT;
}
if (*fgcolor >= 0 && *fgcolor <= 7) {
*texteffects &= ~CE_ANSI_EFFECTS;
if (flag & CE_BOLD) {
*texteffects |=
renderer[*fgcolor].bright_effects;
*fgcolor = renderer[*fgcolor].bright;
} else if (flag & CE_LIGHT) {
*texteffects |=
renderer[*fgcolor].hbright_effects;
*fgcolor = renderer[*fgcolor].hbright;
} else {
*fgcolor = renderer[*fgcolor].normal;
}
} else {
*fgcolor &= 0xf;
}
if (*bgcolor >= 0 && *bgcolor <= 7) {
*bgcolor = renderer[*bgcolor].normal;
} else if (*bgcolor == ANSI_DEFAULT) {
*bgcolor = tw->cfg->bg_color & 0x7;
if (tw->cfg->bg_effects & CE_BOLD)
*bgcolor = renderer[*bgcolor].bright;
else if (tw->cfg->bg_effects & CE_LIGHT)
*bgcolor = renderer[*bgcolor].hbright;
else
*bgcolor = renderer[*bgcolor].normal;
} else {
*bgcolor &= 0xf;
}
}
if (flag & (CINVERSE|CSELECTED)) {
*fgcolor ^= *bgcolor;
*bgcolor ^= *fgcolor;
*fgcolor ^= *bgcolor;
}
*texteffects >>= 8;
}
#if 0
int
get_ansi_color (color)
int color;
{
if (color < 0 || color > 7)
return 0;
else
return renderer[color].normal;
}
#endif