]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 5.7 - patch 20090815
[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.92 2009/06/06 20:26:16 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_PARM->_color_table[n] = tp[n];
203         } else {
204             SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
205             if (hue_lightness_saturation) {
206                 SP_PARM->_color_table[n].green = 100;
207             } else {
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;
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 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0) {
242     int result = FALSE;
243
244     T((T_CALLED("_nc_reset_colors()")));
245     if (SP_PARM->_color_defs > 0)
246         SP_PARM->_color_defs = -(SP_PARM->_color_defs);
247
248     if (reset_color_pair(NCURSES_SP_ARG))
249         result = TRUE;
250     if (orig_colors != 0) {
251         TPUTS_TRACE("orig_colors");
252         putp(orig_colors);
253         result = TRUE;
254     }
255     returnBool(result);
256 }
257
258 #if NCURSES_SP_FUNCS
259 bool
260 _nc_reset_colors(void)
261 {
262     return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
263 }
264 #endif
265
266 NCURSES_EXPORT(int)
267 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
268 {
269     int result = ERR;
270
271     T((T_CALLED("start_color()")));
272
273     if (SP_PARM == 0) {
274         result = ERR;
275     } else if (SP_PARM->_coloron) {
276         result = OK;
277     } else {
278
279         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
280             set_foreground_color(NCURSES_SP_ARGx
281                                  default_fg(),
282                                  NCURSES_SP_NAME(_nc_outch));
283             set_background_color(NCURSES_SP_ARGx
284                                  default_bg(),
285                                  NCURSES_SP_NAME(_nc_outch));
286         }
287
288         if (max_pairs > 0 && max_colors > 0) {
289             SP_PARM->_pair_limit = max_pairs;
290
291 #if NCURSES_EXT_FUNCS
292             /*
293              * If using default colors, allocate extra space in table to
294              * allow for default-color as a component of a color-pair.
295              */
296             SP_PARM->_pair_limit += (1 + (2 * max_colors));
297 #endif
298             SP_PARM->_pair_count = max_pairs;
299             SP_PARM->_color_count = max_colors;
300 #if !USE_REENTRANT
301             COLOR_PAIRS = max_pairs;
302             COLORS = max_colors;
303 #endif
304
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(),
311                                                        default_bg());
312                     init_color_table(NCURSES_SP_ARG);
313
314                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
315                        COLORS, COLOR_PAIRS));
316
317                     SP_PARM->_coloron = 1;
318                     result = OK;
319                 } else if (SP_PARM->_color_pairs != 0) {
320                     FreeAndNull(SP_PARM->_color_pairs);
321                 }
322             }
323         } else {
324             result = OK;
325         }
326     }
327     returnCode(result);
328 }
329
330 #if NCURSES_SP_FUNCS
331 NCURSES_EXPORT(int)
332 start_color(void)
333 {
334     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
335 }
336 #endif
337
338 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
339 static void
340 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
341 /* convert RGB to HLS system */
342 {
343     short min, max, t;
344
345     if ((min = g < r ? g : r) > b)
346         min = b;
347     if ((max = g > r ? g : r) < b)
348         max = b;
349
350     /* calculate lightness */
351     *l = (min + max) / 20;
352
353     if (min == max) {           /* black, white and all shades of gray */
354         *h = 0;
355         *s = 0;
356         return;
357     }
358
359     /* calculate saturation */
360     if (*l < 50)
361         *s = ((max - min) * 100) / (max + min);
362     else
363         *s = ((max - min) * 100) / (2000 - max - min);
364
365     /* calculate hue */
366     if (r == max)
367         t = 120 + ((g - b) * 60) / (max - min);
368     else if (g == max)
369         t = 240 + ((b - r) * 60) / (max - min);
370     else
371         t = 360 + ((r - g) * 60) / (max - min);
372
373     *h = t % 360;
374 }
375
376 /*
377  * Extension (1997/1/18) - Allow negative f/b values to set default color
378  * values.
379  */
380 NCURSES_EXPORT(int)
381 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
382 {
383     colorpair_t result;
384     colorpair_t previous;
385
386     T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
387
388     if (!ValidPair(pair))
389         returnCode(ERR);
390
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;
397
398         /*
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[]
401          * table.
402          */
403         if (isDefaultColor(f)) {
404             f = COLOR_DEFAULT;
405             isDefault = TRUE;
406         } else if (!OkColorHi(f)) {
407             returnCode(ERR);
408         }
409
410         if (isDefaultColor(b)) {
411             b = COLOR_DEFAULT;
412             isDefault = TRUE;
413         } else if (!OkColorHi(b)) {
414             returnCode(ERR);
415         }
416
417         /*
418          * Check if the table entry that we are going to init/update used
419          * default colors.
420          */
421         if ((FORE_OF(previous) == COLOR_DEFAULT)
422             || (BACK_OF(previous) == COLOR_DEFAULT))
423             wasDefault = TRUE;
424
425         /*
426          * Keep track of the number of entries in the color pair table which
427          * used a default color.
428          */
429         if (isDefault && !wasDefault) {
430             ++default_pairs;
431         } else if (wasDefault && !isDefault) {
432             --default_pairs;
433         }
434
435         /*
436          * As an extension, ncurses allows the pair number to exceed the
437          * terminal's color_pairs value for pairs using a default color.
438          *
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.
442          */
443         if (pair > (SP_PARM->_pair_count + default_pairs)) {
444             returnCode(ERR);
445         }
446         SP_PARM->_default_pairs = default_pairs;
447     } else
448 #endif
449     {
450         if ((f < 0) || !OkColorHi(f)
451             || (b < 0) || !OkColorHi(b)
452             || (pair < 1))
453             returnCode(ERR);
454     }
455
456     /*
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).
460      */
461     result = PAIR_OF(f, b);
462     if (previous != 0
463         && previous != result) {
464         int y, x;
465
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);
475                     changed = TRUE;
476                 }
477             }
478             if (changed)
479                 _nc_make_oldhash(y);
480         }
481     }
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 */
485
486     if (initialize_pair && InPalette(f) && InPalette(b)) {
487         const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
488
489         TR(TRACE_ATTRS,
490            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
491             pair,
492             tp[f].red, tp[f].green, tp[f].blue,
493             tp[b].red, tp[b].green, tp[b].blue));
494
495         TPUTS_TRACE("initialize_pair");
496         putp(TPARM_7(initialize_pair,
497                      pair,
498                      tp[f].red, tp[f].green, tp[f].blue,
499                      tp[b].red, tp[b].green, tp[b].blue));
500     }
501
502     returnCode(OK);
503 }
504
505 #if NCURSES_SP_FUNCS
506 NCURSES_EXPORT(int)
507 init_pair(short pair, short f, short b)
508 {
509     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
510 }
511 #endif
512
513 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
514
515 NCURSES_EXPORT(int)
516 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
517                              short color, short r, short g, short b)
518 {
519     int result = ERR;
520
521     T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
522
523     if (initialize_color != NULL
524         && SP_PARM != 0
525         && SP_PARM->_coloron
526         && (color >= 0 && OkColorHi(color))
527         && (okRGB(r) && okRGB(g) && okRGB(b))) {
528
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;
533
534         if (hue_lightness_saturation) {
535             rgb2hls(r, g, b,
536                     &SP_PARM->_color_table[color].red,
537                     &SP_PARM->_color_table[color].green,
538                     &SP_PARM->_color_table[color].blue);
539         } else {
540             SP_PARM->_color_table[color].red = r;
541             SP_PARM->_color_table[color].green = g;
542             SP_PARM->_color_table[color].blue = b;
543         }
544
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);
548         result = OK;
549     }
550     returnCode(result);
551 }
552
553 #if NCURSES_SP_FUNCS
554 NCURSES_EXPORT(int)
555 init_color(short color, short r, short g, short b)
556 {
557     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
558 }
559 #endif
560
561 NCURSES_EXPORT(bool)
562 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
563 {
564     T((T_CALLED("can_change_color()")));
565     returnCode((can_change != 0) ? TRUE : FALSE);
566 }
567
568 #if NCURSES_SP_FUNCS
569 NCURSES_EXPORT(bool)
570 can_change_color(void)
571 {
572     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
573 }
574 #endif
575
576 NCURSES_EXPORT(bool)
577 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
578 {
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);
586 }
587
588 #if NCURSES_SP_FUNCS
589 NCURSES_EXPORT(bool)
590 has_colors(void)
591 {
592     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
593 }
594 #endif
595
596 NCURSES_EXPORT(int)
597 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
598                                 short *g, short *b)
599 {
600     int result;
601
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) {
604         result = ERR;
605     } else {
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;
609
610         if (r)
611             *r = c_r;
612         if (g)
613             *g = c_g;
614         if (b)
615             *b = c_b;
616
617         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
618                          color, c_r, c_g, c_b));
619         result = OK;
620     }
621     returnCode(result);
622 }
623
624 #if NCURSES_SP_FUNCS
625 NCURSES_EXPORT(int)
626 color_content(short color, short *r, short *g, short *b)
627 {
628     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
629 }
630 #endif
631
632 NCURSES_EXPORT(int)
633 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
634                                short *b)
635 {
636     int result;
637
638     T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
639
640     if (!ValidPair(pair)) {
641         result = ERR;
642     } else {
643         NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
644         NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
645
646 #if NCURSES_EXT_FUNCS
647         if (fg == COLOR_DEFAULT)
648             fg = -1;
649         if (bg == COLOR_DEFAULT)
650             bg = -1;
651 #endif
652
653         if (f)
654             *f = fg;
655         if (b)
656             *b = bg;
657
658         TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
659         result = OK;
660     }
661     returnCode(result);
662 }
663
664 #if NCURSES_SP_FUNCS
665 NCURSES_EXPORT(int)
666 pair_content(short pair, short *f, short *b)
667 {
668     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
669 }
670 #endif
671
672 NCURSES_EXPORT(void)
673 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
674                                short old_pair,
675                                short pair,
676                                bool reverse,
677                                NCURSES_SP_OUTC outc)
678 {
679     NCURSES_COLOR_T fg = COLOR_DEFAULT;
680     NCURSES_COLOR_T bg = COLOR_DEFAULT;
681     NCURSES_COLOR_T old_fg, old_bg;
682
683     if (!ValidPair(pair)) {
684         return;
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),
690                                     1, outc);
691             return;
692         } else if (SP_PARM != 0) {
693             pair_content((short) pair, &fg, &bg);
694         }
695     }
696
697     if (old_pair >= 0
698         && SP_PARM != 0
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
703             /*
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.
707              */
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);
716             } else
717 #endif
718                 reset_color_pair(NCURSES_SP_ARG);
719         }
720     } else {
721         reset_color_pair(NCURSES_SP_ARG);
722         if (old_pair < 0)
723             return;
724     }
725
726 #if NCURSES_EXT_FUNCS
727     if (isDefaultColor(fg))
728         fg = default_fg();
729     if (isDefaultColor(bg))
730         bg = default_bg();
731 #endif
732
733     if (reverse) {
734         NCURSES_COLOR_T xx = fg;
735         fg = bg;
736         bg = xx;
737     }
738
739     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
740                      fg, bg));
741
742     if (!isDefaultColor(fg)) {
743         set_foreground_color(NCURSES_SP_ARGx fg, outc);
744     }
745     if (!isDefaultColor(bg)) {
746         set_background_color(NCURSES_SP_ARGx bg, outc);
747     }
748 }
749
750 #if NCURSES_SP_FUNCS
751 NCURSES_EXPORT(void)
752 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
753 {
754     SetSafeOutcWrapper(outc);
755     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
756                                    old_pair,
757                                    pair,
758                                    reverse,
759                                    _nc_outc_wrapper);
760 }
761 #endif