]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 5.7 - patch 20091031
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,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 #include <tic.h>
43
44 #ifndef CUR
45 #define CUR SP_TERMTYPE
46 #endif
47
48 MODULE_ID("$Id: lib_color.c,v 1.96 2009/10/24 22:50:51 tom Exp $")
49
50 #ifdef USE_TERM_DRIVER
51 #define CanChange      InfoOf(SP_PARM).canchange
52 #define DefaultPalette InfoOf(SP_PARM).defaultPalette
53 #define HasColor       InfoOf(SP_PARM).hascolor
54 #define InitColor      InfoOf(SP_PARM).initcolor
55 #define MaxColors      InfoOf(SP_PARM).maxcolors
56 #define MaxPairs       InfoOf(SP_PARM).maxpairs
57 #define UseHlsPalette  (DefaultPalette == _nc_hls_palette)
58 #else
59 #define CanChange      can_change
60 #define DefaultPalette (hue_lightness_saturation ? hls_palette : cga_palette)
61 #define HasColor       has_color
62 #define InitColor      initialize_color
63 #define MaxColors      max_colors
64 #define MaxPairs       max_pairs
65 #define UseHlsPalette  (hue_lightness_saturation)
66 #endif
67
68 #ifndef USE_TERM_DRIVER
69 /*
70  * These should be screen structure members.  They need to be globals for
71  * historical reasons.  So we assign them in start_color() and also in
72  * set_term()'s screen-switching logic.
73  */
74 #if USE_REENTRANT
75 NCURSES_EXPORT(int)
76 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
77 {
78     return SP ? SP->_pair_count : -1;
79 }
80 NCURSES_EXPORT(int)
81 NCURSES_PUBLIC_VAR(COLORS) (void)
82 {
83     return SP ? SP->_color_count : -1;
84 }
85 #else
86 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
87 NCURSES_EXPORT_VAR(int) COLORS = 0;
88 #endif
89 #endif /* !USE_TERM_DRIVER */
90
91 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
92
93 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
94
95 #define MAX_PALETTE     8
96
97 #define OkColorHi(n)    (((n) < COLORS) && ((n) < maxcolors))
98 #define InPalette(n)    ((n) >= 0 && (n) < MAX_PALETTE)
99
100 /*
101  * Given a RGB range of 0..1000, we'll normally set the individual values
102  * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
103  */
104 #define RGB_ON  680
105 #define RGB_OFF 0
106 /* *INDENT-OFF* */
107 static const color_t cga_palette[] =
108 {
109     /*  R               G               B */
110     DATA(RGB_OFF,       RGB_OFF,        RGB_OFF),       /* COLOR_BLACK */
111     DATA(RGB_ON,        RGB_OFF,        RGB_OFF),       /* COLOR_RED */
112     DATA(RGB_OFF,       RGB_ON,         RGB_OFF),       /* COLOR_GREEN */
113     DATA(RGB_ON,        RGB_ON,         RGB_OFF),       /* COLOR_YELLOW */
114     DATA(RGB_OFF,       RGB_OFF,        RGB_ON),        /* COLOR_BLUE */
115     DATA(RGB_ON,        RGB_OFF,        RGB_ON),        /* COLOR_MAGENTA */
116     DATA(RGB_OFF,       RGB_ON,         RGB_ON),        /* COLOR_CYAN */
117     DATA(RGB_ON,        RGB_ON,         RGB_ON),        /* COLOR_WHITE */
118 };
119
120 static const color_t hls_palette[] =
121 {
122     /*          H       L       S */
123     DATA(       0,      0,      0),             /* COLOR_BLACK */
124     DATA(       120,    50,     100),           /* COLOR_RED */
125     DATA(       240,    50,     100),           /* COLOR_GREEN */
126     DATA(       180,    50,     100),           /* COLOR_YELLOW */
127     DATA(       330,    50,     100),           /* COLOR_BLUE */
128     DATA(       60,     50,     100),           /* COLOR_MAGENTA */
129     DATA(       300,    50,     100),           /* COLOR_CYAN */
130     DATA(       0,      50,     100),           /* COLOR_WHITE */
131 };
132
133 #ifdef USE_TERM_DRIVER
134 NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette = cga_palette;
135 NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
136 #endif
137
138 /* *INDENT-ON* */
139
140 /*
141  * Ensure that we use color pairs only when colors have been started, and also
142  * that the index is within the limits of the table which we allocated.
143  */
144 #define ValidPair(pair) \
145     ((SP_PARM != 0) && (pair >= 0) && (pair < SP_PARM->_pair_limit) && SP_PARM->_coloron)
146
147 #if NCURSES_EXT_FUNCS
148 /*
149  * These are called from _nc_do_color(), which in turn is called from
150  * vidattr - so we have to assume that sp may be null.
151  */
152 static int
153 default_fg(NCURSES_SP_DCL0)
154 {
155     return (SP_PARM != 0) ? SP_PARM->_default_fg : COLOR_WHITE;
156 }
157
158 static int
159 default_bg(NCURSES_SP_DCL0)
160 {
161     return SP_PARM != 0 ? SP_PARM->_default_bg : COLOR_BLACK;
162 }
163 #else
164 #define default_fg(sp) COLOR_WHITE
165 #define default_bg(sp) COLOR_BLACK
166 #endif
167
168 #ifndef USE_TERM_DRIVER
169 /*
170  * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
171  * to maintain compatibility with a pre-ANSI scheme.  The same scheme is
172  * also used in the FreeBSD syscons.
173  */
174 static int
175 toggled_colors(int c)
176 {
177     if (c < 16) {
178         static const int table[] =
179         {0, 4, 2, 6, 1, 5, 3, 7,
180          8, 12, 10, 14, 9, 13, 11, 15};
181         c = table[c];
182     }
183     return c;
184 }
185 #endif
186
187 static void
188 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
189 {
190 #ifdef USE_TERM_DRIVER
191     CallDriver_3(SP_PARM, color, FALSE, bg, outc);
192 #else
193     if (set_a_background) {
194         TPUTS_TRACE("set_a_background");
195         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
196                                 TPARM_1(set_a_background, bg),
197                                 1, outc);
198     } else {
199         TPUTS_TRACE("set_background");
200         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
201                                 TPARM_1(set_background, toggled_colors(bg)),
202                                 1, outc);
203     }
204 #endif
205 }
206
207 static void
208 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
209 {
210 #ifdef USE_TERM_DRIVER
211     CallDriver_3(SP_PARM, color, TRUE, fg, outc);
212 #else
213     if (set_a_foreground) {
214         TPUTS_TRACE("set_a_foreground");
215         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
216                                 TPARM_1(set_a_foreground, fg),
217                                 1, outc);
218     } else {
219         TPUTS_TRACE("set_foreground");
220         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
221                                 TPARM_1(set_foreground, toggled_colors(fg)),
222                                 1, outc);
223     }
224 #endif
225 }
226
227 static void
228 init_color_table(NCURSES_SP_DCL0)
229 {
230     const color_t *tp = DefaultPalette;
231     int n;
232
233     assert(tp != 0);
234
235     for (n = 0; n < COLORS; n++) {
236         if (InPalette(n)) {
237             SP_PARM->_color_table[n] = tp[n];
238         } else {
239             SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
240             if (UseHlsPalette) {
241                 SP_PARM->_color_table[n].green = 100;
242             } else {
243                 if (SP_PARM->_color_table[n].red)
244                     SP_PARM->_color_table[n].red = 1000;
245                 if (SP_PARM->_color_table[n].green)
246                     SP_PARM->_color_table[n].green = 1000;
247                 if (SP_PARM->_color_table[n].blue)
248                     SP_PARM->_color_table[n].blue = 1000;
249             }
250         }
251     }
252 }
253
254 /*
255  * Reset the color pair, e.g., to whatever color pair 0 is.
256  */
257 static bool
258 reset_color_pair(NCURSES_SP_DCL0)
259 {
260 #ifdef USE_TERM_DRIVER
261     return CallDriver(SP_PARM, rescol);
262 #else
263     bool result = FALSE;
264
265     if (orig_pair != 0) {
266         TPUTS_TRACE("orig_pair");
267         putp(orig_pair);
268         result = TRUE;
269     }
270     return result;
271 #endif
272 }
273
274 /*
275  * Reset color pairs and definitions.  Actually we do both more to accommodate
276  * badly-written terminal descriptions than for the relatively rare case where
277  * someone has changed the color definitions.
278  */
279 NCURSES_EXPORT(bool)
280 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
281 {
282     int result = FALSE;
283
284     T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
285     if (SP_PARM->_color_defs > 0)
286         SP_PARM->_color_defs = -(SP_PARM->_color_defs);
287     if (reset_color_pair(NCURSES_SP_ARG))
288         result = TRUE;
289
290 #ifdef USE_TERM_DRIVER
291     result = CallDriver(SP_PARM, rescolors);
292 #else
293     if (orig_colors != 0) {
294         TPUTS_TRACE("orig_colors");
295         putp(orig_colors);
296         result = TRUE;
297     }
298 #endif
299     returnBool(result);
300 }
301
302 #if NCURSES_SP_FUNCS
303 NCURSES_EXPORT(bool)
304 _nc_reset_colors(void)
305 {
306     return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
307 }
308 #endif
309
310 NCURSES_EXPORT(int)
311 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
312 {
313     int result = ERR;
314     int maxpairs = 0, maxcolors = 0;
315
316     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
317
318     if (SP_PARM == 0) {
319         result = ERR;
320     } else if (SP_PARM->_coloron) {
321         result = OK;
322     } else {
323         maxpairs = MaxPairs;
324         maxcolors = MaxColors;
325         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
326             set_foreground_color(NCURSES_SP_ARGx
327                                  default_fg(NCURSES_SP_ARG),
328                                  NCURSES_SP_NAME(_nc_outch));
329             set_background_color(NCURSES_SP_ARGx
330                                  default_bg(NCURSES_SP_ARG),
331                                  NCURSES_SP_NAME(_nc_outch));
332         }
333
334         if (maxpairs > 0 && maxcolors > 0) {
335             SP_PARM->_pair_limit = maxpairs;
336
337 #if NCURSES_EXT_FUNCS
338             /*
339              * If using default colors, allocate extra space in table to
340              * allow for default-color as a component of a color-pair.
341              */
342             SP_PARM->_pair_limit += (1 + (2 * maxcolors));
343 #endif
344             SP_PARM->_pair_count = maxpairs;
345             SP_PARM->_color_count = maxcolors;
346 #if !USE_REENTRANT
347             COLOR_PAIRS = maxpairs;
348             COLORS = maxcolors;
349 #endif
350
351             SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t, SP_PARM->_pair_limit);
352             if (SP_PARM->_color_pairs != 0) {
353                 SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
354                 if (SP_PARM->_color_table != 0) {
355                     SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(NCURSES_SP_ARG),
356                                                        default_bg(NCURSES_SP_ARG));
357                     init_color_table(NCURSES_SP_ARG);
358
359                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
360                        COLORS, COLOR_PAIRS));
361
362                     SP_PARM->_coloron = 1;
363                     result = OK;
364                 } else if (SP_PARM->_color_pairs != 0) {
365                     FreeAndNull(SP_PARM->_color_pairs);
366                 }
367             }
368         } else {
369             result = OK;
370         }
371     }
372     returnCode(result);
373 }
374
375 #if NCURSES_SP_FUNCS
376 NCURSES_EXPORT(int)
377 start_color(void)
378 {
379     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
380 }
381 #endif
382
383 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
384 static void
385 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
386 /* convert RGB to HLS system */
387 {
388     short min, max, t;
389
390     if ((min = g < r ? g : r) > b)
391         min = b;
392     if ((max = g > r ? g : r) < b)
393         max = b;
394
395     /* calculate lightness */
396     *l = (min + max) / 20;
397
398     if (min == max) {           /* black, white and all shades of gray */
399         *h = 0;
400         *s = 0;
401         return;
402     }
403
404     /* calculate saturation */
405     if (*l < 50)
406         *s = ((max - min) * 100) / (max + min);
407     else
408         *s = ((max - min) * 100) / (2000 - max - min);
409
410     /* calculate hue */
411     if (r == max)
412         t = 120 + ((g - b) * 60) / (max - min);
413     else if (g == max)
414         t = 240 + ((b - r) * 60) / (max - min);
415     else
416         t = 360 + ((r - g) * 60) / (max - min);
417
418     *h = t % 360;
419 }
420
421 /*
422  * Extension (1997/1/18) - Allow negative f/b values to set default color
423  * values.
424  */
425 NCURSES_EXPORT(int)
426 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
427 {
428     colorpair_t result;
429     colorpair_t previous;
430     int maxcolors;
431
432     T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) SP_PARM, pair, f, b));
433
434     if (!ValidPair(pair))
435         returnCode(ERR);
436
437     maxcolors = MaxColors;
438
439     previous = SP_PARM->_color_pairs[pair];
440 #if NCURSES_EXT_FUNCS
441     if (SP_PARM->_default_color) {
442         bool isDefault = FALSE;
443         bool wasDefault = FALSE;
444         int default_pairs = SP_PARM->_default_pairs;
445
446         /*
447          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
448          * internal unsigned values which we will store in the _color_pairs[]
449          * table.
450          */
451         if (isDefaultColor(f)) {
452             f = COLOR_DEFAULT;
453             isDefault = TRUE;
454         } else if (!OkColorHi(f)) {
455             returnCode(ERR);
456         }
457
458         if (isDefaultColor(b)) {
459             b = COLOR_DEFAULT;
460             isDefault = TRUE;
461         } else if (!OkColorHi(b)) {
462             returnCode(ERR);
463         }
464
465         /*
466          * Check if the table entry that we are going to init/update used
467          * default colors.
468          */
469         if ((FORE_OF(previous) == COLOR_DEFAULT)
470             || (BACK_OF(previous) == COLOR_DEFAULT))
471             wasDefault = TRUE;
472
473         /*
474          * Keep track of the number of entries in the color pair table which
475          * used a default color.
476          */
477         if (isDefault && !wasDefault) {
478             ++default_pairs;
479         } else if (wasDefault && !isDefault) {
480             --default_pairs;
481         }
482
483         /*
484          * As an extension, ncurses allows the pair number to exceed the
485          * terminal's color_pairs value for pairs using a default color.
486          *
487          * Note that updating a pair which used a default color with one
488          * that does not will decrement the count - and possibly interfere
489          * with sequentially adding new pairs.
490          */
491         if (pair > (SP_PARM->_pair_count + default_pairs)) {
492             returnCode(ERR);
493         }
494         SP_PARM->_default_pairs = default_pairs;
495     } else
496 #endif
497     {
498         if ((f < 0) || !OkColorHi(f)
499             || (b < 0) || !OkColorHi(b)
500             || (pair < 1))
501             returnCode(ERR);
502     }
503
504     /*
505      * When a pair's content is changed, replace its colors (if pair was
506      * initialized before a screen update is performed replacing original
507      * pair colors with the new ones).
508      */
509     result = PAIR_OF(f, b);
510     if (previous != 0
511         && previous != result) {
512         int y, x;
513
514         for (y = 0; y <= CurScreen(SP_PARM)->_maxy; y++) {
515             struct ldat *ptr = &(CurScreen(SP_PARM)->_line[y]);
516             bool changed = FALSE;
517             for (x = 0; x <= CurScreen(SP_PARM)->_maxx; x++) {
518                 if (GetPair(ptr->text[x]) == pair) {
519                     /* Set the old cell to zero to ensure it will be
520                        updated on the next doupdate() */
521                     SetChar(ptr->text[x], 0, 0);
522                     CHANGED_CELL(ptr, x);
523                     changed = TRUE;
524                 }
525             }
526             if (changed)
527                 NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
528         }
529     }
530
531     SP_PARM->_color_pairs[pair] = result;
532     if (GET_SCREEN_PAIR(SP_PARM) == pair)
533         SET_SCREEN_PAIR(SP_PARM, (chtype) (~0));        /* force attribute update */
534
535 #ifdef USE_TERM_DRIVER
536     CallDriver_3(SP_PARM, initpair, pair, f, b);
537 #else
538     if (initialize_pair && InPalette(f) && InPalette(b)) {
539         const color_t *tp = DefaultPalette;
540
541         TR(TRACE_ATTRS,
542            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
543             pair,
544             tp[f].red, tp[f].green, tp[f].blue,
545             tp[b].red, tp[b].green, tp[b].blue));
546
547         TPUTS_TRACE("initialize_pair");
548         putp(TPARM_7(initialize_pair,
549                      pair,
550                      tp[f].red, tp[f].green, tp[f].blue,
551                      tp[b].red, tp[b].green, tp[b].blue));
552     }
553 #endif
554
555     returnCode(OK);
556 }
557
558 #if NCURSES_SP_FUNCS
559 NCURSES_EXPORT(int)
560 init_pair(short pair, short f, short b)
561 {
562     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
563 }
564 #endif
565
566 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
567
568 NCURSES_EXPORT(int)
569 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
570                              short color, short r, short g, short b)
571 {
572     int result = ERR;
573     int maxcolors;
574
575     T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
576        (void *) SP_PARM,
577        color,
578        r, g, b));
579
580     if (SP_PARM == 0)
581         returnCode(result);
582
583     maxcolors = MaxColors;
584
585     if (InitColor
586         && SP_PARM->_coloron
587         && (color >= 0 && OkColorHi(color))
588         && (okRGB(r) && okRGB(g) && okRGB(b))) {
589
590         SP_PARM->_color_table[color].init = 1;
591         SP_PARM->_color_table[color].r = r;
592         SP_PARM->_color_table[color].g = g;
593         SP_PARM->_color_table[color].b = b;
594
595         if (UseHlsPalette) {
596             rgb2hls(r, g, b,
597                     &SP_PARM->_color_table[color].red,
598                     &SP_PARM->_color_table[color].green,
599                     &SP_PARM->_color_table[color].blue);
600         } else {
601             SP_PARM->_color_table[color].red = r;
602             SP_PARM->_color_table[color].green = g;
603             SP_PARM->_color_table[color].blue = b;
604         }
605
606 #ifdef USE_TERM_DRIVER
607         CallDriver_4(SP_PARM, initcolor, color, r, g, b);
608 #else
609         TPUTS_TRACE("initialize_color");
610         putp(TPARM_4(initialize_color, color, r, g, b));
611 #endif
612         SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
613
614         result = OK;
615     }
616     returnCode(result);
617 }
618
619 #if NCURSES_SP_FUNCS
620 NCURSES_EXPORT(int)
621 init_color(short color, short r, short g, short b)
622 {
623     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
624 }
625 #endif
626
627 NCURSES_EXPORT(bool)
628 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
629 {
630     T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
631     returnCode((CanChange != 0) ? TRUE : FALSE);
632 }
633
634 #if NCURSES_SP_FUNCS
635 NCURSES_EXPORT(bool)
636 can_change_color(void)
637 {
638     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
639 }
640 #endif
641
642 NCURSES_EXPORT(bool)
643 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
644 {
645     int code;
646
647     T((T_CALLED("has_colors()")));
648 #ifdef USE_TERM_DRIVER
649     code = HasColor;
650 #else
651     code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
652              && (((set_foreground != NULL)
653                   && (set_background != NULL))
654                  || ((set_a_foreground != NULL)
655                      && (set_a_background != NULL))
656                  || set_color_pair)) ? TRUE : FALSE);
657 #endif
658     returnCode(code);
659 }
660
661 #if NCURSES_SP_FUNCS
662 NCURSES_EXPORT(bool)
663 has_colors(void)
664 {
665     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
666 }
667 #endif
668
669 NCURSES_EXPORT(int)
670 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
671                                 short color, short *r, short *g, short *b)
672 {
673     int result = ERR;
674     int maxcolors;
675
676     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
677        (void *) SP_PARM,
678        color,
679        (void *) r,
680        (void *) g,
681        (void *) b));
682
683     if (SP_PARM == 0)
684         returnCode(result);
685
686     maxcolors = MaxColors;
687
688     if (color < 0 || !OkColorHi(color) || !SP_PARM->_coloron) {
689         result = ERR;
690     } else {
691         NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
692         NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
693         NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
694
695         if (r)
696             *r = c_r;
697         if (g)
698             *g = c_g;
699         if (b)
700             *b = c_b;
701
702         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
703                          color, c_r, c_g, c_b));
704         result = OK;
705     }
706     returnCode(result);
707 }
708
709 #if NCURSES_SP_FUNCS
710 NCURSES_EXPORT(int)
711 color_content(short color, short *r, short *g, short *b)
712 {
713     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
714 }
715 #endif
716
717 NCURSES_EXPORT(int)
718 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
719                                short pair, short *f, short *b)
720 {
721     int result;
722
723     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
724        (void *) SP_PARM,
725        pair,
726        (void *) f,
727        (void *) b));
728
729     if (!ValidPair(pair)) {
730         result = ERR;
731     } else {
732         NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
733         NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
734
735 #if NCURSES_EXT_FUNCS
736         if (fg == COLOR_DEFAULT)
737             fg = -1;
738         if (bg == COLOR_DEFAULT)
739             bg = -1;
740 #endif
741
742         if (f)
743             *f = fg;
744         if (b)
745             *b = bg;
746
747         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
748                          (void *) SP_PARM,
749                          pair,
750                          fg, bg));
751         result = OK;
752     }
753     returnCode(result);
754 }
755
756 #if NCURSES_SP_FUNCS
757 NCURSES_EXPORT(int)
758 pair_content(short pair, short *f, short *b)
759 {
760     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
761 }
762 #endif
763
764 NCURSES_EXPORT(void)
765 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
766                                short old_pair,
767                                short pair,
768                                bool reverse,
769                                NCURSES_SP_OUTC outc)
770 {
771 #ifdef USE_TERM_DRIVER
772     CallDriver_4(SP_PARM, docolor, old_pair, pair, reverse, outc);
773 #else
774     NCURSES_COLOR_T fg = COLOR_DEFAULT;
775     NCURSES_COLOR_T bg = COLOR_DEFAULT;
776     NCURSES_COLOR_T old_fg, old_bg;
777
778     if (!ValidPair(pair)) {
779         return;
780     } else if (pair != 0) {
781         if (set_color_pair) {
782             TPUTS_TRACE("set_color_pair");
783             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
784                                     TPARM_1(set_color_pair, pair),
785                                     1, outc);
786             return;
787         } else if (SP_PARM != 0) {
788             pair_content((short) pair, &fg, &bg);
789         }
790     }
791
792     if (old_pair >= 0
793         && SP_PARM != 0
794         && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
795         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
796             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
797 #if NCURSES_EXT_FUNCS
798             /*
799              * A minor optimization - but extension.  If "AX" is specified in
800              * the terminal description, treat it as screen's indicator of ECMA
801              * SGR 39 and SGR 49, and assume the two sequences are independent.
802              */
803             if (SP_PARM->_has_sgr_39_49
804                 && isDefaultColor(old_bg)
805                 && !isDefaultColor(old_fg)) {
806                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
807             } else if (SP_PARM->_has_sgr_39_49
808                        && isDefaultColor(old_fg)
809                        && !isDefaultColor(old_bg)) {
810                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
811             } else
812 #endif
813                 reset_color_pair(NCURSES_SP_ARG);
814         }
815     } else {
816         reset_color_pair(NCURSES_SP_ARG);
817         if (old_pair < 0)
818             return;
819     }
820
821 #if NCURSES_EXT_FUNCS
822     if (isDefaultColor(fg))
823         fg = default_fg(NCURSES_SP_ARG);
824     if (isDefaultColor(bg))
825         bg = default_bg(NCURSES_SP_ARG);
826 #endif
827
828     if (reverse) {
829         NCURSES_COLOR_T xx = fg;
830         fg = bg;
831         bg = xx;
832     }
833
834     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
835                      fg, bg));
836
837     if (!isDefaultColor(fg)) {
838         set_foreground_color(NCURSES_SP_ARGx fg, outc);
839     }
840     if (!isDefaultColor(bg)) {
841         set_background_color(NCURSES_SP_ARGx bg, outc);
842     }
843 #endif
844 }
845
846 #if NCURSES_SP_FUNCS
847 NCURSES_EXPORT(void)
848 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
849 {
850     SetSafeOutcWrapper(outc);
851     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
852                                    old_pair,
853                                    pair,
854                                    reverse,
855                                    _nc_outc_wrapper);
856 }
857 #endif