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