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.84 2007/03/10 19:20:15 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))
72 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
75 * Given a RGB range of 0..1000, we'll normally set the individual values
76 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
81 static const color_t cga_palette[] =
84 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
85 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
86 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
87 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
88 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
89 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
90 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
91 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
94 static const color_t hls_palette[] =
97 DATA( 0, 0, 0), /* COLOR_BLACK */
98 DATA( 120, 50, 100), /* COLOR_RED */
99 DATA( 240, 50, 100), /* COLOR_GREEN */
100 DATA( 180, 50, 100), /* COLOR_YELLOW */
101 DATA( 330, 50, 100), /* COLOR_BLUE */
102 DATA( 60, 50, 100), /* COLOR_MAGENTA */
103 DATA( 300, 50, 100), /* COLOR_CYAN */
104 DATA( 0, 50, 100), /* COLOR_WHITE */
108 #if NCURSES_EXT_FUNCS
110 * These are called from _nc_do_color(), which in turn is called from
111 * vidattr - so we have to assume that SP may be null.
116 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
122 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
125 #define default_fg() COLOR_WHITE
126 #define default_bg() COLOR_BLACK
130 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
131 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
132 * also used in the FreeBSD syscons.
135 toggled_colors(int c)
138 static const int table[] =
139 {0, 4, 2, 6, 1, 5, 3, 7,
140 8, 12, 10, 14, 9, 13, 11, 15};
147 set_background_color(int bg, int (*outc) (int))
149 if (set_a_background) {
150 TPUTS_TRACE("set_a_background");
151 tputs(TPARM_1(set_a_background, bg), 1, outc);
153 TPUTS_TRACE("set_background");
154 tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
159 set_foreground_color(int fg, int (*outc) (int))
161 if (set_a_foreground) {
162 TPUTS_TRACE("set_a_foreground");
163 tputs(TPARM_1(set_a_foreground, fg), 1, outc);
165 TPUTS_TRACE("set_foreground");
166 tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
171 init_color_table(void)
176 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
177 for (n = 0; n < COLORS; n++) {
179 SP->_color_table[n] = tp[n];
181 SP->_color_table[n] = tp[n % 8];
182 if (hue_lightness_saturation) {
183 SP->_color_table[n].green = 100;
185 if (SP->_color_table[n].red)
186 SP->_color_table[n].red = 1000;
187 if (SP->_color_table[n].green)
188 SP->_color_table[n].green = 1000;
189 if (SP->_color_table[n].blue)
190 SP->_color_table[n].blue = 1000;
197 * Reset the color pair, e.g., to whatever color pair 0 is.
200 reset_color_pair(void)
204 if (orig_pair != 0) {
205 TPUTS_TRACE("orig_pair");
213 * Reset color pairs and definitions. Actually we do both more to accommodate
214 * badly-written terminal descriptions than for the relatively rare case where
215 * someone has changed the color definitions.
218 _nc_reset_colors(void)
222 T((T_CALLED("_nc_reset_colors()")));
223 if (SP->_color_defs > 0)
224 SP->_color_defs = -(SP->_color_defs);
226 if (reset_color_pair())
228 if (orig_colors != 0) {
229 TPUTS_TRACE("orig_colors");
241 T((T_CALLED("start_color()")));
245 } else if (SP->_coloron) {
249 if (reset_color_pair() != TRUE) {
250 set_foreground_color(default_fg(), _nc_outch);
251 set_background_color(default_bg(), _nc_outch);
254 if (max_pairs > 0 && max_colors > 0) {
255 SP->_pair_count = max_pairs;
256 SP->_color_count = max_colors;
258 COLOR_PAIRS = max_pairs;
262 if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
264 if ((SP->_color_table = TYPE_CALLOC(color_t,
266 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
269 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
270 COLORS, COLOR_PAIRS));
274 } else if (SP->_color_pairs != 0) {
275 FreeAndNull(SP->_color_pairs);
285 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
287 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
288 /* convert RGB to HLS system */
292 if ((min = g < r ? g : r) > b)
294 if ((max = g > r ? g : r) < b)
297 /* calculate lightness */
298 *l = (min + max) / 20;
300 if (min == max) { /* black, white and all shades of gray */
306 /* calculate saturation */
308 *s = ((max - min) * 100) / (max + min);
310 *s = ((max - min) * 100) / (2000 - max - min);
314 t = 120 + ((g - b) * 60) / (max - min);
316 t = 240 + ((b - r) * 60) / (max - min);
318 t = 360 + ((r - g) * 60) / (max - min);
324 * Extension (1997/1/18) - Allow negative f/b values to set default color
328 init_pair(short pair, short f, short b)
332 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
334 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
336 #if NCURSES_EXT_FUNCS
337 if (SP->_default_color) {
342 if (!OkColorHi(f) && !isDefaultColor(f))
344 if (!OkColorHi(b) && !isDefaultColor(b))
349 if ((f < 0) || !OkColorHi(f)
350 || (b < 0) || !OkColorHi(b)
356 * When a pair's content is changed, replace its colors (if pair was
357 * initialized before a screen update is performed replacing original
358 * pair colors with the new ones).
360 result = PAIR_OF(f, b);
361 if (SP->_color_pairs[pair] != 0
362 && SP->_color_pairs[pair] != result) {
365 for (y = 0; y <= curscr->_maxy; y++) {
366 struct ldat *ptr = &(curscr->_line[y]);
367 bool changed = FALSE;
368 for (x = 0; x <= curscr->_maxx; x++) {
369 if (GetPair(ptr->text[x]) == pair) {
370 /* Set the old cell to zero to ensure it will be
371 updated on the next doupdate() */
372 SetChar(ptr->text[x], 0, 0);
373 CHANGED_CELL(ptr, x);
381 SP->_color_pairs[pair] = result;
382 if (GET_SCREEN_PAIR(SP) == pair)
383 SET_SCREEN_PAIR(SP, (chtype) (~0)); /* force attribute update */
385 if (initialize_pair) {
386 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
389 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
391 tp[f].red, tp[f].green, tp[f].blue,
392 tp[b].red, tp[b].green, tp[b].blue));
394 TPUTS_TRACE("initialize_pair");
395 putp(TPARM_7(initialize_pair,
397 tp[f].red, tp[f].green, tp[f].blue,
398 tp[b].red, tp[b].green, tp[b].blue));
404 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
407 init_color(short color, short r, short g, short b)
411 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
413 if (initialize_color != NULL
416 && (color >= 0 && OkColorHi(color))
417 && (okRGB(r) && okRGB(g) && okRGB(b))) {
419 SP->_color_table[color].init = 1;
420 SP->_color_table[color].r = r;
421 SP->_color_table[color].g = g;
422 SP->_color_table[color].b = b;
424 if (hue_lightness_saturation) {
426 &SP->_color_table[color].red,
427 &SP->_color_table[color].green,
428 &SP->_color_table[color].blue);
430 SP->_color_table[color].red = r;
431 SP->_color_table[color].green = g;
432 SP->_color_table[color].blue = b;
435 TPUTS_TRACE("initialize_color");
436 putp(TPARM_4(initialize_color, color, r, g, b));
437 SP->_color_defs = max(color + 1, SP->_color_defs);
444 can_change_color(void)
446 T((T_CALLED("can_change_color()")));
447 returnCode((can_change != 0) ? TRUE : FALSE);
453 T((T_CALLED("has_colors()")));
454 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
455 && (((set_foreground != NULL)
456 && (set_background != NULL))
457 || ((set_a_foreground != NULL)
458 && (set_a_background != NULL))
459 || set_color_pair)) ? TRUE : FALSE);
463 color_content(short color, short *r, short *g, short *b)
467 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
468 if (color < 0 || !OkColorHi(color) || SP == 0 || !SP->_coloron) {
471 NCURSES_COLOR_T c_r = SP->_color_table[color].red;
472 NCURSES_COLOR_T c_g = SP->_color_table[color].green;
473 NCURSES_COLOR_T c_b = SP->_color_table[color].blue;
482 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
483 color, c_r, c_g, c_b));
490 pair_content(short pair, short *f, short *b)
494 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
496 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
499 NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
500 NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
502 #if NCURSES_EXT_FUNCS
503 if (fg == COLOR_DEFAULT)
505 if (bg == COLOR_DEFAULT)
514 TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
521 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
523 NCURSES_COLOR_T fg = COLOR_DEFAULT;
524 NCURSES_COLOR_T bg = COLOR_DEFAULT;
525 NCURSES_COLOR_T old_fg, old_bg;
527 if (pair < 0 || pair >= COLOR_PAIRS) {
529 } else if (pair != 0) {
530 if (set_color_pair) {
531 TPUTS_TRACE("set_color_pair");
532 tputs(TPARM_1(set_color_pair, pair), 1, outc);
534 } else if (SP != 0) {
535 pair_content((short) pair, &fg, &bg);
541 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
542 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
543 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
544 #if NCURSES_EXT_FUNCS
546 * A minor optimization - but extension. If "AX" is specified in
547 * the terminal description, treat it as screen's indicator of ECMA
548 * SGR 39 and SGR 49, and assume the two sequences are independent.
550 if (SP->_has_sgr_39_49
551 && isDefaultColor(old_bg)
552 && !isDefaultColor(old_fg)) {
553 tputs("\033[39m", 1, outc);
554 } else if (SP->_has_sgr_39_49
555 && isDefaultColor(old_fg)
556 && !isDefaultColor(old_bg)) {
557 tputs("\033[49m", 1, outc);
568 #if NCURSES_EXT_FUNCS
569 if (isDefaultColor(fg))
571 if (isDefaultColor(bg))
576 NCURSES_COLOR_T xx = fg;
581 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
584 if (!isDefaultColor(fg)) {
585 set_foreground_color(fg, outc);
587 if (!isDefaultColor(bg)) {
588 set_background_color(bg, outc);