1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
39 * Handles color emulation of SYS V curses
42 #define NEW_PAIR_INTERNAL 1
44 #include <curses.priv.h>
49 #define CUR SP_TERMTYPE
52 MODULE_ID("$Id: lib_color.c,v 1.150 2023/09/16 16:39:15 tom Exp $")
54 #ifdef USE_TERM_DRIVER
55 #define CanChange InfoOf(SP_PARM).canchange
56 #define DefaultPalette InfoOf(SP_PARM).defaultPalette
57 #define HasColor InfoOf(SP_PARM).hascolor
58 #define InitColor InfoOf(SP_PARM).initcolor
59 #define MaxColors InfoOf(SP_PARM).maxcolors
60 #define MaxPairs InfoOf(SP_PARM).maxpairs
61 #define UseHlsPalette (DefaultPalette == _nc_hls_palette)
63 #define CanChange can_change
64 #define DefaultPalette (hue_lightness_saturation ? hls_palette : cga_palette)
65 #define HasColor has_color
66 #define InitColor initialize_color
67 #define MaxColors max_colors
68 #define MaxPairs max_pairs
69 #define UseHlsPalette (hue_lightness_saturation)
72 #ifndef USE_TERM_DRIVER
74 * These should be screen structure members. They need to be globals for
75 * historical reasons. So we assign them in start_color() and also in
76 * set_term()'s screen-switching logic.
80 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
82 return SP ? SP->_pair_count : -1;
85 NCURSES_PUBLIC_VAR(COLORS) (void)
87 return SP ? SP->_color_count : -1;
90 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
91 NCURSES_EXPORT_VAR(int) COLORS = 0;
93 #endif /* !USE_TERM_DRIVER */
95 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
99 #define OkColorHi(n) (((n) < COLORS) && ((n) < maxcolors))
100 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
103 * Given a RGB range of 0..1000, we'll normally set the individual values
104 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
109 static const color_t cga_palette[] =
112 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
113 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
114 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
115 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
116 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
117 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
118 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
119 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
122 static const color_t hls_palette[] =
125 DATA( 0, 0, 0), /* COLOR_BLACK */
126 DATA( 120, 50, 100), /* COLOR_RED */
127 DATA( 240, 50, 100), /* COLOR_GREEN */
128 DATA( 180, 50, 100), /* COLOR_YELLOW */
129 DATA( 330, 50, 100), /* COLOR_BLUE */
130 DATA( 60, 50, 100), /* COLOR_MAGENTA */
131 DATA( 300, 50, 100), /* COLOR_CYAN */
132 DATA( 0, 50, 100), /* COLOR_WHITE */
135 #ifdef USE_TERM_DRIVER
136 NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette = cga_palette;
137 NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
141 #if NCURSES_EXT_FUNCS
143 * These are called from _nc_do_color(), which in turn is called from
144 * vidattr - so we have to assume that sp may be null.
147 default_fg(NCURSES_SP_DCL0)
149 return (SP_PARM != 0) ? SP_PARM->_default_fg : COLOR_WHITE;
153 default_bg(NCURSES_SP_DCL0)
155 return SP_PARM != 0 ? SP_PARM->_default_bg : COLOR_BLACK;
158 #define default_fg(sp) COLOR_WHITE
159 #define default_bg(sp) COLOR_BLACK
162 #ifndef USE_TERM_DRIVER
164 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
165 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
166 * also used in the FreeBSD syscons.
169 toggled_colors(int c)
172 static const int table[] =
173 {0, 4, 2, 6, 1, 5, 3, 7,
174 8, 12, 10, 14, 9, 13, 11, 15};
182 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
184 #ifdef USE_TERM_DRIVER
185 CallDriver_3(SP_PARM, td_color, FALSE, bg, outc);
187 if (set_a_background) {
188 TPUTS_TRACE("set_a_background");
189 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
190 TIPARM_1(set_a_background, bg),
193 TPUTS_TRACE("set_background");
194 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
195 TIPARM_1(set_background, toggled_colors(bg)),
202 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
204 #ifdef USE_TERM_DRIVER
205 CallDriver_3(SP_PARM, td_color, TRUE, fg, outc);
207 if (set_a_foreground) {
208 TPUTS_TRACE("set_a_foreground");
209 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
210 TIPARM_1(set_a_foreground, fg),
213 TPUTS_TRACE("set_foreground");
214 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
215 TIPARM_1(set_foreground, toggled_colors(fg)),
222 init_color_table(NCURSES_SP_DCL0)
224 const color_t *tp = DefaultPalette;
229 for (n = 0; n < COLORS; n++) {
231 SP_PARM->_color_table[n] = tp[n];
233 SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
235 SP_PARM->_color_table[n].green = 100;
237 if (SP_PARM->_color_table[n].red)
238 SP_PARM->_color_table[n].red = 1000;
239 if (SP_PARM->_color_table[n].green)
240 SP_PARM->_color_table[n].green = 1000;
241 if (SP_PARM->_color_table[n].blue)
242 SP_PARM->_color_table[n].blue = 1000;
249 init_direct_colors(NCURSES_SP_DCL0)
251 static NCURSES_CONST char name[] = "RGB";
253 rgb_bits_t *result = &(SP_PARM->_direct_color);
262 /* find the number of bits needed for the maximum color value */
263 for (width = 0; (1 << width) - 1 < (COLORS - 1); ++width) {
267 if (tigetflag(name) > 0) {
269 result->bits.red = UChar(n);
270 result->bits.green = UChar(n);
271 result->bits.blue = UChar(width - (2 * n));
272 } else if ((n = tigetnum(name)) > 0) {
273 result->bits.red = UChar(n);
274 result->bits.green = UChar(n);
275 result->bits.blue = UChar(n);
276 } else if ((s = tigetstr(name)) != 0 && VALID_STRING(s)) {
279 int blue = width - (2 * n);
281 switch (sscanf(s, "%d/%d/%d", &red, &green, &blue)) {
283 blue = width - (2 * n);
295 result->bits.red = UChar(red);
296 result->bits.green = UChar(green);
297 result->bits.blue = UChar(blue);
300 return (result->value != 0);
304 * Reset the color pair, e.g., to whatever color pair 0 is.
307 reset_color_pair(NCURSES_SP_DCL0)
309 #ifdef USE_TERM_DRIVER
310 return CallDriver(SP_PARM, td_rescol);
315 if (orig_pair != 0) {
316 (void) NCURSES_PUTP2("orig_pair", orig_pair);
324 * Reset color pairs and definitions. Actually we do both more to accommodate
325 * badly-written terminal descriptions than for the relatively rare case where
326 * someone has changed the color definitions.
329 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
333 T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
334 if (SP_PARM->_color_defs > 0)
335 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
336 if (reset_color_pair(NCURSES_SP_ARG))
339 #ifdef USE_TERM_DRIVER
340 result = CallDriver(SP_PARM, td_rescolors);
342 if (orig_colors != 0) {
343 NCURSES_PUTP2("orig_colors", orig_colors);
352 _nc_reset_colors(void)
354 return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
359 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
363 T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
367 } else if (SP_PARM->_coloron) {
370 int maxpairs = MaxPairs;
371 int maxcolors = MaxColors;
372 if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
373 set_foreground_color(NCURSES_SP_ARGx
374 default_fg(NCURSES_SP_ARG),
375 NCURSES_SP_NAME(_nc_outch));
376 set_background_color(NCURSES_SP_ARGx
377 default_bg(NCURSES_SP_ARG),
378 NCURSES_SP_NAME(_nc_outch));
380 #if !NCURSES_EXT_COLORS
382 * Without ext-colors, we cannot represent more than 256 color pairs.
388 if (maxpairs > 0 && maxcolors > 0) {
389 SP_PARM->_pair_limit = maxpairs;
391 #if NCURSES_EXT_FUNCS
393 * If using default colors, allocate extra space in table to
394 * allow for default-color as a component of a color-pair.
396 SP_PARM->_pair_limit += (1 + (2 * maxcolors));
397 #if !NCURSES_EXT_COLORS
398 SP_PARM->_pair_limit = limit_PAIRS(SP_PARM->_pair_limit);
400 #endif /* NCURSES_EXT_FUNCS */
401 SP_PARM->_pair_count = maxpairs;
402 SP_PARM->_color_count = maxcolors;
404 COLOR_PAIRS = maxpairs;
408 ReservePairs(SP_PARM, 16);
409 if (SP_PARM->_color_pairs != 0) {
410 if (init_direct_colors(NCURSES_SP_ARG)) {
413 TYPE_CALLOC(color_t, maxcolors, SP_PARM->_color_table);
414 if (SP_PARM->_color_table != 0) {
415 MakeColorPair(SP_PARM->_color_pairs[0],
416 default_fg(NCURSES_SP_ARG),
417 default_bg(NCURSES_SP_ARG));
418 init_color_table(NCURSES_SP_ARG);
424 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
425 COLORS, COLOR_PAIRS));
427 SP_PARM->_coloron = 1;
428 } else if (SP_PARM->_color_pairs != 0) {
429 FreeAndNull(SP_PARM->_color_pairs);
443 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
447 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
449 rgb2hls(int r, int g, int b, int *h, int *l, int *s)
450 /* convert RGB to HLS system */
454 if ((min = g < r ? g : r) > b)
456 if ((max = g > r ? g : r) < b)
459 /* calculate lightness */
460 *l = ((min + max) / 20);
462 if (min == max) { /* black, white and all shades of gray */
468 /* calculate saturation */
470 *s = (((max - min) * 100) / (max + min));
472 *s = (((max - min) * 100) / (2000 - max - min));
476 t = (120 + ((g - b) * 60) / (max - min));
478 t = (240 + ((b - r) * 60) / (max - min));
480 t = (360 + ((r - g) * 60) / (max - min));
486 * Change all cells which use(d) a given color pair to force a repaint.
489 _nc_change_pair(SCREEN *sp, int pair)
493 if (CurScreen(sp)->_clear)
496 if (_nc_globals.leak_checking)
500 for (y = 0; y <= CurScreen(sp)->_maxy; y++) {
501 struct ldat *ptr = &(CurScreen(sp)->_line[y]);
502 bool changed = FALSE;
503 for (x = 0; x <= CurScreen(sp)->_maxx; x++) {
504 if (GetPair(ptr->text[x]) == pair) {
505 /* Set the old cell to zero to ensure it will be
506 updated on the next doupdate() */
507 SetChar(ptr->text[x], 0, 0);
508 CHANGED_CELL(ptr, x);
513 NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
518 _nc_reserve_pairs(SCREEN *sp, int want)
520 int have = sp->_pair_alloc;
526 if (have > sp->_pair_limit)
527 have = sp->_pair_limit;
529 if (sp->_color_pairs == 0) {
530 TYPE_CALLOC(colorpair_t, have, sp->_color_pairs);
531 } else if (have > sp->_pair_alloc) {
532 #if NCURSES_EXT_COLORS
535 if ((next = typeCalloc(colorpair_t, have)) == 0)
536 _nc_err_abort(MSG_NO_MEMORY);
537 memcpy(next, sp->_color_pairs, (size_t) sp->_pair_alloc * sizeof(*next));
538 _nc_copy_pairs(sp, next, sp->_color_pairs, sp->_pair_alloc);
539 free(sp->_color_pairs);
540 sp->_color_pairs = next;
542 TYPE_REALLOC(colorpair_t, have, sp->_color_pairs);
543 if (sp->_color_pairs != 0) {
544 memset(sp->_color_pairs + sp->_pair_alloc, 0,
545 sizeof(colorpair_t) * (size_t) (have - sp->_pair_alloc));
549 if (sp->_color_pairs != 0) {
550 sp->_pair_alloc = have;
555 * Extension (1997/1/18) - Allow negative f/b values to set default color
559 _nc_init_pair(SCREEN *sp, int pair, int f, int b)
561 static colorpair_t null_pair;
562 colorpair_t result = null_pair;
563 colorpair_t previous;
566 T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) sp, pair, f, b));
568 if (!ValidPair(sp, pair))
571 maxcolors = MaxColors;
573 ReservePairs(sp, pair);
574 previous = sp->_color_pairs[pair];
575 #if NCURSES_EXT_FUNCS
576 if (sp->_default_color || sp->_assumed_color) {
577 bool isDefault = FALSE;
578 bool wasDefault = FALSE;
579 int default_pairs = sp->_default_pairs;
582 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
583 * internal unsigned values which we will store in the _color_pairs[]
586 if (isDefaultColor(f)) {
589 } else if (!OkColorHi(f)) {
593 if (isDefaultColor(b)) {
596 } else if (!OkColorHi(b)) {
601 * Check if the table entry that we are going to init/update used
604 if (isDefaultColor(FORE_OF(previous))
605 || isDefaultColor(BACK_OF(previous)))
609 * Keep track of the number of entries in the color pair table which
610 * used a default color.
612 if (isDefault && !wasDefault) {
614 } else if (wasDefault && !isDefault) {
619 * As an extension, ncurses allows the pair number to exceed the
620 * terminal's color_pairs value for pairs using a default color.
622 * Note that updating a pair which used a default color with one
623 * that does not will decrement the count - and possibly interfere
624 * with sequentially adding new pairs.
626 if (pair > (sp->_pair_count + default_pairs)) {
629 sp->_default_pairs = default_pairs;
633 if ((f < 0) || !OkColorHi(f)
634 || (b < 0) || !OkColorHi(b)
641 * When a pair's content is changed, replace its colors (if pair was
642 * initialized before a screen update is performed replacing original
643 * pair colors with the new ones).
645 MakeColorPair(result, f, b);
646 if ((FORE_OF(previous) != 0
647 || BACK_OF(previous) != 0)
648 && !isSamePair(previous, result)) {
649 _nc_change_pair(sp, pair);
652 _nc_reset_color_pair(sp, pair, &result);
653 sp->_color_pairs[pair] = result;
654 _nc_set_color_pair(sp, pair, cpINIT);
656 if (GET_SCREEN_PAIR(sp) == pair)
657 SET_SCREEN_PAIR(sp, (int) (~0)); /* force attribute update */
659 #ifdef USE_TERM_DRIVER
660 CallDriver_3(sp, td_initpair, pair, f, b);
662 if (initialize_pair && InPalette(f) && InPalette(b)) {
663 const color_t *tp = DefaultPalette;
666 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
668 (int) tp[f].red, (int) tp[f].green, (int) tp[f].blue,
669 (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
671 NCURSES_PUTP2("initialize_pair",
672 TIPARM_7(initialize_pair,
687 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx
688 NCURSES_PAIRS_T pair,
692 return _nc_init_pair(SP_PARM, pair, f, b);
697 init_pair(NCURSES_COLOR_T pair, NCURSES_COLOR_T f, NCURSES_COLOR_T b)
699 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
703 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
706 _nc_init_color(SCREEN *sp, int color, int r, int g, int b)
711 T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
716 if (sp == 0 || sp->_direct_color.value)
719 maxcolors = MaxColors;
723 && (color >= 0 && OkColorHi(color))
724 && (okRGB(r) && okRGB(g) && okRGB(b))) {
726 sp->_color_table[color].init = 1;
727 sp->_color_table[color].r = r;
728 sp->_color_table[color].g = g;
729 sp->_color_table[color].b = b;
733 &sp->_color_table[color].red,
734 &sp->_color_table[color].green,
735 &sp->_color_table[color].blue);
737 sp->_color_table[color].red = r;
738 sp->_color_table[color].green = g;
739 sp->_color_table[color].blue = b;
742 #ifdef USE_TERM_DRIVER
743 CallDriver_4(sp, td_initcolor, color, r, g, b);
745 NCURSES_PUTP2("initialize_color",
746 TIPARM_4(initialize_color, color, r, g, b));
748 sp->_color_defs = Max(color + 1, sp->_color_defs);
756 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
757 NCURSES_COLOR_T color,
762 return _nc_init_color(SP_PARM, color, r, g, b);
767 init_color(NCURSES_COLOR_T color,
772 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
777 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
781 T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
783 if (HasTerminal(SP_PARM) && (CanChange != 0)) {
792 can_change_color(void)
794 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
799 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
804 T((T_CALLED("has_colors(%p)"), (void *) SP_PARM));
805 if (HasTerminal(SP_PARM)) {
806 #ifdef USE_TERM_DRIVER
809 code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
810 && (((set_foreground != NULL)
811 && (set_background != NULL))
812 || ((set_a_foreground != NULL)
813 && (set_a_background != NULL))
814 || set_color_pair)) ? TRUE : FALSE);
824 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
829 _nc_color_content(SCREEN *sp, int color, int *r, int *g, int *b)
833 T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
841 int maxcolors = MaxColors;
843 if (color >= 0 && OkColorHi(color) && sp->_coloron) {
846 if (sp->_direct_color.value) {
847 rgb_bits_t *work = &(sp->_direct_color);
849 #define max_direct_color(name) ((1 << work->bits.name) - 1)
850 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
852 int max_r = max_direct_color(red);
853 int max_g = max_direct_color(green);
854 int max_b = max_direct_color(blue);
858 c_b = value_direct_color(max_b);
859 bitoff += work->bits.blue;
861 c_g = value_direct_color(max_g);
862 bitoff += work->bits.green;
864 c_r = value_direct_color(max_r);
867 c_r = sp->_color_table[color].red;
868 c_g = sp->_color_table[color].green;
869 c_b = sp->_color_table[color].blue;
879 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
880 color, c_r, c_g, c_b));
896 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
897 NCURSES_COLOR_T color,
902 int my_r, my_g, my_b;
903 int rc = _nc_color_content(SP_PARM, color, &my_r, &my_g, &my_b);
905 *r = limit_COLOR(my_r);
906 *g = limit_COLOR(my_g);
907 *b = limit_COLOR(my_b);
914 color_content(NCURSES_COLOR_T color,
919 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
924 _nc_pair_content(SCREEN *sp, int pair, int *f, int *b)
928 T((T_CALLED("pair_content(%p,%d,%p,%p)"),
934 if (!ValidPair(sp, pair)) {
940 ReservePairs(sp, pair);
941 fg = FORE_OF(sp->_color_pairs[pair]);
942 bg = BACK_OF(sp->_color_pairs[pair]);
943 #if NCURSES_EXT_FUNCS
944 if (isDefaultColor(fg))
946 if (isDefaultColor(bg))
955 TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
958 (int) fg, (int) bg));
965 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
966 NCURSES_PAIRS_T pair,
971 int rc = _nc_pair_content(SP_PARM, pair, &my_f, &my_b);
973 *f = limit_COLOR(my_f);
974 *b = limit_COLOR(my_b);
981 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
983 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
988 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
992 NCURSES_SP_OUTC outc)
994 #ifdef USE_TERM_DRIVER
995 CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
997 int fg = COLOR_DEFAULT;
998 int bg = COLOR_DEFAULT;
1002 if (!ValidPair(SP_PARM, pair)) {
1004 } else if (pair != 0) {
1005 if (set_color_pair) {
1006 TPUTS_TRACE("set_color_pair");
1007 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1008 TIPARM_1(set_color_pair, pair),
1011 } else if (SP_PARM != 0) {
1012 if (_nc_pair_content(SP_PARM, pair, &fg, &bg) == ERR)
1019 && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
1020 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
1021 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
1022 #if NCURSES_EXT_FUNCS
1024 * A minor optimization - but extension. If "AX" is specified in
1025 * the terminal description, treat it as screen's indicator of ECMA
1026 * SGR 39 and SGR 49, and assume the two sequences are independent.
1028 if (SP_PARM->_has_sgr_39_49
1029 && isDefaultColor(old_bg)
1030 && !isDefaultColor(old_fg)) {
1031 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
1032 } else if (SP_PARM->_has_sgr_39_49
1033 && isDefaultColor(old_fg)
1034 && !isDefaultColor(old_bg)) {
1035 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
1038 reset_color_pair(NCURSES_SP_ARG);
1041 reset_color_pair(NCURSES_SP_ARG);
1042 if (old_pair < 0 && pair <= 0)
1046 #if NCURSES_EXT_FUNCS
1047 if (isDefaultColor(fg))
1048 fg = default_fg(NCURSES_SP_ARG);
1049 if (isDefaultColor(bg))
1050 bg = default_bg(NCURSES_SP_ARG);
1059 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
1062 if (!isDefaultColor(fg)) {
1063 set_foreground_color(NCURSES_SP_ARGx fg, outc);
1065 if (!isDefaultColor(bg)) {
1066 set_background_color(NCURSES_SP_ARGx bg, outc);
1071 #if NCURSES_SP_FUNCS
1072 NCURSES_EXPORT(void)
1073 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
1075 SetSafeOutcWrapper(outc);
1076 NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
1084 #if NCURSES_EXT_COLORS
1086 NCURSES_SP_NAME(init_extended_pair) (NCURSES_SP_DCLx int pair, int f, int b)
1088 return _nc_init_pair(SP_PARM, pair, f, b);
1092 NCURSES_SP_NAME(init_extended_color) (NCURSES_SP_DCLx
1094 int r, int g, int b)
1096 return _nc_init_color(SP_PARM, color, r, g, b);
1100 NCURSES_SP_NAME(extended_color_content) (NCURSES_SP_DCLx
1102 int *r, int *g, int *b)
1104 return _nc_color_content(SP_PARM, color, r, g, b);
1108 NCURSES_SP_NAME(extended_pair_content) (NCURSES_SP_DCLx
1112 return _nc_pair_content(SP_PARM, pair, f, b);
1115 NCURSES_EXPORT(void)
1116 NCURSES_SP_NAME(reset_color_pairs) (NCURSES_SP_DCL0)
1119 if (SP_PARM->_color_pairs) {
1120 _nc_free_ordered_pairs(SP_PARM);
1121 free(SP_PARM->_color_pairs);
1122 SP_PARM->_color_pairs = 0;
1123 SP_PARM->_pair_alloc = 0;
1124 ReservePairs(SP_PARM, 16);
1125 clearok(CurScreen(SP_PARM), TRUE);
1126 touchwin(StdScreen(SP_PARM));
1131 #if NCURSES_SP_FUNCS
1133 init_extended_pair(int pair, int f, int b)
1135 return NCURSES_SP_NAME(init_extended_pair) (CURRENT_SCREEN, pair, f, b);
1139 init_extended_color(int color, int r, int g, int b)
1141 return NCURSES_SP_NAME(init_extended_color) (CURRENT_SCREEN,
1147 extended_color_content(int color, int *r, int *g, int *b)
1149 return NCURSES_SP_NAME(extended_color_content) (CURRENT_SCREEN,
1155 extended_pair_content(int pair, int *f, int *b)
1157 return NCURSES_SP_NAME(extended_pair_content) (CURRENT_SCREEN, pair, f, b);
1160 NCURSES_EXPORT(void)
1161 reset_color_pairs(void)
1163 NCURSES_SP_NAME(reset_color_pairs) (CURRENT_SCREEN);
1165 #endif /* NCURSES_SP_FUNCS */
1166 #endif /* NCURSES_EXT_COLORS */