1 /****************************************************************************
2 * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
37 * Handles color emulation of SYS V curses
40 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_color.c,v 1.75 2005/06/18 20:19:08 tom Exp $")
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.
52 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
53 NCURSES_EXPORT_VAR(int) COLORS = 0;
55 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
57 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
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.
66 static const color_t cga_palette[] =
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 */
79 static const color_t hls_palette[] =
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 */
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.
101 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
107 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
110 #define default_fg() COLOR_WHITE
111 #define default_bg() COLOR_BLACK
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.
120 toggled_colors(int c)
123 static const int table[] =
124 {0, 4, 2, 6, 1, 5, 3, 7,
125 8, 12, 10, 14, 9, 13, 11, 15};
132 set_background_color(int bg, int (*outc) (int))
134 if (set_a_background) {
135 TPUTS_TRACE("set_a_background");
136 tputs(tparm(set_a_background, bg), 1, outc);
138 TPUTS_TRACE("set_background");
139 tputs(tparm(set_background, toggled_colors(bg)), 1, outc);
144 set_foreground_color(int fg, int (*outc) (int))
146 if (set_a_foreground) {
147 TPUTS_TRACE("set_a_foreground");
148 tputs(tparm(set_a_foreground, fg), 1, outc);
150 TPUTS_TRACE("set_foreground");
151 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
156 init_color_table(void)
161 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
162 for (n = 0; n < COLORS; n++) {
164 SP->_color_table[n] = tp[n];
166 SP->_color_table[n] = tp[n % 8];
167 if (hue_lightness_saturation) {
168 SP->_color_table[n].green = 100;
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;
182 * Reset the color pair, e.g., to whatever color pair 0 is.
185 reset_color_pair(void)
189 if (orig_pair != 0) {
190 TPUTS_TRACE("orig_pair");
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.
203 _nc_reset_colors(void)
207 T((T_CALLED("_nc_reset_colors()")));
208 if (SP->_color_defs > 0)
209 SP->_color_defs = -(SP->_color_defs);
211 if (reset_color_pair())
213 if (orig_colors != 0) {
214 TPUTS_TRACE("orig_colors");
226 T((T_CALLED("start_color()")));
230 } else if (SP->_coloron) {
234 if (reset_color_pair() != TRUE) {
235 set_foreground_color(default_fg(), _nc_outch);
236 set_background_color(default_bg(), _nc_outch);
239 if (max_pairs > 0 && max_colors > 0) {
240 COLOR_PAIRS = SP->_pair_count = max_pairs;
241 COLORS = SP->_color_count = max_colors;
243 if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
245 if ((SP->_color_table = TYPE_CALLOC(color_t,
247 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
250 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
251 COLORS, COLOR_PAIRS));
255 } else if (SP->_color_pairs != 0) {
256 FreeAndNull(SP->_color_pairs);
266 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
268 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
269 /* convert RGB to HLS system */
273 if ((min = g < r ? g : r) > b)
275 if ((max = g > r ? g : r) < b)
278 /* calculate lightness */
279 *l = (min + max) / 20;
281 if (min == max) { /* black, white and all shades of gray */
287 /* calculate saturation */
289 *s = ((max - min) * 100) / (max + min);
291 *s = ((max - min) * 100) / (2000 - max - min);
295 t = 120 + ((g - b) * 60) / (max - min);
297 t = 240 + ((b - r) * 60) / (max - min);
299 t = 360 + ((r - g) * 60) / (max - min);
305 * Extension (1997/1/18) - Allow negative f/b values to set default color
309 init_pair(short pair, short f, short b)
313 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
315 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
317 #if NCURSES_EXT_FUNCS
318 if (SP->_default_color) {
323 if (f >= COLORS && !isDefaultColor(f))
325 if (b >= COLORS && !isDefaultColor(b))
330 if ((f < 0) || (f >= COLORS)
331 || (b < 0) || (b >= COLORS)
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).
341 result = PAIR_OF(f, b);
342 if (SP->_color_pairs[pair] != 0
343 && SP->_color_pairs[pair] != result) {
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);
362 SP->_color_pairs[pair] = result;
363 if (GET_SCREEN_PAIR(SP) == pair)
364 SET_SCREEN_PAIR(SP, ~0); /* force attribute update */
366 if (initialize_pair) {
367 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
369 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
371 tp[f].red, tp[f].green, tp[f].blue,
372 tp[b].red, tp[b].green, tp[b].blue));
374 if (initialize_pair) {
375 TPUTS_TRACE("initialize_pair");
376 putp(tparm(initialize_pair,
378 tp[f].red, tp[f].green, tp[f].blue,
379 tp[b].red, tp[b].green, tp[b].blue));
386 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
389 init_color(short color, short r, short g, short b)
393 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
395 if (initialize_color != NULL
398 && (color >= 0 && color < COLORS)
399 && (okRGB(r) && okRGB(g) && okRGB(b))) {
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;
406 if (hue_lightness_saturation) {
408 &SP->_color_table[color].red,
409 &SP->_color_table[color].green,
410 &SP->_color_table[color].blue);
412 SP->_color_table[color].red = r;
413 SP->_color_table[color].green = g;
414 SP->_color_table[color].blue = b;
417 TPUTS_TRACE("initialize_color");
418 putp(tparm(initialize_color, color, r, g, b));
419 SP->_color_defs = max(color + 1, SP->_color_defs);
426 can_change_color(void)
428 T((T_CALLED("can_change_color()")));
429 returnCode((can_change != 0) ? TRUE : FALSE);
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);
445 color_content(short color, short *r, short *g, short *b)
449 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
450 if (color < 0 || color >= COLORS || SP == 0 || !SP->_coloron) {
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;
464 T(("...color_content(%d,%d,%d,%d)", color, c_r, c_g, c_b));
471 pair_content(short pair, short *f, short *b)
475 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
477 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
480 NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
481 NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
483 #if NCURSES_EXT_FUNCS
484 if (fg == COLOR_DEFAULT)
486 if (bg == COLOR_DEFAULT)
495 T(("...pair_content(%d,%d,%d)", pair, fg, bg));
502 _nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int))
504 NCURSES_COLOR_T fg = COLOR_DEFAULT;
505 NCURSES_COLOR_T bg = COLOR_DEFAULT;
506 NCURSES_COLOR_T old_fg, old_bg;
508 if (pair < 0 || pair >= COLOR_PAIRS) {
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);
515 } else if (SP != 0) {
516 pair_content(pair, &fg, &bg);
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
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.
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);
549 #if NCURSES_EXT_FUNCS
550 if (isDefaultColor(fg))
552 if (isDefaultColor(bg))
557 NCURSES_COLOR_T xx = fg;
562 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
565 if (!isDefaultColor(fg)) {
566 set_foreground_color(fg, outc);
568 if (!isDefaultColor(bg)) {
569 set_background_color(bg, outc);