1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.85 2007/04/07 17:07:28 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.
54 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
56 return SP ? SP->_pair_count : -1;
59 NCURSES_PUBLIC_VAR(COLORS) (void)
61 return SP ? SP->_color_count : -1;
64 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
65 NCURSES_EXPORT_VAR(int) COLORS = 0;
68 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
70 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
74 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
75 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
78 * Given a RGB range of 0..1000, we'll normally set the individual values
79 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
84 static const color_t cga_palette[] =
87 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
88 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
89 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
90 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
91 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
92 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
93 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
94 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
97 static const color_t hls_palette[] =
100 DATA( 0, 0, 0), /* COLOR_BLACK */
101 DATA( 120, 50, 100), /* COLOR_RED */
102 DATA( 240, 50, 100), /* COLOR_GREEN */
103 DATA( 180, 50, 100), /* COLOR_YELLOW */
104 DATA( 330, 50, 100), /* COLOR_BLUE */
105 DATA( 60, 50, 100), /* COLOR_MAGENTA */
106 DATA( 300, 50, 100), /* COLOR_CYAN */
107 DATA( 0, 50, 100), /* COLOR_WHITE */
111 #if NCURSES_EXT_FUNCS
113 * These are called from _nc_do_color(), which in turn is called from
114 * vidattr - so we have to assume that SP may be null.
119 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
125 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
128 #define default_fg() COLOR_WHITE
129 #define default_bg() COLOR_BLACK
133 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
134 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
135 * also used in the FreeBSD syscons.
138 toggled_colors(int c)
141 static const int table[] =
142 {0, 4, 2, 6, 1, 5, 3, 7,
143 8, 12, 10, 14, 9, 13, 11, 15};
150 set_background_color(int bg, int (*outc) (int))
152 if (set_a_background) {
153 TPUTS_TRACE("set_a_background");
154 tputs(TPARM_1(set_a_background, bg), 1, outc);
156 TPUTS_TRACE("set_background");
157 tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
162 set_foreground_color(int fg, int (*outc) (int))
164 if (set_a_foreground) {
165 TPUTS_TRACE("set_a_foreground");
166 tputs(TPARM_1(set_a_foreground, fg), 1, outc);
168 TPUTS_TRACE("set_foreground");
169 tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
174 init_color_table(void)
179 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
180 for (n = 0; n < COLORS; n++) {
182 SP->_color_table[n] = tp[n];
184 SP->_color_table[n] = tp[n % MAX_PALETTE];
185 if (hue_lightness_saturation) {
186 SP->_color_table[n].green = 100;
188 if (SP->_color_table[n].red)
189 SP->_color_table[n].red = 1000;
190 if (SP->_color_table[n].green)
191 SP->_color_table[n].green = 1000;
192 if (SP->_color_table[n].blue)
193 SP->_color_table[n].blue = 1000;
200 * Reset the color pair, e.g., to whatever color pair 0 is.
203 reset_color_pair(void)
207 if (orig_pair != 0) {
208 TPUTS_TRACE("orig_pair");
216 * Reset color pairs and definitions. Actually we do both more to accommodate
217 * badly-written terminal descriptions than for the relatively rare case where
218 * someone has changed the color definitions.
221 _nc_reset_colors(void)
225 T((T_CALLED("_nc_reset_colors()")));
226 if (SP->_color_defs > 0)
227 SP->_color_defs = -(SP->_color_defs);
229 if (reset_color_pair())
231 if (orig_colors != 0) {
232 TPUTS_TRACE("orig_colors");
244 T((T_CALLED("start_color()")));
248 } else if (SP->_coloron) {
252 if (reset_color_pair() != TRUE) {
253 set_foreground_color(default_fg(), _nc_outch);
254 set_background_color(default_bg(), _nc_outch);
257 if (max_pairs > 0 && max_colors > 0) {
258 SP->_pair_count = max_pairs;
259 SP->_color_count = max_colors;
261 COLOR_PAIRS = max_pairs;
265 if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
267 if ((SP->_color_table = TYPE_CALLOC(color_t,
269 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
272 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
273 COLORS, COLOR_PAIRS));
277 } else if (SP->_color_pairs != 0) {
278 FreeAndNull(SP->_color_pairs);
288 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
290 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
291 /* convert RGB to HLS system */
295 if ((min = g < r ? g : r) > b)
297 if ((max = g > r ? g : r) < b)
300 /* calculate lightness */
301 *l = (min + max) / 20;
303 if (min == max) { /* black, white and all shades of gray */
309 /* calculate saturation */
311 *s = ((max - min) * 100) / (max + min);
313 *s = ((max - min) * 100) / (2000 - max - min);
317 t = 120 + ((g - b) * 60) / (max - min);
319 t = 240 + ((b - r) * 60) / (max - min);
321 t = 360 + ((r - g) * 60) / (max - min);
327 * Extension (1997/1/18) - Allow negative f/b values to set default color
331 init_pair(short pair, short f, short b)
335 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
337 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
339 #if NCURSES_EXT_FUNCS
340 if (SP->_default_color) {
345 if (!OkColorHi(f) && !isDefaultColor(f))
347 if (!OkColorHi(b) && !isDefaultColor(b))
352 if ((f < 0) || !OkColorHi(f)
353 || (b < 0) || !OkColorHi(b)
359 * When a pair's content is changed, replace its colors (if pair was
360 * initialized before a screen update is performed replacing original
361 * pair colors with the new ones).
363 result = PAIR_OF(f, b);
364 if (SP->_color_pairs[pair] != 0
365 && SP->_color_pairs[pair] != result) {
368 for (y = 0; y <= curscr->_maxy; y++) {
369 struct ldat *ptr = &(curscr->_line[y]);
370 bool changed = FALSE;
371 for (x = 0; x <= curscr->_maxx; x++) {
372 if (GetPair(ptr->text[x]) == pair) {
373 /* Set the old cell to zero to ensure it will be
374 updated on the next doupdate() */
375 SetChar(ptr->text[x], 0, 0);
376 CHANGED_CELL(ptr, x);
384 SP->_color_pairs[pair] = result;
385 if (GET_SCREEN_PAIR(SP) == pair)
386 SET_SCREEN_PAIR(SP, (chtype) (~0)); /* force attribute update */
388 if (initialize_pair && InPalette(f) && InPalette(b)) {
389 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
392 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
394 tp[f].red, tp[f].green, tp[f].blue,
395 tp[b].red, tp[b].green, tp[b].blue));
397 TPUTS_TRACE("initialize_pair");
398 putp(TPARM_7(initialize_pair,
400 tp[f].red, tp[f].green, tp[f].blue,
401 tp[b].red, tp[b].green, tp[b].blue));
407 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
410 init_color(short color, short r, short g, short b)
414 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
416 if (initialize_color != NULL
419 && (color >= 0 && OkColorHi(color))
420 && (okRGB(r) && okRGB(g) && okRGB(b))) {
422 SP->_color_table[color].init = 1;
423 SP->_color_table[color].r = r;
424 SP->_color_table[color].g = g;
425 SP->_color_table[color].b = b;
427 if (hue_lightness_saturation) {
429 &SP->_color_table[color].red,
430 &SP->_color_table[color].green,
431 &SP->_color_table[color].blue);
433 SP->_color_table[color].red = r;
434 SP->_color_table[color].green = g;
435 SP->_color_table[color].blue = b;
438 TPUTS_TRACE("initialize_color");
439 putp(TPARM_4(initialize_color, color, r, g, b));
440 SP->_color_defs = max(color + 1, SP->_color_defs);
447 can_change_color(void)
449 T((T_CALLED("can_change_color()")));
450 returnCode((can_change != 0) ? TRUE : FALSE);
456 T((T_CALLED("has_colors()")));
457 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
458 && (((set_foreground != NULL)
459 && (set_background != NULL))
460 || ((set_a_foreground != NULL)
461 && (set_a_background != NULL))
462 || set_color_pair)) ? TRUE : FALSE);
466 color_content(short color, short *r, short *g, short *b)
470 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
471 if (color < 0 || !OkColorHi(color) || SP == 0 || !SP->_coloron) {
474 NCURSES_COLOR_T c_r = SP->_color_table[color].red;
475 NCURSES_COLOR_T c_g = SP->_color_table[color].green;
476 NCURSES_COLOR_T c_b = SP->_color_table[color].blue;
485 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
486 color, c_r, c_g, c_b));
493 pair_content(short pair, short *f, short *b)
497 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
499 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
502 NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
503 NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
505 #if NCURSES_EXT_FUNCS
506 if (fg == COLOR_DEFAULT)
508 if (bg == COLOR_DEFAULT)
517 TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
524 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
526 NCURSES_COLOR_T fg = COLOR_DEFAULT;
527 NCURSES_COLOR_T bg = COLOR_DEFAULT;
528 NCURSES_COLOR_T old_fg, old_bg;
530 if (pair < 0 || pair >= COLOR_PAIRS) {
532 } else if (pair != 0) {
533 if (set_color_pair) {
534 TPUTS_TRACE("set_color_pair");
535 tputs(TPARM_1(set_color_pair, pair), 1, outc);
537 } else if (SP != 0) {
538 pair_content((short) pair, &fg, &bg);
544 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
545 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
546 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
547 #if NCURSES_EXT_FUNCS
549 * A minor optimization - but extension. If "AX" is specified in
550 * the terminal description, treat it as screen's indicator of ECMA
551 * SGR 39 and SGR 49, and assume the two sequences are independent.
553 if (SP->_has_sgr_39_49
554 && isDefaultColor(old_bg)
555 && !isDefaultColor(old_fg)) {
556 tputs("\033[39m", 1, outc);
557 } else if (SP->_has_sgr_39_49
558 && isDefaultColor(old_fg)
559 && !isDefaultColor(old_bg)) {
560 tputs("\033[49m", 1, outc);
571 #if NCURSES_EXT_FUNCS
572 if (isDefaultColor(fg))
574 if (isDefaultColor(bg))
579 NCURSES_COLOR_T xx = fg;
584 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
587 if (!isDefaultColor(fg)) {
588 set_foreground_color(fg, outc);
590 if (!isDefaultColor(bg)) {
591 set_background_color(bg, outc);