]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
72a2a4857559dee5e11878f850dea736d3937540
[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.116 2017/03/25 21:10:54 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             SP_PARM->_pair_limit = limit_PAIRS(SP_PARM->_pair_limit);
346 #endif
347             SP_PARM->_pair_count = maxpairs;
348             SP_PARM->_color_count = maxcolors;
349 #if !USE_REENTRANT
350             COLOR_PAIRS = maxpairs;
351             COLORS = maxcolors;
352 #endif
353
354             SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t, SP_PARM->_pair_limit);
355             if (SP_PARM->_color_pairs != 0) {
356                 SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
357                 if (SP_PARM->_color_table != 0) {
358                     MakeColorPair(SP_PARM->_color_pairs[0],
359                                   default_fg(NCURSES_SP_ARG),
360                                   default_bg(NCURSES_SP_ARG));
361                     init_color_table(NCURSES_SP_ARG);
362
363                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
364                        COLORS, COLOR_PAIRS));
365
366                     SP_PARM->_coloron = 1;
367                     result = OK;
368                 } else if (SP_PARM->_color_pairs != 0) {
369                     FreeAndNull(SP_PARM->_color_pairs);
370                 }
371             }
372         } else {
373             result = OK;
374         }
375     }
376     returnCode(result);
377 }
378
379 #if NCURSES_SP_FUNCS
380 NCURSES_EXPORT(int)
381 start_color(void)
382 {
383     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
384 }
385 #endif
386
387 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
388 static void
389 rgb2hls(int r, int g, int b, int *h, int *l, int *s)
390 /* convert RGB to HLS system */
391 {
392     int min, max, t;
393
394     if ((min = g < r ? g : r) > b)
395         min = b;
396     if ((max = g > r ? g : r) < b)
397         max = b;
398
399     /* calculate lightness */
400     *l = ((min + max) / 20);
401
402     if (min == max) {           /* black, white and all shades of gray */
403         *h = 0;
404         *s = 0;
405         return;
406     }
407
408     /* calculate saturation */
409     if (*l < 50)
410         *s = (((max - min) * 100) / (max + min));
411     else
412         *s = (((max - min) * 100) / (2000 - max - min));
413
414     /* calculate hue */
415     if (r == max)
416         t = (120 + ((g - b) * 60) / (max - min));
417     else if (g == max)
418         t = (240 + ((b - r) * 60) / (max - min));
419     else
420         t = (360 + ((r - g) * 60) / (max - min));
421
422     *h = (t % 360);
423 }
424
425 /*
426  * Change all cells which use(d) a given color pair to force a repaint.
427  */
428 NCURSES_EXPORT(void)
429 _nc_change_pair(SCREEN *sp, int pair)
430 {
431     int y, x;
432
433     for (y = 0; y <= CurScreen(sp)->_maxy; y++) {
434         struct ldat *ptr = &(CurScreen(sp)->_line[y]);
435         bool changed = FALSE;
436         for (x = 0; x <= CurScreen(sp)->_maxx; x++) {
437             if (GetPair(ptr->text[x]) == pair) {
438                 /* Set the old cell to zero to ensure it will be
439                    updated on the next doupdate() */
440                 SetChar(ptr->text[x], 0, 0);
441                 CHANGED_CELL(ptr, x);
442                 changed = TRUE;
443             }
444         }
445         if (changed)
446             NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
447     }
448 }
449
450 /*
451  * Extension (1997/1/18) - Allow negative f/b values to set default color
452  * values.
453  */
454 NCURSES_EXPORT(int)
455 _nc_init_pair(SCREEN *sp, int pair, int f, int b)
456 {
457     static colorpair_t null_pair;
458     colorpair_t result = null_pair;
459     colorpair_t previous;
460     int maxcolors;
461
462     T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) sp, pair, f, b));
463
464     if (!ValidPair(sp, pair))
465         returnCode(ERR);
466
467     maxcolors = MaxColors;
468
469     previous = sp->_color_pairs[pair];
470 #if NCURSES_EXT_FUNCS
471     if (sp->_default_color || sp->_assumed_color) {
472         bool isDefault = FALSE;
473         bool wasDefault = FALSE;
474         int default_pairs = sp->_default_pairs;
475
476         /*
477          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
478          * internal unsigned values which we will store in the _color_pairs[]
479          * table.
480          */
481         if (isDefaultColor(f)) {
482             f = COLOR_DEFAULT;
483             isDefault = TRUE;
484         } else if (!OkColorHi(f)) {
485             returnCode(ERR);
486         }
487
488         if (isDefaultColor(b)) {
489             b = COLOR_DEFAULT;
490             isDefault = TRUE;
491         } else if (!OkColorHi(b)) {
492             returnCode(ERR);
493         }
494
495         /*
496          * Check if the table entry that we are going to init/update used
497          * default colors.
498          */
499         if (isDefaultColor(FORE_OF(previous))
500             || isDefaultColor(BACK_OF(previous)))
501             wasDefault = TRUE;
502
503         /*
504          * Keep track of the number of entries in the color pair table which
505          * used a default color.
506          */
507         if (isDefault && !wasDefault) {
508             ++default_pairs;
509         } else if (wasDefault && !isDefault) {
510             --default_pairs;
511         }
512
513         /*
514          * As an extension, ncurses allows the pair number to exceed the
515          * terminal's color_pairs value for pairs using a default color.
516          *
517          * Note that updating a pair which used a default color with one
518          * that does not will decrement the count - and possibly interfere
519          * with sequentially adding new pairs.
520          */
521         if (pair > (sp->_pair_count + default_pairs)) {
522             returnCode(ERR);
523         }
524         sp->_default_pairs = default_pairs;
525     } else
526 #endif
527     {
528         if ((f < 0) || !OkColorHi(f)
529             || (b < 0) || !OkColorHi(b)
530             || (pair < 1)) {
531             returnCode(ERR);
532         }
533     }
534
535     /*
536      * When a pair's content is changed, replace its colors (if pair was
537      * initialized before a screen update is performed replacing original
538      * pair colors with the new ones).
539      */
540     MakeColorPair(result, f, b);
541     if (FORE_OF(previous) != 0
542         && BACK_OF(previous) != 0
543         && !isSamePair(previous, result)) {
544         _nc_change_pair(sp, pair);
545     }
546
547     _nc_reset_color_pair(sp, pair, &result);
548     sp->_color_pairs[pair] = result;
549     _nc_set_color_pair(sp, pair, cpINIT);
550
551     if (GET_SCREEN_PAIR(sp) == pair)
552         SET_SCREEN_PAIR(sp, (int) (~0));        /* force attribute update */
553
554 #ifdef USE_TERM_DRIVER
555     CallDriver_3(sp, td_initpair, pair, f, b);
556 #else
557     if (initialize_pair && InPalette(f) && InPalette(b)) {
558         const color_t *tp = DefaultPalette;
559
560         TR(TRACE_ATTRS,
561            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
562             (int) pair,
563             (int) tp[f].red, (int) tp[f].green, (int) tp[f].blue,
564             (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
565
566         NCURSES_PUTP2("initialize_pair",
567                       TPARM_7(initialize_pair,
568                               pair,
569                               (int) tp[f].red,
570                               (int) tp[f].green,
571                               (int) tp[f].blue,
572                               (int) tp[b].red,
573                               (int) tp[b].green,
574                               (int) tp[b].blue));
575     }
576 #endif
577
578     returnCode(OK);
579 }
580
581 NCURSES_EXPORT(int)
582 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx
583                             NCURSES_PAIRS_T pair,
584                             NCURSES_COLOR_T f,
585                             NCURSES_COLOR_T b)
586 {
587     return _nc_init_pair(SP_PARM, pair, f, b);
588 }
589
590 #if NCURSES_SP_FUNCS
591 NCURSES_EXPORT(int)
592 init_pair(NCURSES_COLOR_T pair, NCURSES_COLOR_T f, NCURSES_COLOR_T b)
593 {
594     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
595 }
596 #endif
597
598 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
599
600 NCURSES_EXPORT(int)
601 _nc_init_color(SCREEN *sp, int color, int r, int g, int b)
602 {
603     int result = ERR;
604     int maxcolors;
605
606     T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
607        (void *) sp,
608        color,
609        r, g, b));
610
611     if (sp == 0)
612         returnCode(result);
613
614     maxcolors = MaxColors;
615
616     if (InitColor
617         && sp->_coloron
618         && (color >= 0 && OkColorHi(color))
619         && (okRGB(r) && okRGB(g) && okRGB(b))) {
620
621         sp->_color_table[color].init = 1;
622         sp->_color_table[color].r = r;
623         sp->_color_table[color].g = g;
624         sp->_color_table[color].b = b;
625
626         if (UseHlsPalette) {
627             rgb2hls(r, g, b,
628                     &sp->_color_table[color].red,
629                     &sp->_color_table[color].green,
630                     &sp->_color_table[color].blue);
631         } else {
632             sp->_color_table[color].red = r;
633             sp->_color_table[color].green = g;
634             sp->_color_table[color].blue = b;
635         }
636
637 #ifdef USE_TERM_DRIVER
638         CallDriver_4(sp, td_initcolor, color, r, g, b);
639 #else
640         NCURSES_PUTP2("initialize_color",
641                       TPARM_4(initialize_color, color, r, g, b));
642 #endif
643         sp->_color_defs = max(color + 1, sp->_color_defs);
644
645         result = OK;
646     }
647     returnCode(result);
648 }
649
650 NCURSES_EXPORT(int)
651 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
652                              NCURSES_COLOR_T color,
653                              NCURSES_COLOR_T r,
654                              NCURSES_COLOR_T g,
655                              NCURSES_COLOR_T b)
656 {
657     return _nc_init_color(SP_PARM, color, r, g, b);
658 }
659
660 #if NCURSES_SP_FUNCS
661 NCURSES_EXPORT(int)
662 init_color(NCURSES_COLOR_T color,
663            NCURSES_COLOR_T r,
664            NCURSES_COLOR_T g,
665            NCURSES_COLOR_T b)
666 {
667     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
668 }
669 #endif
670
671 NCURSES_EXPORT(bool)
672 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
673 {
674     int result = FALSE;
675
676     T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
677
678     if (HasTerminal(SP_PARM) && (CanChange != 0)) {
679         result = TRUE;
680     }
681
682     returnCode(result);
683 }
684
685 #if NCURSES_SP_FUNCS
686 NCURSES_EXPORT(bool)
687 can_change_color(void)
688 {
689     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
690 }
691 #endif
692
693 NCURSES_EXPORT(bool)
694 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
695 {
696     int code = FALSE;
697
698     (void) SP_PARM;
699     T((T_CALLED("has_colors()")));
700     if (HasTerminal(SP_PARM)) {
701 #ifdef USE_TERM_DRIVER
702         code = HasColor;
703 #else
704         code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
705                  && (((set_foreground != NULL)
706                       && (set_background != NULL))
707                      || ((set_a_foreground != NULL)
708                          && (set_a_background != NULL))
709                      || set_color_pair)) ? TRUE : FALSE);
710 #endif
711     }
712     returnCode(code);
713 }
714
715 #if NCURSES_SP_FUNCS
716 NCURSES_EXPORT(bool)
717 has_colors(void)
718 {
719     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
720 }
721 #endif
722
723 static int
724 _nc_color_content(SCREEN *sp, int color, int *r, int *g, int *b)
725 {
726     int result = ERR;
727     int maxcolors;
728
729     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
730        (void *) sp,
731        color,
732        (void *) r,
733        (void *) g,
734        (void *) b));
735
736     if (sp == 0)
737         returnCode(result);
738
739     maxcolors = MaxColors;
740
741     if (color < 0 || !OkColorHi(color) || !sp->_coloron) {
742         result = ERR;
743     } else {
744         int c_r = sp->_color_table[color].red;
745         int c_g = sp->_color_table[color].green;
746         int c_b = sp->_color_table[color].blue;
747
748         if (r)
749             *r = c_r;
750         if (g)
751             *g = c_g;
752         if (b)
753             *b = c_b;
754
755         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
756                          color, c_r, c_g, c_b));
757         result = OK;
758     }
759     returnCode(result);
760 }
761
762 NCURSES_EXPORT(int)
763 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
764                                 NCURSES_COLOR_T color,
765                                 NCURSES_COLOR_T *r,
766                                 NCURSES_COLOR_T *g,
767                                 NCURSES_COLOR_T *b)
768 {
769     int my_r, my_g, my_b;
770     int rc = _nc_color_content(SP_PARM, color, &my_r, &my_g, &my_b);
771     if (rc == OK) {
772         *r = limit_COLOR(my_r);
773         *g = limit_COLOR(my_g);
774         *b = limit_COLOR(my_b);
775     }
776     return rc;
777 }
778
779 #if NCURSES_SP_FUNCS
780 NCURSES_EXPORT(int)
781 color_content(NCURSES_COLOR_T color,
782               NCURSES_COLOR_T *r,
783               NCURSES_COLOR_T *g,
784               NCURSES_COLOR_T *b)
785 {
786     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
787 }
788 #endif
789
790 NCURSES_EXPORT(int)
791 _nc_pair_content(SCREEN *sp, int pair, int *f, int *b)
792 {
793     int result;
794
795     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
796        (void *) sp,
797        (int) pair,
798        (void *) f,
799        (void *) b));
800
801     if (!ValidPair(sp, pair)) {
802         result = ERR;
803     } else {
804         int fg = FORE_OF(sp->_color_pairs[pair]);
805         int bg = BACK_OF(sp->_color_pairs[pair]);
806
807 #if NCURSES_EXT_FUNCS
808         if (isDefaultColor(fg))
809             fg = -1;
810         if (isDefaultColor(bg))
811             bg = -1;
812 #endif
813
814         if (f)
815             *f = fg;
816         if (b)
817             *b = bg;
818
819         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
820                          (void *) sp,
821                          (int) pair,
822                          (int) fg, (int) bg));
823         result = OK;
824     }
825     returnCode(result);
826 }
827
828 NCURSES_EXPORT(int)
829 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
830                                NCURSES_PAIRS_T pair,
831                                NCURSES_COLOR_T *f,
832                                NCURSES_COLOR_T *b)
833 {
834     int my_f, my_b;
835     int rc = _nc_pair_content(SP_PARM, pair, &my_f, &my_b);
836     if (rc == OK) {
837         *f = limit_COLOR(my_f);
838         *b = limit_COLOR(my_b);
839     }
840     return rc;
841 }
842
843 #if NCURSES_SP_FUNCS
844 NCURSES_EXPORT(int)
845 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
846 {
847     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
848 }
849 #endif
850
851 NCURSES_EXPORT(void)
852 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
853                                int old_pair,
854                                int pair,
855                                int reverse,
856                                NCURSES_SP_OUTC outc)
857 {
858 #ifdef USE_TERM_DRIVER
859     CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
860 #else
861     int fg = COLOR_DEFAULT;
862     int bg = COLOR_DEFAULT;
863     int old_fg = -1;
864     int old_bg = -1;
865
866     if (!ValidPair(SP_PARM, pair)) {
867         return;
868     } else if (pair != 0) {
869         if (set_color_pair) {
870             TPUTS_TRACE("set_color_pair");
871             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
872                                     TPARM_1(set_color_pair, pair),
873                                     1, outc);
874             return;
875         } else if (SP_PARM != 0) {
876             if (_nc_pair_content(SP_PARM, pair, &fg, &bg) == ERR)
877                 return;
878         }
879     }
880
881     if (old_pair >= 0
882         && SP_PARM != 0
883         && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
884         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
885             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
886 #if NCURSES_EXT_FUNCS
887             /*
888              * A minor optimization - but extension.  If "AX" is specified in
889              * the terminal description, treat it as screen's indicator of ECMA
890              * SGR 39 and SGR 49, and assume the two sequences are independent.
891              */
892             if (SP_PARM->_has_sgr_39_49
893                 && isDefaultColor(old_bg)
894                 && !isDefaultColor(old_fg)) {
895                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
896             } else if (SP_PARM->_has_sgr_39_49
897                        && isDefaultColor(old_fg)
898                        && !isDefaultColor(old_bg)) {
899                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
900             } else
901 #endif
902                 reset_color_pair(NCURSES_SP_ARG);
903         }
904     } else {
905         reset_color_pair(NCURSES_SP_ARG);
906         if (old_pair < 0 && pair <= 0)
907             return;
908     }
909
910 #if NCURSES_EXT_FUNCS
911     if (isDefaultColor(fg))
912         fg = default_fg(NCURSES_SP_ARG);
913     if (isDefaultColor(bg))
914         bg = default_bg(NCURSES_SP_ARG);
915 #endif
916
917     if (reverse) {
918         int xx = fg;
919         fg = bg;
920         bg = xx;
921     }
922
923     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
924                      fg, bg));
925
926     if (!isDefaultColor(fg)) {
927         set_foreground_color(NCURSES_SP_ARGx fg, outc);
928     }
929     if (!isDefaultColor(bg)) {
930         set_background_color(NCURSES_SP_ARGx bg, outc);
931     }
932 #endif
933 }
934
935 #if NCURSES_SP_FUNCS
936 NCURSES_EXPORT(void)
937 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
938 {
939     SetSafeOutcWrapper(outc);
940     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
941                                    old_pair,
942                                    pair,
943                                    reverse,
944                                    _nc_outc_wrapper);
945 }
946 #endif
947
948 #if USE_EXTENDED_COLORS
949 NCURSES_EXPORT(int)
950 NCURSES_SP_NAME(init_extended_pair) (NCURSES_SP_DCLx int pair, int f, int b)
951 {
952     return _nc_init_pair(SP_PARM, pair, f, b);
953 }
954
955 NCURSES_EXPORT(int)
956 NCURSES_SP_NAME(init_extended_color) (NCURSES_SP_DCLx
957                                       int color,
958                                       int r, int g, int b)
959 {
960     return _nc_init_color(SP_PARM, color, r, g, b);
961 }
962
963 NCURSES_EXPORT(int)
964 NCURSES_SP_NAME(extended_color_content) (NCURSES_SP_DCLx
965                                          int color,
966                                          int *r, int *g, int *b)
967 {
968     return _nc_color_content(SP_PARM, color, r, g, b);
969 }
970
971 NCURSES_EXPORT(int)
972 NCURSES_SP_NAME(extended_pair_content) (NCURSES_SP_DCLx
973                                         int pair,
974                                         int *f, int *b)
975 {
976     return _nc_pair_content(SP_PARM, pair, f, b);
977 }
978
979 #if NCURSES_SP_FUNCS
980 NCURSES_EXPORT(int)
981 init_extended_pair(int pair, int f, int b)
982 {
983     return NCURSES_SP_NAME(init_extended_pair) (CURRENT_SCREEN, pair, f, b);
984 }
985
986 NCURSES_EXPORT(int)
987 init_extended_color(int color, int r, int g, int b)
988 {
989     return NCURSES_SP_NAME(init_extended_color) (CURRENT_SCREEN,
990                                                  color,
991                                                  r, g, b);
992 }
993
994 NCURSES_EXPORT(int)
995 extended_color_content(int color, int *r, int *g, int *b)
996 {
997     return NCURSES_SP_NAME(extended_color_content) (CURRENT_SCREEN,
998                                                     color,
999                                                     r, g, b);
1000 }
1001
1002 NCURSES_EXPORT(int)
1003 extended_pair_content(int pair, int *f, int *b)
1004 {
1005     return NCURSES_SP_NAME(extended_pair_content) (CURRENT_SCREEN, pair, f, b);
1006 }
1007 #endif
1008 #endif