1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc. *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
34
35 /* lib_color.c
36 *
37 * Handles color emulation of SYS V curses
38 */
39
40 #include <curses.priv.h>
41
42 #include <term.h>
43 #include <tic.h>
44
45 MODULE_ID("$Id: lib_color.c,v 1.75 2005/06/18 20:19:08 tom Exp $")
46
47 /*
48 * These should be screen structure members. They need to be globals for
49 * historical reasons. So we assign them in start_color() and also in
50 * set_term()'s screen-switching logic.
51 */
52 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
53 NCURSES_EXPORT_VAR(int) COLORS = 0;
54
55 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
56
57 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
58
59 /*
60 * Given a RGB range of 0..1000, we'll normally set the individual values
61 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
62 */
63 #define RGB_ON 680
64 #define RGB_OFF 0
65 /* *INDENT-OFF* */
66 static const color_t cga_palette[] =
67 {
68 /* R G B */
69 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
70 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
71 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
72 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
73 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
74 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
75 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
76 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
77 };
78
79 static const color_t hls_palette[] =
80 {
81 /* H L S */
82 DATA( 0, 0, 0), /* COLOR_BLACK */
83 DATA( 120, 50, 100), /* COLOR_RED */
84 DATA( 240, 50, 100), /* COLOR_GREEN */
85 DATA( 180, 50, 100), /* COLOR_YELLOW */
86 DATA( 330, 50, 100), /* COLOR_BLUE */
87 DATA( 60, 50, 100), /* COLOR_MAGENTA */
88 DATA( 300, 50, 100), /* COLOR_CYAN */
89 DATA( 0, 50, 100), /* COLOR_WHITE */
90 };
91 /* *INDENT-ON* */
92
93 #if NCURSES_EXT_FUNCS
94 /*
95 * These are called from _nc_do_color(), which in turn is called from
96 * vidattr - so we have to assume that SP may be null.
97 */
98 static int
default_fg(void)99 default_fg(void)
100 {
101 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
102 }
103
104 static int
default_bg(void)105 default_bg(void)
106 {
107 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
108 }
109 #else
110 #define default_fg() COLOR_WHITE
111 #define default_bg() COLOR_BLACK
112 #endif
113
114 /*
115 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
116 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
117 * also used in the FreeBSD syscons.
118 */
119 static int
toggled_colors(int c)120 toggled_colors(int c)
121 {
122 if (c < 16) {
123 static const int table[] =
124 {0, 4, 2, 6, 1, 5, 3, 7,
125 8, 12, 10, 14, 9, 13, 11, 15};
126 c = table[c];
127 }
128 return c;
129 }
130
131 static void
set_background_color(int bg,int (* outc)(int))132 set_background_color(int bg, int (*outc) (int))
133 {
134 if (set_a_background) {
135 TPUTS_TRACE("set_a_background");
136 tputs(tparm(set_a_background, bg), 1, outc);
137 } else {
138 TPUTS_TRACE("set_background");
139 tputs(tparm(set_background, toggled_colors(bg)), 1, outc);
140 }
141 }
142
143 static void
set_foreground_color(int fg,int (* outc)(int))144 set_foreground_color(int fg, int (*outc) (int))
145 {
146 if (set_a_foreground) {
147 TPUTS_TRACE("set_a_foreground");
148 tputs(tparm(set_a_foreground, fg), 1, outc);
149 } else {
150 TPUTS_TRACE("set_foreground");
151 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
152 }
153 }
154
155 static void
init_color_table(void)156 init_color_table(void)
157 {
158 const color_t *tp;
159 int n;
160
161 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
162 for (n = 0; n < COLORS; n++) {
163 if (n < 8) {
164 SP->_color_table[n] = tp[n];
165 } else {
166 SP->_color_table[n] = tp[n % 8];
167 if (hue_lightness_saturation) {
168 SP->_color_table[n].green = 100;
169 } else {
170 if (SP->_color_table[n].red)
171 SP->_color_table[n].red = 1000;
172 if (SP->_color_table[n].green)
173 SP->_color_table[n].green = 1000;
174 if (SP->_color_table[n].blue)
175 SP->_color_table[n].blue = 1000;
176 }
177 }
178 }
179 }
180
181 /*
182 * Reset the color pair, e.g., to whatever color pair 0 is.
183 */
184 static bool
reset_color_pair(void)185 reset_color_pair(void)
186 {
187 bool result = FALSE;
188
189 if (orig_pair != 0) {
190 TPUTS_TRACE("orig_pair");
191 putp(orig_pair);
192 result = TRUE;
193 }
194 return result;
195 }
196
197 /*
198 * Reset color pairs and definitions. Actually we do both more to accommodate
199 * badly-written terminal descriptions than for the relatively rare case where
200 * someone has changed the color definitions.
201 */
202 bool
_nc_reset_colors(void)203 _nc_reset_colors(void)
204 {
205 int result = FALSE;
206
207 T((T_CALLED("_nc_reset_colors()")));
208 if (SP->_color_defs > 0)
209 SP->_color_defs = -(SP->_color_defs);
210
211 if (reset_color_pair())
212 result = TRUE;
213 if (orig_colors != 0) {
214 TPUTS_TRACE("orig_colors");
215 putp(orig_colors);
216 result = TRUE;
217 }
218 returnBool(result);
219 }
220
221 NCURSES_EXPORT(int)
start_color(void)222 start_color(void)
223 {
224 int result = ERR;
225
226 T((T_CALLED("start_color()")));
227
228 if (SP == 0) {
229 result = ERR;
230 } else if (SP->_coloron) {
231 result = OK;
232 } else {
233
234 if (reset_color_pair() != TRUE) {
235 set_foreground_color(default_fg(), _nc_outch);
236 set_background_color(default_bg(), _nc_outch);
237 }
238
239 if (max_pairs > 0 && max_colors > 0) {
240 COLOR_PAIRS = SP->_pair_count = max_pairs;
241 COLORS = SP->_color_count = max_colors;
242
243 if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
244 max_pairs)) != 0) {
245 if ((SP->_color_table = TYPE_CALLOC(color_t,
246 max_colors)) != 0) {
247 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
248 init_color_table();
249
250 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
251 COLORS, COLOR_PAIRS));
252
253 SP->_coloron = 1;
254 result = OK;
255 } else if (SP->_color_pairs != 0) {
256 FreeAndNull(SP->_color_pairs);
257 }
258 }
259 } else {
260 result = OK;
261 }
262 }
263 returnCode(result);
264 }
265
266 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
267 static void
rgb2hls(short r,short g,short b,short * h,short * l,short * s)268 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
269 /* convert RGB to HLS system */
270 {
271 short min, max, t;
272
273 if ((min = g < r ? g : r) > b)
274 min = b;
275 if ((max = g > r ? g : r) < b)
276 max = b;
277
278 /* calculate lightness */
279 *l = (min + max) / 20;
280
281 if (min == max) { /* black, white and all shades of gray */
282 *h = 0;
283 *s = 0;
284 return;
285 }
286
287 /* calculate saturation */
288 if (*l < 50)
289 *s = ((max - min) * 100) / (max + min);
290 else
291 *s = ((max - min) * 100) / (2000 - max - min);
292
293 /* calculate hue */
294 if (r == max)
295 t = 120 + ((g - b) * 60) / (max - min);
296 else if (g == max)
297 t = 240 + ((b - r) * 60) / (max - min);
298 else
299 t = 360 + ((r - g) * 60) / (max - min);
300
301 *h = t % 360;
302 }
303
304 /*
305 * Extension (1997/1/18) - Allow negative f/b values to set default color
306 * values.
307 */
308 NCURSES_EXPORT(int)
init_pair(short pair,short f,short b)309 init_pair(short pair, short f, short b)
310 {
311 colorpair_t result;
312
313 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
314
315 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
316 returnCode(ERR);
317 #if NCURSES_EXT_FUNCS
318 if (SP->_default_color) {
319 if (f < 0)
320 f = COLOR_DEFAULT;
321 if (b < 0)
322 b = COLOR_DEFAULT;
323 if (f >= COLORS && !isDefaultColor(f))
324 returnCode(ERR);
325 if (b >= COLORS && !isDefaultColor(b))
326 returnCode(ERR);
327 } else
328 #endif
329 {
330 if ((f < 0) || (f >= COLORS)
331 || (b < 0) || (b >= COLORS)
332 || (pair < 1))
333 returnCode(ERR);
334 }
335
336 /*
337 * When a pair's content is changed, replace its colors (if pair was
338 * initialized before a screen update is performed replacing original
339 * pair colors with the new ones).
340 */
341 result = PAIR_OF(f, b);
342 if (SP->_color_pairs[pair] != 0
343 && SP->_color_pairs[pair] != result) {
344 int y, x;
345
346 for (y = 0; y <= curscr->_maxy; y++) {
347 struct ldat *ptr = &(curscr->_line[y]);
348 bool changed = FALSE;
349 for (x = 0; x <= curscr->_maxx; x++) {
350 if (GetPair(ptr->text[x]) == pair) {
351 /* Set the old cell to zero to ensure it will be
352 updated on the next doupdate() */
353 SetChar(ptr->text[x], 0, 0);
354 CHANGED_CELL(ptr, x);
355 changed = TRUE;
356 }
357 }
358 if (changed)
359 _nc_make_oldhash(y);
360 }
361 }
362 SP->_color_pairs[pair] = result;
363 if (GET_SCREEN_PAIR(SP) == pair)
364 SET_SCREEN_PAIR(SP, ~0); /* force attribute update */
365
366 if (initialize_pair) {
367 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
368
369 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
370 pair,
371 tp[f].red, tp[f].green, tp[f].blue,
372 tp[b].red, tp[b].green, tp[b].blue));
373
374 if (initialize_pair) {
375 TPUTS_TRACE("initialize_pair");
376 putp(tparm(initialize_pair,
377 pair,
378 tp[f].red, tp[f].green, tp[f].blue,
379 tp[b].red, tp[b].green, tp[b].blue));
380 }
381 }
382
383 returnCode(OK);
384 }
385
386 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
387
388 NCURSES_EXPORT(int)
init_color(short color,short r,short g,short b)389 init_color(short color, short r, short g, short b)
390 {
391 int result = ERR;
392
393 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
394
395 if (initialize_color != NULL
396 && SP != 0
397 && SP->_coloron
398 && (color >= 0 && color < COLORS)
399 && (okRGB(r) && okRGB(g) && okRGB(b))) {
400
401 SP->_color_table[color].init = 1;
402 SP->_color_table[color].r = r;
403 SP->_color_table[color].g = g;
404 SP->_color_table[color].b = b;
405
406 if (hue_lightness_saturation) {
407 rgb2hls(r, g, b,
408 &SP->_color_table[color].red,
409 &SP->_color_table[color].green,
410 &SP->_color_table[color].blue);
411 } else {
412 SP->_color_table[color].red = r;
413 SP->_color_table[color].green = g;
414 SP->_color_table[color].blue = b;
415 }
416
417 TPUTS_TRACE("initialize_color");
418 putp(tparm(initialize_color, color, r, g, b));
419 SP->_color_defs = max(color + 1, SP->_color_defs);
420 result = OK;
421 }
422 returnCode(result);
423 }
424
425 NCURSES_EXPORT(bool)
can_change_color(void)426 can_change_color(void)
427 {
428 T((T_CALLED("can_change_color()")));
429 returnCode((can_change != 0) ? TRUE : FALSE);
430 }
431
432 NCURSES_EXPORT(bool)
has_colors(void)433 has_colors(void)
434 {
435 T((T_CALLED("has_colors()")));
436 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
437 && (((set_foreground != NULL)
438 && (set_background != NULL))
439 || ((set_a_foreground != NULL)
440 && (set_a_background != NULL))
441 || set_color_pair)) ? TRUE : FALSE);
442 }
443
444 NCURSES_EXPORT(int)
color_content(short color,short * r,short * g,short * b)445 color_content(short color, short *r, short *g, short *b)
446 {
447 int result;
448
449 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
450 if (color < 0 || color >= COLORS || SP == 0 || !SP->_coloron) {
451 result = ERR;
452 } else {
453 NCURSES_COLOR_T c_r = SP->_color_table[color].red;
454 NCURSES_COLOR_T c_g = SP->_color_table[color].green;
455 NCURSES_COLOR_T c_b = SP->_color_table[color].blue;
456
457 if (r)
458 *r = c_r;
459 if (g)
460 *g = c_g;
461 if (b)
462 *b = c_b;
463
464 T(("...color_content(%d,%d,%d,%d)", color, c_r, c_g, c_b));
465 result = OK;
466 }
467 returnCode(result);
468 }
469
470 NCURSES_EXPORT(int)
pair_content(short pair,short * f,short * b)471 pair_content(short pair, short *f, short *b)
472 {
473 int result;
474
475 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
476
477 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
478 result = ERR;
479 } else {
480 NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
481 NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
482
483 #if NCURSES_EXT_FUNCS
484 if (fg == COLOR_DEFAULT)
485 fg = -1;
486 if (bg == COLOR_DEFAULT)
487 bg = -1;
488 #endif
489
490 if (f)
491 *f = fg;
492 if (b)
493 *b = bg;
494
495 T(("...pair_content(%d,%d,%d)", pair, fg, bg));
496 result = OK;
497 }
498 returnCode(result);
499 }
500
501 NCURSES_EXPORT(void)
_nc_do_color(int old_pair,int pair,bool reverse,int (* outc)(int))502 _nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int))
503 {
504 NCURSES_COLOR_T fg = COLOR_DEFAULT;
505 NCURSES_COLOR_T bg = COLOR_DEFAULT;
506 NCURSES_COLOR_T old_fg, old_bg;
507
508 if (pair < 0 || pair >= COLOR_PAIRS) {
509 return;
510 } else if (pair != 0) {
511 if (set_color_pair) {
512 TPUTS_TRACE("set_color_pair");
513 tputs(tparm(set_color_pair, pair), 1, outc);
514 return;
515 } else if (SP != 0) {
516 pair_content(pair, &fg, &bg);
517 }
518 }
519
520 if (old_pair >= 0
521 && SP != 0
522 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
523 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
524 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
525 #if NCURSES_EXT_FUNCS
526 /*
527 * A minor optimization - but extension. If "AX" is specified in
528 * the terminal description, treat it as screen's indicator of ECMA
529 * SGR 39 and SGR 49, and assume the two sequences are independent.
530 */
531 if (SP->_has_sgr_39_49
532 && isDefaultColor(old_bg)
533 && !isDefaultColor(old_fg)) {
534 tputs("\033[39m", 1, outc);
535 } else if (SP->_has_sgr_39_49
536 && isDefaultColor(old_fg)
537 && !isDefaultColor(old_bg)) {
538 tputs("\033[49m", 1, outc);
539 } else
540 #endif
541 reset_color_pair();
542 }
543 } else {
544 reset_color_pair();
545 if (old_pair < 0)
546 return;
547 }
548
549 #if NCURSES_EXT_FUNCS
550 if (isDefaultColor(fg))
551 fg = default_fg();
552 if (isDefaultColor(bg))
553 bg = default_bg();
554 #endif
555
556 if (reverse) {
557 NCURSES_COLOR_T xx = fg;
558 fg = bg;
559 bg = xx;
560 }
561
562 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
563 fg, bg));
564
565 if (!isDefaultColor(fg)) {
566 set_foreground_color(fg, outc);
567 }
568 if (!isDefaultColor(bg)) {
569 set_background_color(bg, outc);
570 }
571 }
572