]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 5.7 - patch 20090214
[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 MODULE_ID("$Id: lib_color.c,v 1.88 2009/02/15 00:33:02 tom Exp $")
47
48 /*
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.
52  */
53 #if USE_REENTRANT
54 NCURSES_EXPORT(int)
55 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
56 {
57     return SP ? SP->_pair_count : -1;
58 }
59 NCURSES_EXPORT(int)
60 NCURSES_PUBLIC_VAR(COLORS) (void)
61 {
62     return SP ? SP->_color_count : -1;
63 }
64 #else
65 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
66 NCURSES_EXPORT_VAR(int) COLORS = 0;
67 #endif
68
69 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
70
71 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
72
73 #define MAX_PALETTE     8
74
75 #define OkColorHi(n)    (((n) < COLORS) && ((n) < max_colors))
76 #define InPalette(n)    ((n) >= 0 && (n) < MAX_PALETTE)
77
78 /*
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.
81  */
82 #define RGB_ON  680
83 #define RGB_OFF 0
84 /* *INDENT-OFF* */
85 static const color_t cga_palette[] =
86 {
87     /*  R               G               B */
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 */
96 };
97
98 static const color_t hls_palette[] =
99 {
100     /*          H       L       S */
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 */
109 };
110 /* *INDENT-ON* */
111
112 /*
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.
115  */
116 #define ValidPair(pair) \
117     ((SP != 0) && (pair >= 0) && (pair < SP->_pair_limit) && SP->_coloron)
118
119 #if NCURSES_EXT_FUNCS
120 /*
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.
123  */
124 static int
125 default_fg(void)
126 {
127     return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
128 }
129
130 static int
131 default_bg(void)
132 {
133     return SP != 0 ? SP->_default_bg : COLOR_BLACK;
134 }
135 #else
136 #define default_fg() COLOR_WHITE
137 #define default_bg() COLOR_BLACK
138 #endif
139
140 /*
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.
144  */
145 static int
146 toggled_colors(int c)
147 {
148     if (c < 16) {
149         static const int table[] =
150         {0, 4, 2, 6, 1, 5, 3, 7,
151          8, 12, 10, 14, 9, 13, 11, 15};
152         c = table[c];
153     }
154     return c;
155 }
156
157 static void
158 set_background_color(int bg, int (*outc) (int))
159 {
160     if (set_a_background) {
161         TPUTS_TRACE("set_a_background");
162         tputs(TPARM_1(set_a_background, bg), 1, outc);
163     } else {
164         TPUTS_TRACE("set_background");
165         tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
166     }
167 }
168
169 static void
170 set_foreground_color(int fg, int (*outc) (int))
171 {
172     if (set_a_foreground) {
173         TPUTS_TRACE("set_a_foreground");
174         tputs(TPARM_1(set_a_foreground, fg), 1, outc);
175     } else {
176         TPUTS_TRACE("set_foreground");
177         tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
178     }
179 }
180
181 static void
182 init_color_table(void)
183 {
184     const color_t *tp;
185     int n;
186
187     tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
188     for (n = 0; n < COLORS; n++) {
189         if (InPalette(n)) {
190             SP->_color_table[n] = tp[n];
191         } else {
192             SP->_color_table[n] = tp[n % MAX_PALETTE];
193             if (hue_lightness_saturation) {
194                 SP->_color_table[n].green = 100;
195             } else {
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;
202             }
203         }
204     }
205 }
206
207 /*
208  * Reset the color pair, e.g., to whatever color pair 0 is.
209  */
210 static bool
211 reset_color_pair(void)
212 {
213     bool result = FALSE;
214
215     if (orig_pair != 0) {
216         TPUTS_TRACE("orig_pair");
217         putp(orig_pair);
218         result = TRUE;
219     }
220     return result;
221 }
222
223 /*
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.
227  */
228 bool
229 _nc_reset_colors(void)
230 {
231     int result = FALSE;
232
233     T((T_CALLED("_nc_reset_colors()")));
234     if (SP->_color_defs > 0)
235         SP->_color_defs = -(SP->_color_defs);
236
237     if (reset_color_pair())
238         result = TRUE;
239     if (orig_colors != 0) {
240         TPUTS_TRACE("orig_colors");
241         putp(orig_colors);
242         result = TRUE;
243     }
244     returnBool(result);
245 }
246
247 NCURSES_EXPORT(int)
248 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
249 {
250     int result = ERR;
251
252     T((T_CALLED("start_color()")));
253
254     if (SP_PARM == 0) {
255         result = ERR;
256     } else if (SP_PARM->_coloron) {
257         result = OK;
258     } else {
259
260         if (reset_color_pair() != TRUE) {
261             set_foreground_color(default_fg(), _nc_outch);
262             set_background_color(default_bg(), _nc_outch);
263         }
264
265         if (max_pairs > 0 && max_colors > 0) {
266             SP_PARM->_pair_limit = max_pairs;
267
268 #if NCURSES_EXT_FUNCS
269             /*
270              * If using default colors, allocate extra space in table to
271              * allow for default-color as a component of a color-pair.
272              */
273             SP_PARM->_pair_limit += (1 + (2 * max_colors));
274 #endif
275             SP_PARM->_pair_count = max_pairs;
276             SP_PARM->_color_count = max_colors;
277 #if !USE_REENTRANT
278             COLOR_PAIRS = max_pairs;
279             COLORS = max_colors;
280 #endif
281
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(),
288                                                        default_bg());
289                     init_color_table();
290
291                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
292                        COLORS, COLOR_PAIRS));
293
294                     SP_PARM->_coloron = 1;
295                     result = OK;
296                 } else if (SP_PARM->_color_pairs != 0) {
297                     FreeAndNull(SP_PARM->_color_pairs);
298                 }
299             }
300         } else {
301             result = OK;
302         }
303     }
304     returnCode(result);
305 }
306
307 #if NCURSES_SP_FUNCS
308 NCURSES_EXPORT(int)
309 start_color(void)
310 {
311     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
312 }
313 #endif
314
315 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
316 static void
317 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
318 /* convert RGB to HLS system */
319 {
320     short min, max, t;
321
322     if ((min = g < r ? g : r) > b)
323         min = b;
324     if ((max = g > r ? g : r) < b)
325         max = b;
326
327     /* calculate lightness */
328     *l = (min + max) / 20;
329
330     if (min == max) {           /* black, white and all shades of gray */
331         *h = 0;
332         *s = 0;
333         return;
334     }
335
336     /* calculate saturation */
337     if (*l < 50)
338         *s = ((max - min) * 100) / (max + min);
339     else
340         *s = ((max - min) * 100) / (2000 - max - min);
341
342     /* calculate hue */
343     if (r == max)
344         t = 120 + ((g - b) * 60) / (max - min);
345     else if (g == max)
346         t = 240 + ((b - r) * 60) / (max - min);
347     else
348         t = 360 + ((r - g) * 60) / (max - min);
349
350     *h = t % 360;
351 }
352
353 /*
354  * Extension (1997/1/18) - Allow negative f/b values to set default color
355  * values.
356  */
357 NCURSES_EXPORT(int)
358 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
359 {
360     colorpair_t result;
361     colorpair_t previous;
362
363     T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
364
365     if (!ValidPair(pair))
366         returnCode(ERR);
367
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;
374
375         /*
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[]
378          * table.
379          */
380         if (isDefaultColor(f)) {
381             f = COLOR_DEFAULT;
382             isDefault = TRUE;
383         } else if (!OkColorHi(f)) {
384             returnCode(ERR);
385         }
386
387         if (isDefaultColor(b)) {
388             b = COLOR_DEFAULT;
389             isDefault = TRUE;
390         } else if (!OkColorHi(b)) {
391             returnCode(ERR);
392         }
393
394         /*
395          * Check if the table entry that we are going to init/update used
396          * default colors.
397          */
398         if ((FORE_OF(previous) == COLOR_DEFAULT)
399             || (BACK_OF(previous) == COLOR_DEFAULT))
400             wasDefault = TRUE;
401
402         /*
403          * Keep track of the number of entries in the color pair table which
404          * used a default color.
405          */
406         if (isDefault && !wasDefault) {
407             ++default_pairs;
408         } else if (wasDefault && !isDefault) {
409             --default_pairs;
410         }
411
412         /*
413          * As an extension, ncurses allows the pair number to exceed the
414          * terminal's color_pairs value for pairs using a default color.
415          *
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.
419          */
420         if (pair > (SP_PARM->_pair_count + default_pairs)) {
421             returnCode(ERR);
422         }
423         SP_PARM->_default_pairs = default_pairs;
424     } else
425 #endif
426     {
427         if ((f < 0) || !OkColorHi(f)
428             || (b < 0) || !OkColorHi(b)
429             || (pair < 1))
430             returnCode(ERR);
431     }
432
433     /*
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).
437      */
438     result = PAIR_OF(f, b);
439     if (previous != 0
440         && previous != result) {
441         int y, x;
442
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);
452                     changed = TRUE;
453                 }
454             }
455             if (changed)
456                 _nc_make_oldhash(y);
457         }
458     }
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 */
462
463     if (initialize_pair && InPalette(f) && InPalette(b)) {
464         const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
465
466         TR(TRACE_ATTRS,
467            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
468             pair,
469             tp[f].red, tp[f].green, tp[f].blue,
470             tp[b].red, tp[b].green, tp[b].blue));
471
472         TPUTS_TRACE("initialize_pair");
473         putp(TPARM_7(initialize_pair,
474                      pair,
475                      tp[f].red, tp[f].green, tp[f].blue,
476                      tp[b].red, tp[b].green, tp[b].blue));
477     }
478
479     returnCode(OK);
480 }
481
482 #if NCURSES_SP_FUNCS
483 NCURSES_EXPORT(int)
484 init_pair(short pair, short f, short b)
485 {
486     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
487 }
488 #endif
489
490 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
491
492 NCURSES_EXPORT(int)
493 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
494                              short color, short r, short g, short b)
495 {
496     int result = ERR;
497
498     T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
499
500     if (initialize_color != NULL
501         && SP_PARM != 0
502         && SP_PARM->_coloron
503         && (color >= 0 && OkColorHi(color))
504         && (okRGB(r) && okRGB(g) && okRGB(b))) {
505
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;
510
511         if (hue_lightness_saturation) {
512             rgb2hls(r, g, b,
513                     &SP_PARM->_color_table[color].red,
514                     &SP_PARM->_color_table[color].green,
515                     &SP_PARM->_color_table[color].blue);
516         } else {
517             SP_PARM->_color_table[color].red = r;
518             SP_PARM->_color_table[color].green = g;
519             SP_PARM->_color_table[color].blue = b;
520         }
521
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);
525         result = OK;
526     }
527     returnCode(result);
528 }
529
530 #if NCURSES_SP_FUNCS
531 NCURSES_EXPORT(int)
532 init_color(short color, short r, short g, short b)
533 {
534     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
535 }
536 #endif
537
538 NCURSES_EXPORT(bool)
539 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
540 {
541     T((T_CALLED("can_change_color()")));
542     returnCode((can_change != 0) ? TRUE : FALSE);
543 }
544
545 #if NCURSES_SP_FUNCS
546 NCURSES_EXPORT(bool)
547 can_change_color(void)
548 {
549     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
550 }
551 #endif
552
553 NCURSES_EXPORT(bool)
554 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
555 {
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);
563 }
564
565 #if NCURSES_SP_FUNCS
566 NCURSES_EXPORT(bool)
567 has_colors(void)
568 {
569     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
570 }
571 #endif
572
573 NCURSES_EXPORT(int)
574 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
575                                 short *g, short *b)
576 {
577     int result;
578
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) {
581         result = ERR;
582     } else {
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;
586
587         if (r)
588             *r = c_r;
589         if (g)
590             *g = c_g;
591         if (b)
592             *b = c_b;
593
594         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
595                          color, c_r, c_g, c_b));
596         result = OK;
597     }
598     returnCode(result);
599 }
600
601 #if NCURSES_SP_FUNCS
602 NCURSES_EXPORT(int)
603 color_content(short color, short *r, short *g, short *b)
604 {
605     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
606 }
607 #endif
608
609 NCURSES_EXPORT(int)
610 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
611                                short *b)
612 {
613     int result;
614
615     T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
616
617     if (!ValidPair(pair)) {
618         result = ERR;
619     } else {
620         NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
621         NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
622
623 #if NCURSES_EXT_FUNCS
624         if (fg == COLOR_DEFAULT)
625             fg = -1;
626         if (bg == COLOR_DEFAULT)
627             bg = -1;
628 #endif
629
630         if (f)
631             *f = fg;
632         if (b)
633             *b = bg;
634
635         TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
636         result = OK;
637     }
638     returnCode(result);
639 }
640
641 #if NCURSES_SP_FUNCS
642 NCURSES_EXPORT(int)
643 pair_content(short pair, short *f, short *b)
644 {
645     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
646 }
647 #endif
648
649 NCURSES_EXPORT(void)
650 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
651 {
652     NCURSES_COLOR_T fg = COLOR_DEFAULT;
653     NCURSES_COLOR_T bg = COLOR_DEFAULT;
654     NCURSES_COLOR_T old_fg, old_bg;
655
656     if (!ValidPair(pair)) {
657         return;
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);
662             return;
663         } else if (SP != 0) {
664             pair_content((short) pair, &fg, &bg);
665         }
666     }
667
668     if (old_pair >= 0
669         && SP != 0
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
674             /*
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.
678              */
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);
687             } else
688 #endif
689                 reset_color_pair();
690         }
691     } else {
692         reset_color_pair();
693         if (old_pair < 0)
694             return;
695     }
696
697 #if NCURSES_EXT_FUNCS
698     if (isDefaultColor(fg))
699         fg = default_fg();
700     if (isDefaultColor(bg))
701         bg = default_bg();
702 #endif
703
704     if (reverse) {
705         NCURSES_COLOR_T xx = fg;
706         fg = bg;
707         bg = xx;
708     }
709
710     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
711                      fg, bg));
712
713     if (!isDefaultColor(fg)) {
714         set_foreground_color(fg, outc);
715     }
716     if (!isDefaultColor(bg)) {
717         set_background_color(bg, outc);
718     }
719 }