]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
3ad821504a9480cf941d0e26df735a83ab635763
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  ****************************************************************************/
35
36 /* lib_color.c
37  *
38  * Handles color emulation of SYS V curses
39  */
40
41 #include <curses.priv.h>
42
43 #include <term.h>
44 #include <tic.h>
45
46 #ifndef CUR
47 #define CUR SP_TERMTYPE 
48 #endif
49
50 MODULE_ID("$Id: lib_color.c,v 1.91 2009/05/10 00:48:29 tom Exp $")
51
52 /*
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.
56  */
57 #if USE_REENTRANT
58 NCURSES_EXPORT(int)
59 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
60 {
61     return SP ? SP->_pair_count : -1;
62 }
63 NCURSES_EXPORT(int)
64 NCURSES_PUBLIC_VAR(COLORS) (void)
65 {
66     return SP ? SP->_color_count : -1;
67 }
68 #else
69 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
70 NCURSES_EXPORT_VAR(int) COLORS = 0;
71 #endif
72
73 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
74
75 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
76
77 #define MAX_PALETTE     8
78
79 #define OkColorHi(n)    (((n) < COLORS) && ((n) < max_colors))
80 #define InPalette(n)    ((n) >= 0 && (n) < MAX_PALETTE)
81
82 /*
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.
85  */
86 #define RGB_ON  680
87 #define RGB_OFF 0
88 /* *INDENT-OFF* */
89 static const color_t cga_palette[] =
90 {
91     /*  R               G               B */
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 */
100 };
101
102 static const color_t hls_palette[] =
103 {
104     /*          H       L       S */
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 */
113 };
114 /* *INDENT-ON* */
115
116 /*
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.
119  */
120 #define ValidPair(pair) \
121     ((SP != 0) && (pair >= 0) && (pair < SP->_pair_limit) && SP->_coloron)
122
123 #if NCURSES_EXT_FUNCS
124 /*
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.
127  */
128 static int
129 default_fg(void)
130 {
131     return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
132 }
133
134 static int
135 default_bg(void)
136 {
137     return SP != 0 ? SP->_default_bg : COLOR_BLACK;
138 }
139 #else
140 #define default_fg() COLOR_WHITE
141 #define default_bg() COLOR_BLACK
142 #endif
143
144 /*
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.
148  */
149 static int
150 toggled_colors(int c)
151 {
152     if (c < 16) {
153         static const int table[] =
154         {0, 4, 2, 6, 1, 5, 3, 7,
155          8, 12, 10, 14, 9, 13, 11, 15};
156         c = table[c];
157     }
158     return c;
159 }
160
161 static void
162 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
163 {
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),
168                                 1, outc);
169     } else {
170         TPUTS_TRACE("set_background");
171         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
172                                 TPARM_1(set_background, toggled_colors(bg)),
173                                 1, outc);
174     }
175 }
176
177 static void
178 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
179 {
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),
184                                 1, outc);
185     } else {
186         TPUTS_TRACE("set_foreground");
187         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
188                                 TPARM_1(set_foreground, toggled_colors(fg)),
189                                 1, outc);
190     }
191 }
192
193 static void
194 init_color_table(NCURSES_SP_DCL0)
195 {
196     const color_t *tp;
197     int n;
198
199     tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
200     for (n = 0; n < COLORS; n++) {
201         if (InPalette(n)) {
202             SP->_color_table[n] = tp[n];
203         } else {
204             SP->_color_table[n] = tp[n % MAX_PALETTE];
205             if (hue_lightness_saturation) {
206                 SP->_color_table[n].green = 100;
207             } else {
208                 if (SP->_color_table[n].red)
209                     SP->_color_table[n].red = 1000;
210                 if (SP->_color_table[n].green)
211                     SP->_color_table[n].green = 1000;
212                 if (SP->_color_table[n].blue)
213                     SP->_color_table[n].blue = 1000;
214             }
215         }
216     }
217 }
218
219 /*
220  * Reset the color pair, e.g., to whatever color pair 0 is.
221  */
222 static bool
223 reset_color_pair(NCURSES_SP_DCL0)
224 {
225     bool result = FALSE;
226
227     if (orig_pair != 0) {
228         TPUTS_TRACE("orig_pair");
229         putp(orig_pair);
230         result = TRUE;
231     }
232     return result;
233 }
234
235 /*
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.
239  */
240 bool
241 _nc_reset_colors(void)
242 {
243 #if NCURSES_SP_FUNCS
244     SCREEN *sp = CURRENT_SCREEN;
245 #endif
246     int result = FALSE;
247
248     T((T_CALLED("_nc_reset_colors()")));
249     if (SP->_color_defs > 0)
250         SP->_color_defs = -(SP->_color_defs);
251
252     if (reset_color_pair(NCURSES_SP_ARG))
253         result = TRUE;
254     if (orig_colors != 0) {
255         TPUTS_TRACE("orig_colors");
256         putp(orig_colors);
257         result = TRUE;
258     }
259     returnBool(result);
260 }
261
262 NCURSES_EXPORT(int)
263 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
264 {
265     int result = ERR;
266
267     T((T_CALLED("start_color()")));
268
269     if (SP_PARM == 0) {
270         result = ERR;
271     } else if (SP_PARM->_coloron) {
272         result = OK;
273     } else {
274
275         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
276             set_foreground_color(NCURSES_SP_ARGx
277                                  default_fg(),
278                                  NCURSES_SP_NAME(_nc_outch));
279             set_background_color(NCURSES_SP_ARGx
280                                  default_bg(),
281                                  NCURSES_SP_NAME(_nc_outch));
282         }
283
284         if (max_pairs > 0 && max_colors > 0) {
285             SP_PARM->_pair_limit = max_pairs;
286
287 #if NCURSES_EXT_FUNCS
288             /*
289              * If using default colors, allocate extra space in table to
290              * allow for default-color as a component of a color-pair.
291              */
292             SP_PARM->_pair_limit += (1 + (2 * max_colors));
293 #endif
294             SP_PARM->_pair_count = max_pairs;
295             SP_PARM->_color_count = max_colors;
296 #if !USE_REENTRANT
297             COLOR_PAIRS = max_pairs;
298             COLORS = max_colors;
299 #endif
300
301             SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t,
302                                                 SP_PARM->_pair_limit);
303             if (SP_PARM->_color_pairs != 0) {
304                 SP_PARM->_color_table = TYPE_CALLOC(color_t, max_colors);
305                 if (SP_PARM->_color_table != 0) {
306                     SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(),
307                                                        default_bg());
308                     init_color_table(NCURSES_SP_ARG);
309
310                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
311                        COLORS, COLOR_PAIRS));
312
313                     SP_PARM->_coloron = 1;
314                     result = OK;
315                 } else if (SP_PARM->_color_pairs != 0) {
316                     FreeAndNull(SP_PARM->_color_pairs);
317                 }
318             }
319         } else {
320             result = OK;
321         }
322     }
323     returnCode(result);
324 }
325
326 #if NCURSES_SP_FUNCS
327 NCURSES_EXPORT(int)
328 start_color(void)
329 {
330     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
331 }
332 #endif
333
334 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
335 static void
336 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
337 /* convert RGB to HLS system */
338 {
339     short min, max, t;
340
341     if ((min = g < r ? g : r) > b)
342         min = b;
343     if ((max = g > r ? g : r) < b)
344         max = b;
345
346     /* calculate lightness */
347     *l = (min + max) / 20;
348
349     if (min == max) {           /* black, white and all shades of gray */
350         *h = 0;
351         *s = 0;
352         return;
353     }
354
355     /* calculate saturation */
356     if (*l < 50)
357         *s = ((max - min) * 100) / (max + min);
358     else
359         *s = ((max - min) * 100) / (2000 - max - min);
360
361     /* calculate hue */
362     if (r == max)
363         t = 120 + ((g - b) * 60) / (max - min);
364     else if (g == max)
365         t = 240 + ((b - r) * 60) / (max - min);
366     else
367         t = 360 + ((r - g) * 60) / (max - min);
368
369     *h = t % 360;
370 }
371
372 /*
373  * Extension (1997/1/18) - Allow negative f/b values to set default color
374  * values.
375  */
376 NCURSES_EXPORT(int)
377 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
378 {
379     colorpair_t result;
380     colorpair_t previous;
381
382     T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
383
384     if (!ValidPair(pair))
385         returnCode(ERR);
386
387     previous = SP_PARM->_color_pairs[pair];
388 #if NCURSES_EXT_FUNCS
389     if (SP_PARM->_default_color) {
390         bool isDefault = FALSE;
391         bool wasDefault = FALSE;
392         int default_pairs = SP_PARM->_default_pairs;
393
394         /*
395          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
396          * internal unsigned values which we will store in the _color_pairs[]
397          * table.
398          */
399         if (isDefaultColor(f)) {
400             f = COLOR_DEFAULT;
401             isDefault = TRUE;
402         } else if (!OkColorHi(f)) {
403             returnCode(ERR);
404         }
405
406         if (isDefaultColor(b)) {
407             b = COLOR_DEFAULT;
408             isDefault = TRUE;
409         } else if (!OkColorHi(b)) {
410             returnCode(ERR);
411         }
412
413         /*
414          * Check if the table entry that we are going to init/update used
415          * default colors.
416          */
417         if ((FORE_OF(previous) == COLOR_DEFAULT)
418             || (BACK_OF(previous) == COLOR_DEFAULT))
419             wasDefault = TRUE;
420
421         /*
422          * Keep track of the number of entries in the color pair table which
423          * used a default color.
424          */
425         if (isDefault && !wasDefault) {
426             ++default_pairs;
427         } else if (wasDefault && !isDefault) {
428             --default_pairs;
429         }
430
431         /*
432          * As an extension, ncurses allows the pair number to exceed the
433          * terminal's color_pairs value for pairs using a default color.
434          *
435          * Note that updating a pair which used a default color with one
436          * that does not will decrement the count - and possibly interfere
437          * with sequentially adding new pairs.
438          */
439         if (pair > (SP_PARM->_pair_count + default_pairs)) {
440             returnCode(ERR);
441         }
442         SP_PARM->_default_pairs = default_pairs;
443     } else
444 #endif
445     {
446         if ((f < 0) || !OkColorHi(f)
447             || (b < 0) || !OkColorHi(b)
448             || (pair < 1))
449             returnCode(ERR);
450     }
451
452     /*
453      * When a pair's content is changed, replace its colors (if pair was
454      * initialized before a screen update is performed replacing original
455      * pair colors with the new ones).
456      */
457     result = PAIR_OF(f, b);
458     if (previous != 0
459         && previous != result) {
460         int y, x;
461
462         for (y = 0; y <= curscr->_maxy; y++) {
463             struct ldat *ptr = &(curscr->_line[y]);
464             bool changed = FALSE;
465             for (x = 0; x <= curscr->_maxx; x++) {
466                 if (GetPair(ptr->text[x]) == pair) {
467                     /* Set the old cell to zero to ensure it will be
468                        updated on the next doupdate() */
469                     SetChar(ptr->text[x], 0, 0);
470                     CHANGED_CELL(ptr, x);
471                     changed = TRUE;
472                 }
473             }
474             if (changed)
475                 _nc_make_oldhash(y);
476         }
477     }
478     SP_PARM->_color_pairs[pair] = result;
479     if (GET_SCREEN_PAIR(SP_PARM) == pair)
480         SET_SCREEN_PAIR(SP_PARM, (chtype) (~0));        /* force attribute update */
481
482     if (initialize_pair && InPalette(f) && InPalette(b)) {
483         const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
484
485         TR(TRACE_ATTRS,
486            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
487             pair,
488             tp[f].red, tp[f].green, tp[f].blue,
489             tp[b].red, tp[b].green, tp[b].blue));
490
491         TPUTS_TRACE("initialize_pair");
492         putp(TPARM_7(initialize_pair,
493                      pair,
494                      tp[f].red, tp[f].green, tp[f].blue,
495                      tp[b].red, tp[b].green, tp[b].blue));
496     }
497
498     returnCode(OK);
499 }
500
501 #if NCURSES_SP_FUNCS
502 NCURSES_EXPORT(int)
503 init_pair(short pair, short f, short b)
504 {
505     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
506 }
507 #endif
508
509 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
510
511 NCURSES_EXPORT(int)
512 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
513                              short color, short r, short g, short b)
514 {
515     int result = ERR;
516
517     T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
518
519     if (initialize_color != NULL
520         && SP_PARM != 0
521         && SP_PARM->_coloron
522         && (color >= 0 && OkColorHi(color))
523         && (okRGB(r) && okRGB(g) && okRGB(b))) {
524
525         SP_PARM->_color_table[color].init = 1;
526         SP_PARM->_color_table[color].r = r;
527         SP_PARM->_color_table[color].g = g;
528         SP_PARM->_color_table[color].b = b;
529
530         if (hue_lightness_saturation) {
531             rgb2hls(r, g, b,
532                     &SP_PARM->_color_table[color].red,
533                     &SP_PARM->_color_table[color].green,
534                     &SP_PARM->_color_table[color].blue);
535         } else {
536             SP_PARM->_color_table[color].red = r;
537             SP_PARM->_color_table[color].green = g;
538             SP_PARM->_color_table[color].blue = b;
539         }
540
541         TPUTS_TRACE("initialize_color");
542         putp(TPARM_4(initialize_color, color, r, g, b));
543         SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
544         result = OK;
545     }
546     returnCode(result);
547 }
548
549 #if NCURSES_SP_FUNCS
550 NCURSES_EXPORT(int)
551 init_color(short color, short r, short g, short b)
552 {
553     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
554 }
555 #endif
556
557 NCURSES_EXPORT(bool)
558 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
559 {
560     T((T_CALLED("can_change_color()")));
561     returnCode((can_change != 0) ? TRUE : FALSE);
562 }
563
564 #if NCURSES_SP_FUNCS
565 NCURSES_EXPORT(bool)
566 can_change_color(void)
567 {
568     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
569 }
570 #endif
571
572 NCURSES_EXPORT(bool)
573 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
574 {
575     T((T_CALLED("has_colors()")));
576     returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
577                 && (((set_foreground != NULL)
578                      && (set_background != NULL))
579                     || ((set_a_foreground != NULL)
580                         && (set_a_background != NULL))
581                     || set_color_pair)) ? TRUE : FALSE);
582 }
583
584 #if NCURSES_SP_FUNCS
585 NCURSES_EXPORT(bool)
586 has_colors(void)
587 {
588     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
589 }
590 #endif
591
592 NCURSES_EXPORT(int)
593 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
594                                 short *g, short *b)
595 {
596     int result;
597
598     T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
599     if (color < 0 || !OkColorHi(color) || SP_PARM == 0 || !SP_PARM->_coloron) {
600         result = ERR;
601     } else {
602         NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
603         NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
604         NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
605
606         if (r)
607             *r = c_r;
608         if (g)
609             *g = c_g;
610         if (b)
611             *b = c_b;
612
613         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
614                          color, c_r, c_g, c_b));
615         result = OK;
616     }
617     returnCode(result);
618 }
619
620 #if NCURSES_SP_FUNCS
621 NCURSES_EXPORT(int)
622 color_content(short color, short *r, short *g, short *b)
623 {
624     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
625 }
626 #endif
627
628 NCURSES_EXPORT(int)
629 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
630                                short *b)
631 {
632     int result;
633
634     T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
635
636     if (!ValidPair(pair)) {
637         result = ERR;
638     } else {
639         NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
640         NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
641
642 #if NCURSES_EXT_FUNCS
643         if (fg == COLOR_DEFAULT)
644             fg = -1;
645         if (bg == COLOR_DEFAULT)
646             bg = -1;
647 #endif
648
649         if (f)
650             *f = fg;
651         if (b)
652             *b = bg;
653
654         TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
655         result = OK;
656     }
657     returnCode(result);
658 }
659
660 #if NCURSES_SP_FUNCS
661 NCURSES_EXPORT(int)
662 pair_content(short pair, short *f, short *b)
663 {
664     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
665 }
666 #endif
667
668 NCURSES_EXPORT(void)
669 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
670                                short old_pair,
671                                short pair,
672                                bool reverse,
673                                NCURSES_SP_OUTC outc)
674 {
675     NCURSES_COLOR_T fg = COLOR_DEFAULT;
676     NCURSES_COLOR_T bg = COLOR_DEFAULT;
677     NCURSES_COLOR_T old_fg, old_bg;
678
679     if (!ValidPair(pair)) {
680         return;
681     } else if (pair != 0) {
682         if (set_color_pair) {
683             TPUTS_TRACE("set_color_pair");
684             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
685                                     TPARM_1(set_color_pair, pair),
686                                     1, outc);
687             return;
688         } else if (SP != 0) {
689             pair_content((short) pair, &fg, &bg);
690         }
691     }
692
693     if (old_pair >= 0
694         && SP != 0
695         && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
696         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
697             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
698 #if NCURSES_EXT_FUNCS
699             /*
700              * A minor optimization - but extension.  If "AX" is specified in
701              * the terminal description, treat it as screen's indicator of ECMA
702              * SGR 39 and SGR 49, and assume the two sequences are independent.
703              */
704             if (SP->_has_sgr_39_49
705                 && isDefaultColor(old_bg)
706                 && !isDefaultColor(old_fg)) {
707                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
708             } else if (SP->_has_sgr_39_49
709                        && isDefaultColor(old_fg)
710                        && !isDefaultColor(old_bg)) {
711                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
712             } else
713 #endif
714                 reset_color_pair(NCURSES_SP_ARG);
715         }
716     } else {
717         reset_color_pair(NCURSES_SP_ARG);
718         if (old_pair < 0)
719             return;
720     }
721
722 #if NCURSES_EXT_FUNCS
723     if (isDefaultColor(fg))
724         fg = default_fg();
725     if (isDefaultColor(bg))
726         bg = default_bg();
727 #endif
728
729     if (reverse) {
730         NCURSES_COLOR_T xx = fg;
731         fg = bg;
732         bg = xx;
733     }
734
735     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
736                      fg, bg));
737
738     if (!isDefaultColor(fg)) {
739         set_foreground_color(NCURSES_SP_ARGx fg, outc);
740     }
741     if (!isDefaultColor(bg)) {
742         set_background_color(NCURSES_SP_ARGx bg, outc);
743     }
744 }
745
746 #if NCURSES_SP_FUNCS
747 NCURSES_EXPORT(void)
748 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
749 {
750     SetSafeOutcWrapper(outc);
751     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
752                                    old_pair,
753                                    pair,
754                                    reverse,
755                                    _nc_outc_wrapper);
756 }
757 #endif