1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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>
45 #define CUR SP_TERMTYPE
48 MODULE_ID("$Id: lib_color.c,v 1.104 2011/10/22 15:53:42 tom Exp $")
50 #ifdef USE_TERM_DRIVER
51 #define CanChange InfoOf(SP_PARM).canchange
52 #define DefaultPalette InfoOf(SP_PARM).defaultPalette
53 #define HasColor InfoOf(SP_PARM).hascolor
54 #define InitColor InfoOf(SP_PARM).initcolor
55 #define MaxColors InfoOf(SP_PARM).maxcolors
56 #define MaxPairs InfoOf(SP_PARM).maxpairs
57 #define UseHlsPalette (DefaultPalette == _nc_hls_palette)
59 #define CanChange can_change
60 #define DefaultPalette (hue_lightness_saturation ? hls_palette : cga_palette)
61 #define HasColor has_color
62 #define InitColor initialize_color
63 #define MaxColors max_colors
64 #define MaxPairs max_pairs
65 #define UseHlsPalette (hue_lightness_saturation)
68 #ifndef USE_TERM_DRIVER
70 * These should be screen structure members. They need to be globals for
71 * historical reasons. So we assign them in start_color() and also in
72 * set_term()'s screen-switching logic.
76 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
78 return SP ? SP->_pair_count : -1;
81 NCURSES_PUBLIC_VAR(COLORS) (void)
83 return SP ? SP->_color_count : -1;
86 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
87 NCURSES_EXPORT_VAR(int) COLORS = 0;
89 #endif /* !USE_TERM_DRIVER */
91 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
93 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
97 #define OkColorHi(n) (((n) < COLORS) && ((n) < maxcolors))
98 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
101 * Given a RGB range of 0..1000, we'll normally set the individual values
102 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
107 static const color_t cga_palette[] =
110 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
111 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
112 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
113 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
114 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
115 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
116 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
117 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
120 static const color_t hls_palette[] =
123 DATA( 0, 0, 0), /* COLOR_BLACK */
124 DATA( 120, 50, 100), /* COLOR_RED */
125 DATA( 240, 50, 100), /* COLOR_GREEN */
126 DATA( 180, 50, 100), /* COLOR_YELLOW */
127 DATA( 330, 50, 100), /* COLOR_BLUE */
128 DATA( 60, 50, 100), /* COLOR_MAGENTA */
129 DATA( 300, 50, 100), /* COLOR_CYAN */
130 DATA( 0, 50, 100), /* COLOR_WHITE */
133 #ifdef USE_TERM_DRIVER
134 NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette = cga_palette;
135 NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
141 * Ensure that we use color pairs only when colors have been started, and also
142 * that the index is within the limits of the table which we allocated.
144 #define ValidPair(pair) \
145 ((SP_PARM != 0) && (pair >= 0) && (pair < SP_PARM->_pair_limit) && SP_PARM->_coloron)
147 #if NCURSES_EXT_FUNCS
149 * These are called from _nc_do_color(), which in turn is called from
150 * vidattr - so we have to assume that sp may be null.
153 default_fg(NCURSES_SP_DCL0)
155 return (SP_PARM != 0) ? SP_PARM->_default_fg : COLOR_WHITE;
159 default_bg(NCURSES_SP_DCL0)
161 return SP_PARM != 0 ? SP_PARM->_default_bg : COLOR_BLACK;
164 #define default_fg(sp) COLOR_WHITE
165 #define default_bg(sp) COLOR_BLACK
168 #ifndef USE_TERM_DRIVER
170 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
171 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
172 * also used in the FreeBSD syscons.
175 toggled_colors(int c)
178 static const int table[] =
179 {0, 4, 2, 6, 1, 5, 3, 7,
180 8, 12, 10, 14, 9, 13, 11, 15};
188 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
190 #ifdef USE_TERM_DRIVER
191 CallDriver_3(SP_PARM, color, FALSE, bg, outc);
193 if (set_a_background) {
194 TPUTS_TRACE("set_a_background");
195 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
196 TPARM_1(set_a_background, bg),
199 TPUTS_TRACE("set_background");
200 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
201 TPARM_1(set_background, toggled_colors(bg)),
208 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
210 #ifdef USE_TERM_DRIVER
211 CallDriver_3(SP_PARM, color, TRUE, fg, outc);
213 if (set_a_foreground) {
214 TPUTS_TRACE("set_a_foreground");
215 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
216 TPARM_1(set_a_foreground, fg),
219 TPUTS_TRACE("set_foreground");
220 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
221 TPARM_1(set_foreground, toggled_colors(fg)),
228 init_color_table(NCURSES_SP_DCL0)
230 const color_t *tp = DefaultPalette;
235 for (n = 0; n < COLORS; n++) {
237 SP_PARM->_color_table[n] = tp[n];
239 SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
241 SP_PARM->_color_table[n].green = 100;
243 if (SP_PARM->_color_table[n].red)
244 SP_PARM->_color_table[n].red = 1000;
245 if (SP_PARM->_color_table[n].green)
246 SP_PARM->_color_table[n].green = 1000;
247 if (SP_PARM->_color_table[n].blue)
248 SP_PARM->_color_table[n].blue = 1000;
255 * Reset the color pair, e.g., to whatever color pair 0 is.
258 reset_color_pair(NCURSES_SP_DCL0)
260 #ifdef USE_TERM_DRIVER
261 return CallDriver(SP_PARM, rescol);
266 if (orig_pair != 0) {
267 TPUTS_TRACE("orig_pair");
276 * Reset color pairs and definitions. Actually we do both more to accommodate
277 * badly-written terminal descriptions than for the relatively rare case where
278 * someone has changed the color definitions.
281 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
285 T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
286 if (SP_PARM->_color_defs > 0)
287 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
288 if (reset_color_pair(NCURSES_SP_ARG))
291 #ifdef USE_TERM_DRIVER
292 result = CallDriver(SP_PARM, rescolors);
294 if (orig_colors != 0) {
295 TPUTS_TRACE("orig_colors");
305 _nc_reset_colors(void)
307 return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
312 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
315 int maxpairs = 0, maxcolors = 0;
317 T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
321 } else if (SP_PARM->_coloron) {
325 maxcolors = MaxColors;
326 if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
327 set_foreground_color(NCURSES_SP_ARGx
328 default_fg(NCURSES_SP_ARG),
329 NCURSES_SP_NAME(_nc_outch));
330 set_background_color(NCURSES_SP_ARGx
331 default_bg(NCURSES_SP_ARG),
332 NCURSES_SP_NAME(_nc_outch));
334 #if !NCURSES_EXT_COLORS
336 * Without ext-colors, we cannot represent more than 256 color pairs.
342 if (maxpairs > 0 && maxcolors > 0) {
343 SP_PARM->_pair_limit = maxpairs;
345 #if NCURSES_EXT_FUNCS
347 * If using default colors, allocate extra space in table to
348 * allow for default-color as a component of a color-pair.
350 SP_PARM->_pair_limit += (1 + (2 * maxcolors));
352 SP_PARM->_pair_count = maxpairs;
353 SP_PARM->_color_count = maxcolors;
355 COLOR_PAIRS = maxpairs;
359 SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t, SP_PARM->_pair_limit);
360 if (SP_PARM->_color_pairs != 0) {
361 SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
362 if (SP_PARM->_color_table != 0) {
363 SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(NCURSES_SP_ARG),
364 default_bg(NCURSES_SP_ARG));
365 init_color_table(NCURSES_SP_ARG);
367 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
368 COLORS, COLOR_PAIRS));
370 SP_PARM->_coloron = 1;
372 } else if (SP_PARM->_color_pairs != 0) {
373 FreeAndNull(SP_PARM->_color_pairs);
387 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
391 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
393 rgb2hls(int r, int g, int b, short *h, short *l, short *s)
394 /* convert RGB to HLS system */
398 if ((min = g < r ? g : r) > b)
400 if ((max = g > r ? g : r) < b)
403 /* calculate lightness */
404 *l = (short) ((min + max) / 20);
406 if (min == max) { /* black, white and all shades of gray */
412 /* calculate saturation */
414 *s = (short) (((max - min) * 100) / (max + min));
416 *s = (short) (((max - min) * 100) / (2000 - max - min));
420 t = (short) (120 + ((g - b) * 60) / (max - min));
422 t = (short) (240 + ((b - r) * 60) / (max - min));
424 t = (short) (360 + ((r - g) * 60) / (max - min));
426 *h = (short) (t % 360);
430 * Extension (1997/1/18) - Allow negative f/b values to set default color
434 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
437 colorpair_t previous;
440 T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) SP_PARM, pair, f, b));
442 if (!ValidPair(pair))
445 maxcolors = MaxColors;
447 previous = SP_PARM->_color_pairs[pair];
448 #if NCURSES_EXT_FUNCS
449 if (SP_PARM->_default_color || SP_PARM->_assumed_color) {
450 bool isDefault = FALSE;
451 bool wasDefault = FALSE;
452 int default_pairs = SP_PARM->_default_pairs;
455 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
456 * internal unsigned values which we will store in the _color_pairs[]
459 if (isDefaultColor(f)) {
462 } else if (!OkColorHi(f)) {
466 if (isDefaultColor(b)) {
469 } else if (!OkColorHi(b)) {
474 * Check if the table entry that we are going to init/update used
477 if ((FORE_OF(previous) == COLOR_DEFAULT)
478 || (BACK_OF(previous) == COLOR_DEFAULT))
482 * Keep track of the number of entries in the color pair table which
483 * used a default color.
485 if (isDefault && !wasDefault) {
487 } else if (wasDefault && !isDefault) {
492 * As an extension, ncurses allows the pair number to exceed the
493 * terminal's color_pairs value for pairs using a default color.
495 * Note that updating a pair which used a default color with one
496 * that does not will decrement the count - and possibly interfere
497 * with sequentially adding new pairs.
499 if (pair > (SP_PARM->_pair_count + default_pairs)) {
502 SP_PARM->_default_pairs = default_pairs;
506 if ((f < 0) || !OkColorHi(f)
507 || (b < 0) || !OkColorHi(b)
514 * When a pair's content is changed, replace its colors (if pair was
515 * initialized before a screen update is performed replacing original
516 * pair colors with the new ones).
518 result = PAIR_OF(f, b);
520 && previous != result) {
523 for (y = 0; y <= CurScreen(SP_PARM)->_maxy; y++) {
524 struct ldat *ptr = &(CurScreen(SP_PARM)->_line[y]);
525 bool changed = FALSE;
526 for (x = 0; x <= CurScreen(SP_PARM)->_maxx; x++) {
527 if (GetPair(ptr->text[x]) == pair) {
528 /* Set the old cell to zero to ensure it will be
529 updated on the next doupdate() */
530 SetChar(ptr->text[x], 0, 0);
531 CHANGED_CELL(ptr, x);
536 NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
540 SP_PARM->_color_pairs[pair] = result;
541 if (GET_SCREEN_PAIR(SP_PARM) == pair)
542 SET_SCREEN_PAIR(SP_PARM, (chtype) (~0)); /* force attribute update */
544 #ifdef USE_TERM_DRIVER
545 CallDriver_3(SP_PARM, initpair, pair, f, b);
547 if (initialize_pair && InPalette(f) && InPalette(b)) {
548 const color_t *tp = DefaultPalette;
551 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
553 tp[f].red, tp[f].green, tp[f].blue,
554 tp[b].red, tp[b].green, tp[b].blue));
556 TPUTS_TRACE("initialize_pair");
557 putp(TPARM_7(initialize_pair,
559 tp[f].red, tp[f].green, tp[f].blue,
560 tp[b].red, tp[b].green, tp[b].blue));
569 init_pair(short pair, short f, short b)
571 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
575 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
578 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
579 short color, short r, short g, short b)
584 T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
592 maxcolors = MaxColors;
596 && (color >= 0 && OkColorHi(color))
597 && (okRGB(r) && okRGB(g) && okRGB(b))) {
599 SP_PARM->_color_table[color].init = 1;
600 SP_PARM->_color_table[color].r = r;
601 SP_PARM->_color_table[color].g = g;
602 SP_PARM->_color_table[color].b = b;
606 &SP_PARM->_color_table[color].red,
607 &SP_PARM->_color_table[color].green,
608 &SP_PARM->_color_table[color].blue);
610 SP_PARM->_color_table[color].red = r;
611 SP_PARM->_color_table[color].green = g;
612 SP_PARM->_color_table[color].blue = b;
615 #ifdef USE_TERM_DRIVER
616 CallDriver_4(SP_PARM, initcolor, color, r, g, b);
618 TPUTS_TRACE("initialize_color");
619 putp(TPARM_4(initialize_color, color, r, g, b));
621 SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
630 init_color(short color, short r, short g, short b)
632 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
637 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
641 T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
643 if (HasTerminal(SP_PARM) && (CanChange != 0)) {
652 can_change_color(void)
654 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
659 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
664 T((T_CALLED("has_colors()")));
665 if (HasTerminal(SP_PARM)) {
666 #ifdef USE_TERM_DRIVER
669 code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
670 && (((set_foreground != NULL)
671 && (set_background != NULL))
672 || ((set_a_foreground != NULL)
673 && (set_a_background != NULL))
674 || set_color_pair)) ? TRUE : FALSE);
684 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
689 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
690 short color, short *r, short *g, short *b)
695 T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
705 maxcolors = MaxColors;
707 if (color < 0 || !OkColorHi(color) || !SP_PARM->_coloron) {
710 NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
711 NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
712 NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
721 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
722 color, c_r, c_g, c_b));
730 color_content(short color, short *r, short *g, short *b)
732 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
737 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
738 short pair, short *f, short *b)
742 T((T_CALLED("pair_content(%p,%d,%p,%p)"),
748 if (!ValidPair(pair)) {
751 NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
752 NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
754 #if NCURSES_EXT_FUNCS
755 if (fg == COLOR_DEFAULT)
757 if (bg == COLOR_DEFAULT)
766 TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
777 pair_content(short pair, short *f, short *b)
779 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
784 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
788 NCURSES_SP_OUTC outc)
790 #ifdef USE_TERM_DRIVER
791 CallDriver_4(SP_PARM, docolor, old_pair, pair, reverse, outc);
793 NCURSES_COLOR_T fg = COLOR_DEFAULT;
794 NCURSES_COLOR_T bg = COLOR_DEFAULT;
795 NCURSES_COLOR_T old_fg, old_bg;
797 if (!ValidPair(pair)) {
799 } else if (pair != 0) {
800 if (set_color_pair) {
801 TPUTS_TRACE("set_color_pair");
802 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
803 TPARM_1(set_color_pair, pair),
806 } else if (SP_PARM != 0) {
807 pair_content((short) pair, &fg, &bg);
813 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
814 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
815 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
816 #if NCURSES_EXT_FUNCS
818 * A minor optimization - but extension. If "AX" is specified in
819 * the terminal description, treat it as screen's indicator of ECMA
820 * SGR 39 and SGR 49, and assume the two sequences are independent.
822 if (SP_PARM->_has_sgr_39_49
823 && isDefaultColor(old_bg)
824 && !isDefaultColor(old_fg)) {
825 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
826 } else if (SP_PARM->_has_sgr_39_49
827 && isDefaultColor(old_fg)
828 && !isDefaultColor(old_bg)) {
829 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
832 reset_color_pair(NCURSES_SP_ARG);
835 reset_color_pair(NCURSES_SP_ARG);
840 #if NCURSES_EXT_FUNCS
841 if (isDefaultColor(fg))
842 fg = (short) default_fg(NCURSES_SP_ARG);
843 if (isDefaultColor(bg))
844 bg = (short) default_bg(NCURSES_SP_ARG);
848 NCURSES_COLOR_T xx = fg;
853 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
856 if (!isDefaultColor(fg)) {
857 set_foreground_color(NCURSES_SP_ARGx fg, outc);
859 if (!isDefaultColor(bg)) {
860 set_background_color(NCURSES_SP_ARGx bg, outc);
867 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
869 SetSafeOutcWrapper(outc);
870 NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,