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>
47 #define CUR SP_TERMTYPE
50 MODULE_ID("$Id: lib_color.c,v 1.92 2009/06/06 20:26:16 tom Exp $")
53 * These should be screen structure members. They need to be globals for
54 * historical reasons. So we assign them in start_color() and also in
55 * set_term()'s screen-switching logic.
59 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
61 return SP ? SP->_pair_count : -1;
64 NCURSES_PUBLIC_VAR(COLORS) (void)
66 return SP ? SP->_color_count : -1;
69 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
70 NCURSES_EXPORT_VAR(int) COLORS = 0;
73 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
75 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
79 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
80 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
83 * Given a RGB range of 0..1000, we'll normally set the individual values
84 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
89 static const color_t cga_palette[] =
92 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
93 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
94 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
95 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
96 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
97 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
98 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
99 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
102 static const color_t hls_palette[] =
105 DATA( 0, 0, 0), /* COLOR_BLACK */
106 DATA( 120, 50, 100), /* COLOR_RED */
107 DATA( 240, 50, 100), /* COLOR_GREEN */
108 DATA( 180, 50, 100), /* COLOR_YELLOW */
109 DATA( 330, 50, 100), /* COLOR_BLUE */
110 DATA( 60, 50, 100), /* COLOR_MAGENTA */
111 DATA( 300, 50, 100), /* COLOR_CYAN */
112 DATA( 0, 50, 100), /* COLOR_WHITE */
117 * Ensure that we use color pairs only when colors have been started, and also
118 * that the index is within the limits of the table which we allocated.
120 #define ValidPair(pair) \
121 ((SP != 0) && (pair >= 0) && (pair < SP->_pair_limit) && SP->_coloron)
123 #if NCURSES_EXT_FUNCS
125 * These are called from _nc_do_color(), which in turn is called from
126 * vidattr - so we have to assume that SP may be null.
131 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
137 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
140 #define default_fg() COLOR_WHITE
141 #define default_bg() COLOR_BLACK
145 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
146 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
147 * also used in the FreeBSD syscons.
150 toggled_colors(int c)
153 static const int table[] =
154 {0, 4, 2, 6, 1, 5, 3, 7,
155 8, 12, 10, 14, 9, 13, 11, 15};
162 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
164 if (set_a_background) {
165 TPUTS_TRACE("set_a_background");
166 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
167 TPARM_1(set_a_background, bg),
170 TPUTS_TRACE("set_background");
171 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
172 TPARM_1(set_background, toggled_colors(bg)),
178 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
180 if (set_a_foreground) {
181 TPUTS_TRACE("set_a_foreground");
182 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
183 TPARM_1(set_a_foreground, fg),
186 TPUTS_TRACE("set_foreground");
187 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
188 TPARM_1(set_foreground, toggled_colors(fg)),
194 init_color_table(NCURSES_SP_DCL0)
199 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
200 for (n = 0; n < COLORS; n++) {
202 SP_PARM->_color_table[n] = tp[n];
204 SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
205 if (hue_lightness_saturation) {
206 SP_PARM->_color_table[n].green = 100;
208 if (SP_PARM->_color_table[n].red)
209 SP_PARM->_color_table[n].red = 1000;
210 if (SP_PARM->_color_table[n].green)
211 SP_PARM->_color_table[n].green = 1000;
212 if (SP_PARM->_color_table[n].blue)
213 SP_PARM->_color_table[n].blue = 1000;
220 * Reset the color pair, e.g., to whatever color pair 0 is.
223 reset_color_pair(NCURSES_SP_DCL0)
227 if (orig_pair != 0) {
228 TPUTS_TRACE("orig_pair");
236 * Reset color pairs and definitions. Actually we do both more to accommodate
237 * badly-written terminal descriptions than for the relatively rare case where
238 * someone has changed the color definitions.
241 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0) {
244 T((T_CALLED("_nc_reset_colors()")));
245 if (SP_PARM->_color_defs > 0)
246 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
248 if (reset_color_pair(NCURSES_SP_ARG))
250 if (orig_colors != 0) {
251 TPUTS_TRACE("orig_colors");
260 _nc_reset_colors(void)
262 return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
267 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
271 T((T_CALLED("start_color()")));
275 } else if (SP_PARM->_coloron) {
279 if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
280 set_foreground_color(NCURSES_SP_ARGx
282 NCURSES_SP_NAME(_nc_outch));
283 set_background_color(NCURSES_SP_ARGx
285 NCURSES_SP_NAME(_nc_outch));
288 if (max_pairs > 0 && max_colors > 0) {
289 SP_PARM->_pair_limit = max_pairs;
291 #if NCURSES_EXT_FUNCS
293 * If using default colors, allocate extra space in table to
294 * allow for default-color as a component of a color-pair.
296 SP_PARM->_pair_limit += (1 + (2 * max_colors));
298 SP_PARM->_pair_count = max_pairs;
299 SP_PARM->_color_count = max_colors;
301 COLOR_PAIRS = max_pairs;
305 SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t,
306 SP_PARM->_pair_limit);
307 if (SP_PARM->_color_pairs != 0) {
308 SP_PARM->_color_table = TYPE_CALLOC(color_t, max_colors);
309 if (SP_PARM->_color_table != 0) {
310 SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(),
312 init_color_table(NCURSES_SP_ARG);
314 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
315 COLORS, COLOR_PAIRS));
317 SP_PARM->_coloron = 1;
319 } else if (SP_PARM->_color_pairs != 0) {
320 FreeAndNull(SP_PARM->_color_pairs);
334 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
338 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
340 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
341 /* convert RGB to HLS system */
345 if ((min = g < r ? g : r) > b)
347 if ((max = g > r ? g : r) < b)
350 /* calculate lightness */
351 *l = (min + max) / 20;
353 if (min == max) { /* black, white and all shades of gray */
359 /* calculate saturation */
361 *s = ((max - min) * 100) / (max + min);
363 *s = ((max - min) * 100) / (2000 - max - min);
367 t = 120 + ((g - b) * 60) / (max - min);
369 t = 240 + ((b - r) * 60) / (max - min);
371 t = 360 + ((r - g) * 60) / (max - min);
377 * Extension (1997/1/18) - Allow negative f/b values to set default color
381 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
384 colorpair_t previous;
386 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
388 if (!ValidPair(pair))
391 previous = SP_PARM->_color_pairs[pair];
392 #if NCURSES_EXT_FUNCS
393 if (SP_PARM->_default_color) {
394 bool isDefault = FALSE;
395 bool wasDefault = FALSE;
396 int default_pairs = SP_PARM->_default_pairs;
399 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
400 * internal unsigned values which we will store in the _color_pairs[]
403 if (isDefaultColor(f)) {
406 } else if (!OkColorHi(f)) {
410 if (isDefaultColor(b)) {
413 } else if (!OkColorHi(b)) {
418 * Check if the table entry that we are going to init/update used
421 if ((FORE_OF(previous) == COLOR_DEFAULT)
422 || (BACK_OF(previous) == COLOR_DEFAULT))
426 * Keep track of the number of entries in the color pair table which
427 * used a default color.
429 if (isDefault && !wasDefault) {
431 } else if (wasDefault && !isDefault) {
436 * As an extension, ncurses allows the pair number to exceed the
437 * terminal's color_pairs value for pairs using a default color.
439 * Note that updating a pair which used a default color with one
440 * that does not will decrement the count - and possibly interfere
441 * with sequentially adding new pairs.
443 if (pair > (SP_PARM->_pair_count + default_pairs)) {
446 SP_PARM->_default_pairs = default_pairs;
450 if ((f < 0) || !OkColorHi(f)
451 || (b < 0) || !OkColorHi(b)
457 * When a pair's content is changed, replace its colors (if pair was
458 * initialized before a screen update is performed replacing original
459 * pair colors with the new ones).
461 result = PAIR_OF(f, b);
463 && previous != result) {
466 for (y = 0; y <= curscr->_maxy; y++) {
467 struct ldat *ptr = &(curscr->_line[y]);
468 bool changed = FALSE;
469 for (x = 0; x <= curscr->_maxx; x++) {
470 if (GetPair(ptr->text[x]) == pair) {
471 /* Set the old cell to zero to ensure it will be
472 updated on the next doupdate() */
473 SetChar(ptr->text[x], 0, 0);
474 CHANGED_CELL(ptr, x);
482 SP_PARM->_color_pairs[pair] = result;
483 if (GET_SCREEN_PAIR(SP_PARM) == pair)
484 SET_SCREEN_PAIR(SP_PARM, (chtype) (~0)); /* force attribute update */
486 if (initialize_pair && InPalette(f) && InPalette(b)) {
487 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
490 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
492 tp[f].red, tp[f].green, tp[f].blue,
493 tp[b].red, tp[b].green, tp[b].blue));
495 TPUTS_TRACE("initialize_pair");
496 putp(TPARM_7(initialize_pair,
498 tp[f].red, tp[f].green, tp[f].blue,
499 tp[b].red, tp[b].green, tp[b].blue));
507 init_pair(short pair, short f, short b)
509 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
513 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
516 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
517 short color, short r, short g, short b)
521 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
523 if (initialize_color != NULL
526 && (color >= 0 && OkColorHi(color))
527 && (okRGB(r) && okRGB(g) && okRGB(b))) {
529 SP_PARM->_color_table[color].init = 1;
530 SP_PARM->_color_table[color].r = r;
531 SP_PARM->_color_table[color].g = g;
532 SP_PARM->_color_table[color].b = b;
534 if (hue_lightness_saturation) {
536 &SP_PARM->_color_table[color].red,
537 &SP_PARM->_color_table[color].green,
538 &SP_PARM->_color_table[color].blue);
540 SP_PARM->_color_table[color].red = r;
541 SP_PARM->_color_table[color].green = g;
542 SP_PARM->_color_table[color].blue = b;
545 TPUTS_TRACE("initialize_color");
546 putp(TPARM_4(initialize_color, color, r, g, b));
547 SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
555 init_color(short color, short r, short g, short b)
557 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
562 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
564 T((T_CALLED("can_change_color()")));
565 returnCode((can_change != 0) ? TRUE : FALSE);
570 can_change_color(void)
572 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
577 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
579 T((T_CALLED("has_colors()")));
580 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
581 && (((set_foreground != NULL)
582 && (set_background != NULL))
583 || ((set_a_foreground != NULL)
584 && (set_a_background != NULL))
585 || set_color_pair)) ? TRUE : FALSE);
592 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
597 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
602 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
603 if (color < 0 || !OkColorHi(color) || SP_PARM == 0 || !SP_PARM->_coloron) {
606 NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
607 NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
608 NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
617 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
618 color, c_r, c_g, c_b));
626 color_content(short color, short *r, short *g, short *b)
628 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
633 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
638 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
640 if (!ValidPair(pair)) {
643 NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
644 NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
646 #if NCURSES_EXT_FUNCS
647 if (fg == COLOR_DEFAULT)
649 if (bg == COLOR_DEFAULT)
658 TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
666 pair_content(short pair, short *f, short *b)
668 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
673 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
677 NCURSES_SP_OUTC outc)
679 NCURSES_COLOR_T fg = COLOR_DEFAULT;
680 NCURSES_COLOR_T bg = COLOR_DEFAULT;
681 NCURSES_COLOR_T old_fg, old_bg;
683 if (!ValidPair(pair)) {
685 } else if (pair != 0) {
686 if (set_color_pair) {
687 TPUTS_TRACE("set_color_pair");
688 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
689 TPARM_1(set_color_pair, pair),
692 } else if (SP_PARM != 0) {
693 pair_content((short) pair, &fg, &bg);
699 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
700 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
701 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
702 #if NCURSES_EXT_FUNCS
704 * A minor optimization - but extension. If "AX" is specified in
705 * the terminal description, treat it as screen's indicator of ECMA
706 * SGR 39 and SGR 49, and assume the two sequences are independent.
708 if (SP_PARM->_has_sgr_39_49
709 && isDefaultColor(old_bg)
710 && !isDefaultColor(old_fg)) {
711 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
712 } else if (SP_PARM->_has_sgr_39_49
713 && isDefaultColor(old_fg)
714 && !isDefaultColor(old_bg)) {
715 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
718 reset_color_pair(NCURSES_SP_ARG);
721 reset_color_pair(NCURSES_SP_ARG);
726 #if NCURSES_EXT_FUNCS
727 if (isDefaultColor(fg))
729 if (isDefaultColor(bg))
734 NCURSES_COLOR_T xx = fg;
739 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
742 if (!isDefaultColor(fg)) {
743 set_foreground_color(NCURSES_SP_ARGx fg, outc);
745 if (!isDefaultColor(bg)) {
746 set_background_color(NCURSES_SP_ARGx bg, outc);
752 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
754 SetSafeOutcWrapper(outc);
755 NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,