]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
50d580ec72266561ee4330c020deda58b75e1eae
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2011 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.101 2011/05/28 21:57:59 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     (void) SP_PARM;
266     if (orig_pair != 0) {
267         TPUTS_TRACE("orig_pair");
268         putp(orig_pair);
269         result = TRUE;
270     }
271     return result;
272 #endif
273 }
274
275 /*
276  * Reset color pairs and definitions.  Actually we do both more to accommodate
277  * badly-written terminal descriptions than for the relatively rare case where
278  * someone has changed the color definitions.
279  */
280 NCURSES_EXPORT(bool)
281 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
282 {
283     int result = FALSE;
284
285     T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
286     if (SP_PARM->_color_defs > 0)
287         SP_PARM->_color_defs = -(SP_PARM->_color_defs);
288     if (reset_color_pair(NCURSES_SP_ARG))
289         result = TRUE;
290
291 #ifdef USE_TERM_DRIVER
292     result = CallDriver(SP_PARM, rescolors);
293 #else
294     if (orig_colors != 0) {
295         TPUTS_TRACE("orig_colors");
296         putp(orig_colors);
297         result = TRUE;
298     }
299 #endif
300     returnBool(result);
301 }
302
303 #if NCURSES_SP_FUNCS
304 NCURSES_EXPORT(bool)
305 _nc_reset_colors(void)
306 {
307     return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
308 }
309 #endif
310
311 NCURSES_EXPORT(int)
312 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
313 {
314     int result = ERR;
315     int maxpairs = 0, maxcolors = 0;
316
317     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
318
319     if (SP_PARM == 0) {
320         result = ERR;
321     } else if (SP_PARM->_coloron) {
322         result = OK;
323     } else {
324         maxpairs = MaxPairs;
325         maxcolors = MaxColors;
326         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
327             set_foreground_color(NCURSES_SP_ARGx
328                                  default_fg(NCURSES_SP_ARG),
329                                  NCURSES_SP_NAME(_nc_outch));
330             set_background_color(NCURSES_SP_ARGx
331                                  default_bg(NCURSES_SP_ARG),
332                                  NCURSES_SP_NAME(_nc_outch));
333         }
334
335         if (maxpairs > 0 && maxcolors > 0) {
336             SP_PARM->_pair_limit = maxpairs;
337
338 #if NCURSES_EXT_FUNCS
339             /*
340              * If using default colors, allocate extra space in table to
341              * allow for default-color as a component of a color-pair.
342              */
343             SP_PARM->_pair_limit += (1 + (2 * maxcolors));
344 #endif
345             SP_PARM->_pair_count = maxpairs;
346             SP_PARM->_color_count = maxcolors;
347 #if !USE_REENTRANT
348             COLOR_PAIRS = maxpairs;
349             COLORS = maxcolors;
350 #endif
351
352             SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t, SP_PARM->_pair_limit);
353             if (SP_PARM->_color_pairs != 0) {
354                 SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
355                 if (SP_PARM->_color_table != 0) {
356                     SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(NCURSES_SP_ARG),
357                                                        default_bg(NCURSES_SP_ARG));
358                     init_color_table(NCURSES_SP_ARG);
359
360                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
361                        COLORS, COLOR_PAIRS));
362
363                     SP_PARM->_coloron = 1;
364                     result = OK;
365                 } else if (SP_PARM->_color_pairs != 0) {
366                     FreeAndNull(SP_PARM->_color_pairs);
367                 }
368             }
369         } else {
370             result = OK;
371         }
372     }
373     returnCode(result);
374 }
375
376 #if NCURSES_SP_FUNCS
377 NCURSES_EXPORT(int)
378 start_color(void)
379 {
380     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
381 }
382 #endif
383
384 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
385 static void
386 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
387 /* convert RGB to HLS system */
388 {
389     short min, max, t;
390
391     if ((min = g < r ? g : r) > b)
392         min = b;
393     if ((max = g > r ? g : r) < b)
394         max = b;
395
396     /* calculate lightness */
397     *l = (short) ((min + max) / 20);
398
399     if (min == max) {           /* black, white and all shades of gray */
400         *h = 0;
401         *s = 0;
402         return;
403     }
404
405     /* calculate saturation */
406     if (*l < 50)
407         *s = (short) (((max - min) * 100) / (max + min));
408     else
409         *s = (short) (((max - min) * 100) / (2000 - max - min));
410
411     /* calculate hue */
412     if (r == max)
413         t = (short) (120 + ((g - b) * 60) / (max - min));
414     else if (g == max)
415         t = (short) (240 + ((b - r) * 60) / (max - min));
416     else
417         t = (short) (360 + ((r - g) * 60) / (max - min));
418
419     *h = t % 360;
420 }
421
422 /*
423  * Extension (1997/1/18) - Allow negative f/b values to set default color
424  * values.
425  */
426 NCURSES_EXPORT(int)
427 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
428 {
429     colorpair_t result;
430     colorpair_t previous;
431     int maxcolors;
432
433     T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) SP_PARM, pair, f, b));
434
435     if (!ValidPair(pair))
436         returnCode(ERR);
437
438     maxcolors = MaxColors;
439
440     previous = SP_PARM->_color_pairs[pair];
441 #if NCURSES_EXT_FUNCS
442     if (SP_PARM->_default_color || SP_PARM->_assumed_color) {
443         bool isDefault = FALSE;
444         bool wasDefault = FALSE;
445         int default_pairs = SP_PARM->_default_pairs;
446
447         /*
448          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
449          * internal unsigned values which we will store in the _color_pairs[]
450          * table.
451          */
452         if (isDefaultColor(f)) {
453             f = COLOR_DEFAULT;
454             isDefault = TRUE;
455         } else if (!OkColorHi(f)) {
456             returnCode(ERR);
457         }
458
459         if (isDefaultColor(b)) {
460             b = COLOR_DEFAULT;
461             isDefault = TRUE;
462         } else if (!OkColorHi(b)) {
463             returnCode(ERR);
464         }
465
466         /*
467          * Check if the table entry that we are going to init/update used
468          * default colors.
469          */
470         if ((FORE_OF(previous) == COLOR_DEFAULT)
471             || (BACK_OF(previous) == COLOR_DEFAULT))
472             wasDefault = TRUE;
473
474         /*
475          * Keep track of the number of entries in the color pair table which
476          * used a default color.
477          */
478         if (isDefault && !wasDefault) {
479             ++default_pairs;
480         } else if (wasDefault && !isDefault) {
481             --default_pairs;
482         }
483
484         /*
485          * As an extension, ncurses allows the pair number to exceed the
486          * terminal's color_pairs value for pairs using a default color.
487          *
488          * Note that updating a pair which used a default color with one
489          * that does not will decrement the count - and possibly interfere
490          * with sequentially adding new pairs.
491          */
492         if (pair > (SP_PARM->_pair_count + default_pairs)) {
493             returnCode(ERR);
494         }
495         SP_PARM->_default_pairs = default_pairs;
496     } else
497 #endif
498     {
499         if ((f < 0) || !OkColorHi(f)
500             || (b < 0) || !OkColorHi(b)
501             || (pair < 1)) {
502             returnCode(ERR);
503         }
504     }
505
506     /*
507      * When a pair's content is changed, replace its colors (if pair was
508      * initialized before a screen update is performed replacing original
509      * pair colors with the new ones).
510      */
511     result = PAIR_OF(f, b);
512     if (previous != 0
513         && previous != result) {
514         int y, x;
515
516         for (y = 0; y <= CurScreen(SP_PARM)->_maxy; y++) {
517             struct ldat *ptr = &(CurScreen(SP_PARM)->_line[y]);
518             bool changed = FALSE;
519             for (x = 0; x <= CurScreen(SP_PARM)->_maxx; x++) {
520                 if (GetPair(ptr->text[x]) == pair) {
521                     /* Set the old cell to zero to ensure it will be
522                        updated on the next doupdate() */
523                     SetChar(ptr->text[x], 0, 0);
524                     CHANGED_CELL(ptr, x);
525                     changed = TRUE;
526                 }
527             }
528             if (changed)
529                 NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
530         }
531     }
532
533     SP_PARM->_color_pairs[pair] = result;
534     if (GET_SCREEN_PAIR(SP_PARM) == pair)
535         SET_SCREEN_PAIR(SP_PARM, (chtype) (~0));        /* force attribute update */
536
537 #ifdef USE_TERM_DRIVER
538     CallDriver_3(SP_PARM, initpair, pair, f, b);
539 #else
540     if (initialize_pair && InPalette(f) && InPalette(b)) {
541         const color_t *tp = DefaultPalette;
542
543         TR(TRACE_ATTRS,
544            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
545             pair,
546             tp[f].red, tp[f].green, tp[f].blue,
547             tp[b].red, tp[b].green, tp[b].blue));
548
549         TPUTS_TRACE("initialize_pair");
550         putp(TPARM_7(initialize_pair,
551                      pair,
552                      tp[f].red, tp[f].green, tp[f].blue,
553                      tp[b].red, tp[b].green, tp[b].blue));
554     }
555 #endif
556
557     returnCode(OK);
558 }
559
560 #if NCURSES_SP_FUNCS
561 NCURSES_EXPORT(int)
562 init_pair(short pair, short f, short b)
563 {
564     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
565 }
566 #endif
567
568 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
569
570 NCURSES_EXPORT(int)
571 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
572                              short color, short r, short g, short b)
573 {
574     int result = ERR;
575     int maxcolors;
576
577     T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
578        (void *) SP_PARM,
579        color,
580        r, g, b));
581
582     if (SP_PARM == 0)
583         returnCode(result);
584
585     maxcolors = MaxColors;
586
587     if (InitColor
588         && SP_PARM->_coloron
589         && (color >= 0 && OkColorHi(color))
590         && (okRGB(r) && okRGB(g) && okRGB(b))) {
591
592         SP_PARM->_color_table[color].init = 1;
593         SP_PARM->_color_table[color].r = r;
594         SP_PARM->_color_table[color].g = g;
595         SP_PARM->_color_table[color].b = b;
596
597         if (UseHlsPalette) {
598             rgb2hls(r, g, b,
599                     &SP_PARM->_color_table[color].red,
600                     &SP_PARM->_color_table[color].green,
601                     &SP_PARM->_color_table[color].blue);
602         } else {
603             SP_PARM->_color_table[color].red = r;
604             SP_PARM->_color_table[color].green = g;
605             SP_PARM->_color_table[color].blue = b;
606         }
607
608 #ifdef USE_TERM_DRIVER
609         CallDriver_4(SP_PARM, initcolor, color, r, g, b);
610 #else
611         TPUTS_TRACE("initialize_color");
612         putp(TPARM_4(initialize_color, color, r, g, b));
613 #endif
614         SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
615
616         result = OK;
617     }
618     returnCode(result);
619 }
620
621 #if NCURSES_SP_FUNCS
622 NCURSES_EXPORT(int)
623 init_color(short color, short r, short g, short b)
624 {
625     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
626 }
627 #endif
628
629 NCURSES_EXPORT(bool)
630 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
631 {
632     int result = FALSE;
633
634     T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
635
636     if (HasTerminal(SP_PARM) && (CanChange != 0)) {
637         result = TRUE;
638     }
639
640     returnCode(result);
641 }
642
643 #if NCURSES_SP_FUNCS
644 NCURSES_EXPORT(bool)
645 can_change_color(void)
646 {
647     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
648 }
649 #endif
650
651 NCURSES_EXPORT(bool)
652 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
653 {
654     int code = FALSE;
655
656     (void) SP_PARM;
657     T((T_CALLED("has_colors()")));
658     if (HasTerminal(SP_PARM)) {
659 #ifdef USE_TERM_DRIVER
660         code = HasColor;
661 #else
662         code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
663                  && (((set_foreground != NULL)
664                       && (set_background != NULL))
665                      || ((set_a_foreground != NULL)
666                          && (set_a_background != NULL))
667                      || set_color_pair)) ? TRUE : FALSE);
668 #endif
669     }
670     returnCode(code);
671 }
672
673 #if NCURSES_SP_FUNCS
674 NCURSES_EXPORT(bool)
675 has_colors(void)
676 {
677     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
678 }
679 #endif
680
681 NCURSES_EXPORT(int)
682 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
683                                 short color, short *r, short *g, short *b)
684 {
685     int result = ERR;
686     int maxcolors;
687
688     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
689        (void *) SP_PARM,
690        color,
691        (void *) r,
692        (void *) g,
693        (void *) b));
694
695     if (SP_PARM == 0)
696         returnCode(result);
697
698     maxcolors = MaxColors;
699
700     if (color < 0 || !OkColorHi(color) || !SP_PARM->_coloron) {
701         result = ERR;
702     } else {
703         NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
704         NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
705         NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
706
707         if (r)
708             *r = c_r;
709         if (g)
710             *g = c_g;
711         if (b)
712             *b = c_b;
713
714         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
715                          color, c_r, c_g, c_b));
716         result = OK;
717     }
718     returnCode(result);
719 }
720
721 #if NCURSES_SP_FUNCS
722 NCURSES_EXPORT(int)
723 color_content(short color, short *r, short *g, short *b)
724 {
725     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
726 }
727 #endif
728
729 NCURSES_EXPORT(int)
730 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
731                                short pair, short *f, short *b)
732 {
733     int result;
734
735     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
736        (void *) SP_PARM,
737        pair,
738        (void *) f,
739        (void *) b));
740
741     if (!ValidPair(pair)) {
742         result = ERR;
743     } else {
744         NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
745         NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
746
747 #if NCURSES_EXT_FUNCS
748         if (fg == COLOR_DEFAULT)
749             fg = -1;
750         if (bg == COLOR_DEFAULT)
751             bg = -1;
752 #endif
753
754         if (f)
755             *f = fg;
756         if (b)
757             *b = bg;
758
759         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
760                          (void *) SP_PARM,
761                          pair,
762                          fg, bg));
763         result = OK;
764     }
765     returnCode(result);
766 }
767
768 #if NCURSES_SP_FUNCS
769 NCURSES_EXPORT(int)
770 pair_content(short pair, short *f, short *b)
771 {
772     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
773 }
774 #endif
775
776 NCURSES_EXPORT(void)
777 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
778                                short old_pair,
779                                short pair,
780                                bool reverse,
781                                NCURSES_SP_OUTC outc)
782 {
783 #ifdef USE_TERM_DRIVER
784     CallDriver_4(SP_PARM, docolor, old_pair, pair, reverse, outc);
785 #else
786     NCURSES_COLOR_T fg = COLOR_DEFAULT;
787     NCURSES_COLOR_T bg = COLOR_DEFAULT;
788     NCURSES_COLOR_T old_fg, old_bg;
789
790     if (!ValidPair(pair)) {
791         return;
792     } else if (pair != 0) {
793         if (set_color_pair) {
794             TPUTS_TRACE("set_color_pair");
795             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
796                                     TPARM_1(set_color_pair, pair),
797                                     1, outc);
798             return;
799         } else if (SP_PARM != 0) {
800             pair_content((short) pair, &fg, &bg);
801         }
802     }
803
804     if (old_pair >= 0
805         && SP_PARM != 0
806         && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
807         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
808             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
809 #if NCURSES_EXT_FUNCS
810             /*
811              * A minor optimization - but extension.  If "AX" is specified in
812              * the terminal description, treat it as screen's indicator of ECMA
813              * SGR 39 and SGR 49, and assume the two sequences are independent.
814              */
815             if (SP_PARM->_has_sgr_39_49
816                 && isDefaultColor(old_bg)
817                 && !isDefaultColor(old_fg)) {
818                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
819             } else if (SP_PARM->_has_sgr_39_49
820                        && isDefaultColor(old_fg)
821                        && !isDefaultColor(old_bg)) {
822                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
823             } else
824 #endif
825                 reset_color_pair(NCURSES_SP_ARG);
826         }
827     } else {
828         reset_color_pair(NCURSES_SP_ARG);
829         if (old_pair < 0)
830             return;
831     }
832
833 #if NCURSES_EXT_FUNCS
834     if (isDefaultColor(fg))
835         fg = (short) default_fg(NCURSES_SP_ARG);
836     if (isDefaultColor(bg))
837         bg = (short) default_bg(NCURSES_SP_ARG);
838 #endif
839
840     if (reverse) {
841         NCURSES_COLOR_T xx = fg;
842         fg = bg;
843         bg = xx;
844     }
845
846     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
847                      fg, bg));
848
849     if (!isDefaultColor(fg)) {
850         set_foreground_color(NCURSES_SP_ARGx fg, outc);
851     }
852     if (!isDefaultColor(bg)) {
853         set_background_color(NCURSES_SP_ARGx bg, outc);
854     }
855 #endif
856 }
857
858 #if NCURSES_SP_FUNCS
859 NCURSES_EXPORT(void)
860 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
861 {
862     SetSafeOutcWrapper(outc);
863     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
864                                    old_pair,
865                                    pair,
866                                    reverse,
867                                    _nc_outc_wrapper);
868 }
869 #endif