1 /****************************************************************************
2 * Copyright (c) 1998-2007,2009 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
38 * Handles color emulation of SYS V curses
41 #include <curses.priv.h>
46 MODULE_ID("$Id: lib_color.c,v 1.88 2009/02/15 00:33:02 tom Exp $")
49 * These should be screen structure members. They need to be globals for
50 * historical reasons. So we assign them in start_color() and also in
51 * set_term()'s screen-switching logic.
55 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
57 return SP ? SP->_pair_count : -1;
60 NCURSES_PUBLIC_VAR(COLORS) (void)
62 return SP ? SP->_color_count : -1;
65 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
66 NCURSES_EXPORT_VAR(int) COLORS = 0;
69 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
71 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
75 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
76 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
79 * Given a RGB range of 0..1000, we'll normally set the individual values
80 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
85 static const color_t cga_palette[] =
88 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
89 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
90 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
91 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
92 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
93 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
94 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
95 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
98 static const color_t hls_palette[] =
101 DATA( 0, 0, 0), /* COLOR_BLACK */
102 DATA( 120, 50, 100), /* COLOR_RED */
103 DATA( 240, 50, 100), /* COLOR_GREEN */
104 DATA( 180, 50, 100), /* COLOR_YELLOW */
105 DATA( 330, 50, 100), /* COLOR_BLUE */
106 DATA( 60, 50, 100), /* COLOR_MAGENTA */
107 DATA( 300, 50, 100), /* COLOR_CYAN */
108 DATA( 0, 50, 100), /* COLOR_WHITE */
113 * Ensure that we use color pairs only when colors have been started, and also
114 * that the index is within the limits of the table which we allocated.
116 #define ValidPair(pair) \
117 ((SP != 0) && (pair >= 0) && (pair < SP->_pair_limit) && SP->_coloron)
119 #if NCURSES_EXT_FUNCS
121 * These are called from _nc_do_color(), which in turn is called from
122 * vidattr - so we have to assume that SP may be null.
127 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
133 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
136 #define default_fg() COLOR_WHITE
137 #define default_bg() COLOR_BLACK
141 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
142 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
143 * also used in the FreeBSD syscons.
146 toggled_colors(int c)
149 static const int table[] =
150 {0, 4, 2, 6, 1, 5, 3, 7,
151 8, 12, 10, 14, 9, 13, 11, 15};
158 set_background_color(int bg, int (*outc) (int))
160 if (set_a_background) {
161 TPUTS_TRACE("set_a_background");
162 tputs(TPARM_1(set_a_background, bg), 1, outc);
164 TPUTS_TRACE("set_background");
165 tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
170 set_foreground_color(int fg, int (*outc) (int))
172 if (set_a_foreground) {
173 TPUTS_TRACE("set_a_foreground");
174 tputs(TPARM_1(set_a_foreground, fg), 1, outc);
176 TPUTS_TRACE("set_foreground");
177 tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
182 init_color_table(void)
187 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
188 for (n = 0; n < COLORS; n++) {
190 SP->_color_table[n] = tp[n];
192 SP->_color_table[n] = tp[n % MAX_PALETTE];
193 if (hue_lightness_saturation) {
194 SP->_color_table[n].green = 100;
196 if (SP->_color_table[n].red)
197 SP->_color_table[n].red = 1000;
198 if (SP->_color_table[n].green)
199 SP->_color_table[n].green = 1000;
200 if (SP->_color_table[n].blue)
201 SP->_color_table[n].blue = 1000;
208 * Reset the color pair, e.g., to whatever color pair 0 is.
211 reset_color_pair(void)
215 if (orig_pair != 0) {
216 TPUTS_TRACE("orig_pair");
224 * Reset color pairs and definitions. Actually we do both more to accommodate
225 * badly-written terminal descriptions than for the relatively rare case where
226 * someone has changed the color definitions.
229 _nc_reset_colors(void)
233 T((T_CALLED("_nc_reset_colors()")));
234 if (SP->_color_defs > 0)
235 SP->_color_defs = -(SP->_color_defs);
237 if (reset_color_pair())
239 if (orig_colors != 0) {
240 TPUTS_TRACE("orig_colors");
248 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
252 T((T_CALLED("start_color()")));
256 } else if (SP_PARM->_coloron) {
260 if (reset_color_pair() != TRUE) {
261 set_foreground_color(default_fg(), _nc_outch);
262 set_background_color(default_bg(), _nc_outch);
265 if (max_pairs > 0 && max_colors > 0) {
266 SP_PARM->_pair_limit = max_pairs;
268 #if NCURSES_EXT_FUNCS
270 * If using default colors, allocate extra space in table to
271 * allow for default-color as a component of a color-pair.
273 SP_PARM->_pair_limit += (1 + (2 * max_colors));
275 SP_PARM->_pair_count = max_pairs;
276 SP_PARM->_color_count = max_colors;
278 COLOR_PAIRS = max_pairs;
282 SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t,
283 SP_PARM->_pair_limit);
284 if (SP_PARM->_color_pairs != 0) {
285 SP_PARM->_color_table = TYPE_CALLOC(color_t, max_colors);
286 if (SP_PARM->_color_table != 0) {
287 SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(),
291 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
292 COLORS, COLOR_PAIRS));
294 SP_PARM->_coloron = 1;
296 } else if (SP_PARM->_color_pairs != 0) {
297 FreeAndNull(SP_PARM->_color_pairs);
311 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
315 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
317 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
318 /* convert RGB to HLS system */
322 if ((min = g < r ? g : r) > b)
324 if ((max = g > r ? g : r) < b)
327 /* calculate lightness */
328 *l = (min + max) / 20;
330 if (min == max) { /* black, white and all shades of gray */
336 /* calculate saturation */
338 *s = ((max - min) * 100) / (max + min);
340 *s = ((max - min) * 100) / (2000 - max - min);
344 t = 120 + ((g - b) * 60) / (max - min);
346 t = 240 + ((b - r) * 60) / (max - min);
348 t = 360 + ((r - g) * 60) / (max - min);
354 * Extension (1997/1/18) - Allow negative f/b values to set default color
358 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
361 colorpair_t previous;
363 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
365 if (!ValidPair(pair))
368 previous = SP_PARM->_color_pairs[pair];
369 #if NCURSES_EXT_FUNCS
370 if (SP_PARM->_default_color) {
371 bool isDefault = FALSE;
372 bool wasDefault = FALSE;
373 int default_pairs = SP_PARM->_default_pairs;
376 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
377 * internal unsigned values which we will store in the _color_pairs[]
380 if (isDefaultColor(f)) {
383 } else if (!OkColorHi(f)) {
387 if (isDefaultColor(b)) {
390 } else if (!OkColorHi(b)) {
395 * Check if the table entry that we are going to init/update used
398 if ((FORE_OF(previous) == COLOR_DEFAULT)
399 || (BACK_OF(previous) == COLOR_DEFAULT))
403 * Keep track of the number of entries in the color pair table which
404 * used a default color.
406 if (isDefault && !wasDefault) {
408 } else if (wasDefault && !isDefault) {
413 * As an extension, ncurses allows the pair number to exceed the
414 * terminal's color_pairs value for pairs using a default color.
416 * Note that updating a pair which used a default color with one
417 * that does not will decrement the count - and possibly interfere
418 * with sequentially adding new pairs.
420 if (pair > (SP_PARM->_pair_count + default_pairs)) {
423 SP_PARM->_default_pairs = default_pairs;
427 if ((f < 0) || !OkColorHi(f)
428 || (b < 0) || !OkColorHi(b)
434 * When a pair's content is changed, replace its colors (if pair was
435 * initialized before a screen update is performed replacing original
436 * pair colors with the new ones).
438 result = PAIR_OF(f, b);
440 && previous != result) {
443 for (y = 0; y <= curscr->_maxy; y++) {
444 struct ldat *ptr = &(curscr->_line[y]);
445 bool changed = FALSE;
446 for (x = 0; x <= curscr->_maxx; x++) {
447 if (GetPair(ptr->text[x]) == pair) {
448 /* Set the old cell to zero to ensure it will be
449 updated on the next doupdate() */
450 SetChar(ptr->text[x], 0, 0);
451 CHANGED_CELL(ptr, x);
459 SP_PARM->_color_pairs[pair] = result;
460 if (GET_SCREEN_PAIR(SP_PARM) == pair)
461 SET_SCREEN_PAIR(SP_PARM, (chtype) (~0)); /* force attribute update */
463 if (initialize_pair && InPalette(f) && InPalette(b)) {
464 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
467 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
469 tp[f].red, tp[f].green, tp[f].blue,
470 tp[b].red, tp[b].green, tp[b].blue));
472 TPUTS_TRACE("initialize_pair");
473 putp(TPARM_7(initialize_pair,
475 tp[f].red, tp[f].green, tp[f].blue,
476 tp[b].red, tp[b].green, tp[b].blue));
484 init_pair(short pair, short f, short b)
486 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
490 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
493 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
494 short color, short r, short g, short b)
498 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
500 if (initialize_color != NULL
503 && (color >= 0 && OkColorHi(color))
504 && (okRGB(r) && okRGB(g) && okRGB(b))) {
506 SP_PARM->_color_table[color].init = 1;
507 SP_PARM->_color_table[color].r = r;
508 SP_PARM->_color_table[color].g = g;
509 SP_PARM->_color_table[color].b = b;
511 if (hue_lightness_saturation) {
513 &SP_PARM->_color_table[color].red,
514 &SP_PARM->_color_table[color].green,
515 &SP_PARM->_color_table[color].blue);
517 SP_PARM->_color_table[color].red = r;
518 SP_PARM->_color_table[color].green = g;
519 SP_PARM->_color_table[color].blue = b;
522 TPUTS_TRACE("initialize_color");
523 putp(TPARM_4(initialize_color, color, r, g, b));
524 SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
532 init_color(short color, short r, short g, short b)
534 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
539 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
541 T((T_CALLED("can_change_color()")));
542 returnCode((can_change != 0) ? TRUE : FALSE);
547 can_change_color(void)
549 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
554 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
556 T((T_CALLED("has_colors()")));
557 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
558 && (((set_foreground != NULL)
559 && (set_background != NULL))
560 || ((set_a_foreground != NULL)
561 && (set_a_background != NULL))
562 || set_color_pair)) ? TRUE : FALSE);
569 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
574 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
579 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
580 if (color < 0 || !OkColorHi(color) || SP_PARM == 0 || !SP_PARM->_coloron) {
583 NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
584 NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
585 NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
594 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
595 color, c_r, c_g, c_b));
603 color_content(short color, short *r, short *g, short *b)
605 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
610 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
615 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
617 if (!ValidPair(pair)) {
620 NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
621 NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
623 #if NCURSES_EXT_FUNCS
624 if (fg == COLOR_DEFAULT)
626 if (bg == COLOR_DEFAULT)
635 TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
643 pair_content(short pair, short *f, short *b)
645 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
650 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
652 NCURSES_COLOR_T fg = COLOR_DEFAULT;
653 NCURSES_COLOR_T bg = COLOR_DEFAULT;
654 NCURSES_COLOR_T old_fg, old_bg;
656 if (!ValidPair(pair)) {
658 } else if (pair != 0) {
659 if (set_color_pair) {
660 TPUTS_TRACE("set_color_pair");
661 tputs(TPARM_1(set_color_pair, pair), 1, outc);
663 } else if (SP != 0) {
664 pair_content((short) pair, &fg, &bg);
670 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
671 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
672 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
673 #if NCURSES_EXT_FUNCS
675 * A minor optimization - but extension. If "AX" is specified in
676 * the terminal description, treat it as screen's indicator of ECMA
677 * SGR 39 and SGR 49, and assume the two sequences are independent.
679 if (SP->_has_sgr_39_49
680 && isDefaultColor(old_bg)
681 && !isDefaultColor(old_fg)) {
682 tputs("\033[39m", 1, outc);
683 } else if (SP->_has_sgr_39_49
684 && isDefaultColor(old_fg)
685 && !isDefaultColor(old_bg)) {
686 tputs("\033[49m", 1, outc);
697 #if NCURSES_EXT_FUNCS
698 if (isDefaultColor(fg))
700 if (isDefaultColor(bg))
705 NCURSES_COLOR_T xx = fg;
710 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
713 if (!isDefaultColor(fg)) {
714 set_foreground_color(fg, outc);
716 if (!isDefaultColor(bg)) {
717 set_background_color(bg, outc);