]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 6.3 - patch 20220430
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright 2018-2021,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
34  *     and: Juergen Pfeifer                         2009                    *
35  ****************************************************************************/
36
37 /* lib_color.c
38  *
39  * Handles color emulation of SYS V curses
40  */
41
42 #define NEW_PAIR_INTERNAL 1
43
44 #include <curses.priv.h>
45 #include <new_pair.h>
46 #include <tic.h>
47
48 #ifndef CUR
49 #define CUR SP_TERMTYPE
50 #endif
51
52 MODULE_ID("$Id: lib_color.c,v 1.149 2022/04/30 18:36:54 tom Exp $")
53
54 #ifdef USE_TERM_DRIVER
55 #define CanChange      InfoOf(SP_PARM).canchange
56 #define DefaultPalette InfoOf(SP_PARM).defaultPalette
57 #define HasColor       InfoOf(SP_PARM).hascolor
58 #define InitColor      InfoOf(SP_PARM).initcolor
59 #define MaxColors      InfoOf(SP_PARM).maxcolors
60 #define MaxPairs       InfoOf(SP_PARM).maxpairs
61 #define UseHlsPalette  (DefaultPalette == _nc_hls_palette)
62 #else
63 #define CanChange      can_change
64 #define DefaultPalette (hue_lightness_saturation ? hls_palette : cga_palette)
65 #define HasColor       has_color
66 #define InitColor      initialize_color
67 #define MaxColors      max_colors
68 #define MaxPairs       max_pairs
69 #define UseHlsPalette  (hue_lightness_saturation)
70 #endif
71
72 #ifndef USE_TERM_DRIVER
73 /*
74  * These should be screen structure members.  They need to be globals for
75  * historical reasons.  So we assign them in start_color() and also in
76  * set_term()'s screen-switching logic.
77  */
78 #if USE_REENTRANT
79 NCURSES_EXPORT(int)
80 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
81 {
82     return SP ? SP->_pair_count : -1;
83 }
84 NCURSES_EXPORT(int)
85 NCURSES_PUBLIC_VAR(COLORS) (void)
86 {
87     return SP ? SP->_color_count : -1;
88 }
89 #else
90 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
91 NCURSES_EXPORT_VAR(int) COLORS = 0;
92 #endif
93 #endif /* !USE_TERM_DRIVER */
94
95 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
96
97 #define MAX_PALETTE     8
98
99 #define OkColorHi(n)    (((n) < COLORS) && ((n) < maxcolors))
100 #define InPalette(n)    ((n) >= 0 && (n) < MAX_PALETTE)
101
102 /*
103  * Given a RGB range of 0..1000, we'll normally set the individual values
104  * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
105  */
106 #define RGB_ON  680
107 #define RGB_OFF 0
108 /* *INDENT-OFF* */
109 static const color_t cga_palette[] =
110 {
111     /*  R               G               B */
112     DATA(RGB_OFF,       RGB_OFF,        RGB_OFF),       /* COLOR_BLACK */
113     DATA(RGB_ON,        RGB_OFF,        RGB_OFF),       /* COLOR_RED */
114     DATA(RGB_OFF,       RGB_ON,         RGB_OFF),       /* COLOR_GREEN */
115     DATA(RGB_ON,        RGB_ON,         RGB_OFF),       /* COLOR_YELLOW */
116     DATA(RGB_OFF,       RGB_OFF,        RGB_ON),        /* COLOR_BLUE */
117     DATA(RGB_ON,        RGB_OFF,        RGB_ON),        /* COLOR_MAGENTA */
118     DATA(RGB_OFF,       RGB_ON,         RGB_ON),        /* COLOR_CYAN */
119     DATA(RGB_ON,        RGB_ON,         RGB_ON),        /* COLOR_WHITE */
120 };
121
122 static const color_t hls_palette[] =
123 {
124     /*          H       L       S */
125     DATA(       0,      0,      0),             /* COLOR_BLACK */
126     DATA(       120,    50,     100),           /* COLOR_RED */
127     DATA(       240,    50,     100),           /* COLOR_GREEN */
128     DATA(       180,    50,     100),           /* COLOR_YELLOW */
129     DATA(       330,    50,     100),           /* COLOR_BLUE */
130     DATA(       60,     50,     100),           /* COLOR_MAGENTA */
131     DATA(       300,    50,     100),           /* COLOR_CYAN */
132     DATA(       0,      50,     100),           /* COLOR_WHITE */
133 };
134
135 #ifdef USE_TERM_DRIVER
136 NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette = cga_palette;
137 NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
138 #endif
139
140 /* *INDENT-ON* */
141 #if NCURSES_EXT_FUNCS
142 /*
143  * These are called from _nc_do_color(), which in turn is called from
144  * vidattr - so we have to assume that sp may be null.
145  */
146 static int
147 default_fg(NCURSES_SP_DCL0)
148 {
149     return (SP_PARM != 0) ? SP_PARM->_default_fg : COLOR_WHITE;
150 }
151
152 static int
153 default_bg(NCURSES_SP_DCL0)
154 {
155     return SP_PARM != 0 ? SP_PARM->_default_bg : COLOR_BLACK;
156 }
157 #else
158 #define default_fg(sp) COLOR_WHITE
159 #define default_bg(sp) COLOR_BLACK
160 #endif
161
162 #ifndef USE_TERM_DRIVER
163 /*
164  * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
165  * to maintain compatibility with a pre-ANSI scheme.  The same scheme is
166  * also used in the FreeBSD syscons.
167  */
168 static int
169 toggled_colors(int c)
170 {
171     if (c < 16) {
172         static const int table[] =
173         {0, 4, 2, 6, 1, 5, 3, 7,
174          8, 12, 10, 14, 9, 13, 11, 15};
175         c = table[c];
176     }
177     return c;
178 }
179 #endif
180
181 static void
182 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
183 {
184 #ifdef USE_TERM_DRIVER
185     CallDriver_3(SP_PARM, td_color, FALSE, bg, outc);
186 #else
187     if (set_a_background) {
188         TPUTS_TRACE("set_a_background");
189         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
190                                 TIPARM_1(set_a_background, bg),
191                                 1, outc);
192     } else {
193         TPUTS_TRACE("set_background");
194         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
195                                 TIPARM_1(set_background, toggled_colors(bg)),
196                                 1, outc);
197     }
198 #endif
199 }
200
201 static void
202 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
203 {
204 #ifdef USE_TERM_DRIVER
205     CallDriver_3(SP_PARM, td_color, TRUE, fg, outc);
206 #else
207     if (set_a_foreground) {
208         TPUTS_TRACE("set_a_foreground");
209         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
210                                 TIPARM_1(set_a_foreground, fg),
211                                 1, outc);
212     } else {
213         TPUTS_TRACE("set_foreground");
214         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
215                                 TIPARM_1(set_foreground, toggled_colors(fg)),
216                                 1, outc);
217     }
218 #endif
219 }
220
221 static void
222 init_color_table(NCURSES_SP_DCL0)
223 {
224     const color_t *tp = DefaultPalette;
225     int n;
226
227     assert(tp != 0);
228
229     for (n = 0; n < COLORS; n++) {
230         if (InPalette(n)) {
231             SP_PARM->_color_table[n] = tp[n];
232         } else {
233             SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
234             if (UseHlsPalette) {
235                 SP_PARM->_color_table[n].green = 100;
236             } else {
237                 if (SP_PARM->_color_table[n].red)
238                     SP_PARM->_color_table[n].red = 1000;
239                 if (SP_PARM->_color_table[n].green)
240                     SP_PARM->_color_table[n].green = 1000;
241                 if (SP_PARM->_color_table[n].blue)
242                     SP_PARM->_color_table[n].blue = 1000;
243             }
244         }
245     }
246 }
247
248 static bool
249 init_direct_colors(NCURSES_SP_DCL0)
250 {
251     static NCURSES_CONST char name[] = "RGB";
252
253     rgb_bits_t *result = &(SP_PARM->_direct_color);
254
255     result->value = 0;
256
257     if (COLORS >= 8) {
258         int n;
259         const char *s;
260         int width;
261
262         /* find the number of bits needed for the maximum color value */
263         for (width = 0; (1 << width) - 1 < (COLORS - 1); ++width) {
264             ;
265         }
266
267         if (tigetflag(name) > 0) {
268             n = (width + 2) / 3;
269             result->bits.red = UChar(n);
270             result->bits.green = UChar(n);
271             result->bits.blue = UChar(width - (2 * n));
272         } else if ((n = tigetnum(name)) > 0) {
273             result->bits.red = UChar(n);
274             result->bits.green = UChar(n);
275             result->bits.blue = UChar(n);
276         } else if ((s = tigetstr(name)) != 0 && VALID_STRING(s)) {
277             int red = n;
278             int green = n;
279             int blue = width - (2 * n);
280
281             switch (sscanf(s, "%d/%d/%d", &red, &green, &blue)) {
282             default:
283                 blue = width - (2 * n);
284                 /* FALLTHRU */
285             case 1:
286                 green = n;
287                 /* FALLTHRU */
288             case 2:
289                 red = n;
290                 /* FALLTHRU */
291             case 3:
292                 /* okay */
293                 break;
294             }
295             result->bits.red = UChar(red);
296             result->bits.green = UChar(green);
297             result->bits.blue = UChar(blue);
298         }
299     }
300     return (result->value != 0);
301 }
302
303 /*
304  * Reset the color pair, e.g., to whatever color pair 0 is.
305  */
306 static bool
307 reset_color_pair(NCURSES_SP_DCL0)
308 {
309 #ifdef USE_TERM_DRIVER
310     return CallDriver(SP_PARM, td_rescol);
311 #else
312     bool result = FALSE;
313
314     (void) SP_PARM;
315     if (orig_pair != 0) {
316         (void) NCURSES_PUTP2("orig_pair", orig_pair);
317         result = TRUE;
318     }
319     return result;
320 #endif
321 }
322
323 /*
324  * Reset color pairs and definitions.  Actually we do both more to accommodate
325  * badly-written terminal descriptions than for the relatively rare case where
326  * someone has changed the color definitions.
327  */
328 NCURSES_EXPORT(bool)
329 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
330 {
331     int result = FALSE;
332
333     T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
334     if (SP_PARM->_color_defs > 0)
335         SP_PARM->_color_defs = -(SP_PARM->_color_defs);
336     if (reset_color_pair(NCURSES_SP_ARG))
337         result = TRUE;
338
339 #ifdef USE_TERM_DRIVER
340     result = CallDriver(SP_PARM, td_rescolors);
341 #else
342     if (orig_colors != 0) {
343         NCURSES_PUTP2("orig_colors", orig_colors);
344         result = TRUE;
345     }
346 #endif
347     returnBool(result);
348 }
349
350 #if NCURSES_SP_FUNCS
351 NCURSES_EXPORT(bool)
352 _nc_reset_colors(void)
353 {
354     return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
355 }
356 #endif
357
358 NCURSES_EXPORT(int)
359 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
360 {
361     int result = ERR;
362
363     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
364
365     if (SP_PARM == 0) {
366         result = ERR;
367     } else if (SP_PARM->_coloron) {
368         result = OK;
369     } else {
370         int maxpairs = MaxPairs;
371         int maxcolors = MaxColors;
372         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
373             set_foreground_color(NCURSES_SP_ARGx
374                                  default_fg(NCURSES_SP_ARG),
375                                  NCURSES_SP_NAME(_nc_outch));
376             set_background_color(NCURSES_SP_ARGx
377                                  default_bg(NCURSES_SP_ARG),
378                                  NCURSES_SP_NAME(_nc_outch));
379         }
380 #if !NCURSES_EXT_COLORS
381         /*
382          * Without ext-colors, we cannot represent more than 256 color pairs.
383          */
384         if (maxpairs > 256)
385             maxpairs = 256;
386 #endif
387
388         if (maxpairs > 0 && maxcolors > 0) {
389             SP_PARM->_pair_limit = maxpairs;
390
391 #if NCURSES_EXT_FUNCS
392             /*
393              * If using default colors, allocate extra space in table to
394              * allow for default-color as a component of a color-pair.
395              */
396             SP_PARM->_pair_limit += (1 + (2 * maxcolors));
397 #if !NCURSES_EXT_COLORS
398             SP_PARM->_pair_limit = limit_PAIRS(SP_PARM->_pair_limit);
399 #endif
400 #endif /* NCURSES_EXT_FUNCS */
401             SP_PARM->_pair_count = maxpairs;
402             SP_PARM->_color_count = maxcolors;
403 #if !USE_REENTRANT
404             COLOR_PAIRS = maxpairs;
405             COLORS = maxcolors;
406 #endif
407
408             ReservePairs(SP_PARM, 16);
409             if (SP_PARM->_color_pairs != 0) {
410                 if (init_direct_colors(NCURSES_SP_ARG)) {
411                     result = OK;
412                 } else {
413                     TYPE_CALLOC(color_t, maxcolors, SP_PARM->_color_table);
414                     if (SP_PARM->_color_table != 0) {
415                         MakeColorPair(SP_PARM->_color_pairs[0],
416                                       default_fg(NCURSES_SP_ARG),
417                                       default_bg(NCURSES_SP_ARG));
418                         init_color_table(NCURSES_SP_ARG);
419
420                         result = OK;
421                     }
422                 }
423                 if (result == OK) {
424                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
425                        COLORS, COLOR_PAIRS));
426
427                     SP_PARM->_coloron = 1;
428                 } else if (SP_PARM->_color_pairs != 0) {
429                     FreeAndNull(SP_PARM->_color_pairs);
430                 }
431             }
432         } else {
433             result = OK;
434         }
435     }
436     returnCode(result);
437 }
438
439 #if NCURSES_SP_FUNCS
440 NCURSES_EXPORT(int)
441 start_color(void)
442 {
443     return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
444 }
445 #endif
446
447 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
448 static void
449 rgb2hls(int r, int g, int b, int *h, int *l, int *s)
450 /* convert RGB to HLS system */
451 {
452     int min, max, t;
453
454     if ((min = g < r ? g : r) > b)
455         min = b;
456     if ((max = g > r ? g : r) < b)
457         max = b;
458
459     /* calculate lightness */
460     *l = ((min + max) / 20);
461
462     if (min == max) {           /* black, white and all shades of gray */
463         *h = 0;
464         *s = 0;
465         return;
466     }
467
468     /* calculate saturation */
469     if (*l < 50)
470         *s = (((max - min) * 100) / (max + min));
471     else
472         *s = (((max - min) * 100) / (2000 - max - min));
473
474     /* calculate hue */
475     if (r == max)
476         t = (120 + ((g - b) * 60) / (max - min));
477     else if (g == max)
478         t = (240 + ((b - r) * 60) / (max - min));
479     else
480         t = (360 + ((r - g) * 60) / (max - min));
481
482     *h = (t % 360);
483 }
484
485 /*
486  * Change all cells which use(d) a given color pair to force a repaint.
487  */
488 NCURSES_EXPORT(void)
489 _nc_change_pair(SCREEN *sp, int pair)
490 {
491     int y, x;
492
493     if (CurScreen(sp)->_clear)
494         return;
495 #if NO_LEAKS
496     if (_nc_globals.leak_checking)
497         return;
498 #endif
499
500     for (y = 0; y <= CurScreen(sp)->_maxy; y++) {
501         struct ldat *ptr = &(CurScreen(sp)->_line[y]);
502         bool changed = FALSE;
503         for (x = 0; x <= CurScreen(sp)->_maxx; x++) {
504             if (GetPair(ptr->text[x]) == pair) {
505                 /* Set the old cell to zero to ensure it will be
506                    updated on the next doupdate() */
507                 SetChar(ptr->text[x], 0, 0);
508                 CHANGED_CELL(ptr, x);
509                 changed = TRUE;
510             }
511         }
512         if (changed)
513             NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
514     }
515 }
516
517 NCURSES_EXPORT(void)
518 _nc_reserve_pairs(SCREEN *sp, int want)
519 {
520     int have = sp->_pair_alloc;
521
522     if (have == 0)
523         have = 1;
524     while (have <= want)
525         have *= 2;
526     if (have > sp->_pair_limit)
527         have = sp->_pair_limit;
528
529     if (sp->_color_pairs == 0) {
530         TYPE_CALLOC(colorpair_t, have, sp->_color_pairs);
531     } else if (have > sp->_pair_alloc) {
532 #if NCURSES_EXT_COLORS
533         colorpair_t *next;
534
535         if ((next = typeCalloc(colorpair_t, have)) == 0)
536             _nc_err_abort(MSG_NO_MEMORY);
537         memcpy(next, sp->_color_pairs, (size_t) sp->_pair_alloc * sizeof(*next));
538         _nc_copy_pairs(sp, next, sp->_color_pairs, sp->_pair_alloc);
539         free(sp->_color_pairs);
540         sp->_color_pairs = next;
541 #else
542         TYPE_REALLOC(colorpair_t, have, sp->_color_pairs);
543         if (sp->_color_pairs != 0) {
544             memset(sp->_color_pairs + sp->_pair_alloc, 0,
545                    sizeof(colorpair_t) * (size_t) (have - sp->_pair_alloc));
546         }
547 #endif
548     }
549     if (sp->_color_pairs != 0) {
550         sp->_pair_alloc = have;
551     }
552 }
553
554 /*
555  * Extension (1997/1/18) - Allow negative f/b values to set default color
556  * values.
557  */
558 NCURSES_EXPORT(int)
559 _nc_init_pair(SCREEN *sp, int pair, int f, int b)
560 {
561     static colorpair_t null_pair;
562     colorpair_t result = null_pair;
563     colorpair_t previous;
564     int maxcolors;
565
566     T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) sp, pair, f, b));
567
568     if (!ValidPair(sp, pair))
569         returnCode(ERR);
570
571     maxcolors = MaxColors;
572
573     ReservePairs(sp, pair);
574     previous = sp->_color_pairs[pair];
575 #if NCURSES_EXT_FUNCS
576     if (sp->_default_color || sp->_assumed_color) {
577         bool isDefault = FALSE;
578         bool wasDefault = FALSE;
579         int default_pairs = sp->_default_pairs;
580
581         /*
582          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
583          * internal unsigned values which we will store in the _color_pairs[]
584          * table.
585          */
586         if (isDefaultColor(f)) {
587             f = COLOR_DEFAULT;
588             isDefault = TRUE;
589         } else if (!OkColorHi(f)) {
590             returnCode(ERR);
591         }
592
593         if (isDefaultColor(b)) {
594             b = COLOR_DEFAULT;
595             isDefault = TRUE;
596         } else if (!OkColorHi(b)) {
597             returnCode(ERR);
598         }
599
600         /*
601          * Check if the table entry that we are going to init/update used
602          * default colors.
603          */
604         if (isDefaultColor(FORE_OF(previous))
605             || isDefaultColor(BACK_OF(previous)))
606             wasDefault = TRUE;
607
608         /*
609          * Keep track of the number of entries in the color pair table which
610          * used a default color.
611          */
612         if (isDefault && !wasDefault) {
613             ++default_pairs;
614         } else if (wasDefault && !isDefault) {
615             --default_pairs;
616         }
617
618         /*
619          * As an extension, ncurses allows the pair number to exceed the
620          * terminal's color_pairs value for pairs using a default color.
621          *
622          * Note that updating a pair which used a default color with one
623          * that does not will decrement the count - and possibly interfere
624          * with sequentially adding new pairs.
625          */
626         if (pair > (sp->_pair_count + default_pairs)) {
627             returnCode(ERR);
628         }
629         sp->_default_pairs = default_pairs;
630     } else
631 #endif
632     {
633         if ((f < 0) || !OkColorHi(f)
634             || (b < 0) || !OkColorHi(b)
635             || (pair < 1)) {
636             returnCode(ERR);
637         }
638     }
639
640     /*
641      * When a pair's content is changed, replace its colors (if pair was
642      * initialized before a screen update is performed replacing original
643      * pair colors with the new ones).
644      */
645     MakeColorPair(result, f, b);
646     if ((FORE_OF(previous) != 0
647          || BACK_OF(previous) != 0)
648         && !isSamePair(previous, result)) {
649         _nc_change_pair(sp, pair);
650     }
651
652     _nc_reset_color_pair(sp, pair, &result);
653     sp->_color_pairs[pair] = result;
654     _nc_set_color_pair(sp, pair, cpINIT);
655
656     if (GET_SCREEN_PAIR(sp) == pair)
657         SET_SCREEN_PAIR(sp, (int) (~0));        /* force attribute update */
658
659 #ifdef USE_TERM_DRIVER
660     CallDriver_3(sp, td_initpair, pair, f, b);
661 #else
662     if (initialize_pair && InPalette(f) && InPalette(b)) {
663         const color_t *tp = DefaultPalette;
664
665         TR(TRACE_ATTRS,
666            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
667             (int) pair,
668             (int) tp[f].red, (int) tp[f].green, (int) tp[f].blue,
669             (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
670
671         NCURSES_PUTP2("initialize_pair",
672                       TIPARM_7(initialize_pair,
673                                pair,
674                                (int) tp[f].red,
675                                (int) tp[f].green,
676                                (int) tp[f].blue,
677                                (int) tp[b].red,
678                                (int) tp[b].green,
679                                (int) tp[b].blue));
680     }
681 #endif
682
683     returnCode(OK);
684 }
685
686 NCURSES_EXPORT(int)
687 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx
688                             NCURSES_PAIRS_T pair,
689                             NCURSES_COLOR_T f,
690                             NCURSES_COLOR_T b)
691 {
692     return _nc_init_pair(SP_PARM, pair, f, b);
693 }
694
695 #if NCURSES_SP_FUNCS
696 NCURSES_EXPORT(int)
697 init_pair(NCURSES_COLOR_T pair, NCURSES_COLOR_T f, NCURSES_COLOR_T b)
698 {
699     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
700 }
701 #endif
702
703 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
704
705 NCURSES_EXPORT(int)
706 _nc_init_color(SCREEN *sp, int color, int r, int g, int b)
707 {
708     int result = ERR;
709     int maxcolors;
710
711     T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
712        (void *) sp,
713        color,
714        r, g, b));
715
716     if (sp == 0 || sp->_direct_color.value)
717         returnCode(result);
718
719     maxcolors = MaxColors;
720
721     if (InitColor
722         && sp->_coloron
723         && (color >= 0 && OkColorHi(color))
724         && (okRGB(r) && okRGB(g) && okRGB(b))) {
725
726         sp->_color_table[color].init = 1;
727         sp->_color_table[color].r = r;
728         sp->_color_table[color].g = g;
729         sp->_color_table[color].b = b;
730
731         if (UseHlsPalette) {
732             rgb2hls(r, g, b,
733                     &sp->_color_table[color].red,
734                     &sp->_color_table[color].green,
735                     &sp->_color_table[color].blue);
736         } else {
737             sp->_color_table[color].red = r;
738             sp->_color_table[color].green = g;
739             sp->_color_table[color].blue = b;
740         }
741
742 #ifdef USE_TERM_DRIVER
743         CallDriver_4(sp, td_initcolor, color, r, g, b);
744 #else
745         NCURSES_PUTP2("initialize_color",
746                       TIPARM_4(initialize_color, color, r, g, b));
747 #endif
748         sp->_color_defs = max(color + 1, sp->_color_defs);
749
750         result = OK;
751     }
752     returnCode(result);
753 }
754
755 NCURSES_EXPORT(int)
756 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
757                              NCURSES_COLOR_T color,
758                              NCURSES_COLOR_T r,
759                              NCURSES_COLOR_T g,
760                              NCURSES_COLOR_T b)
761 {
762     return _nc_init_color(SP_PARM, color, r, g, b);
763 }
764
765 #if NCURSES_SP_FUNCS
766 NCURSES_EXPORT(int)
767 init_color(NCURSES_COLOR_T color,
768            NCURSES_COLOR_T r,
769            NCURSES_COLOR_T g,
770            NCURSES_COLOR_T b)
771 {
772     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
773 }
774 #endif
775
776 NCURSES_EXPORT(bool)
777 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
778 {
779     int result = FALSE;
780
781     T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
782
783     if (HasTerminal(SP_PARM) && (CanChange != 0)) {
784         result = TRUE;
785     }
786
787     returnCode(result);
788 }
789
790 #if NCURSES_SP_FUNCS
791 NCURSES_EXPORT(bool)
792 can_change_color(void)
793 {
794     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
795 }
796 #endif
797
798 NCURSES_EXPORT(bool)
799 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
800 {
801     int code = FALSE;
802
803     (void) SP_PARM;
804     T((T_CALLED("has_colors(%p)"), (void *) SP_PARM));
805     if (HasTerminal(SP_PARM)) {
806 #ifdef USE_TERM_DRIVER
807         code = HasColor;
808 #else
809         code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
810                  && (((set_foreground != NULL)
811                       && (set_background != NULL))
812                      || ((set_a_foreground != NULL)
813                          && (set_a_background != NULL))
814                      || set_color_pair)) ? TRUE : FALSE);
815 #endif
816     }
817     returnCode(code);
818 }
819
820 #if NCURSES_SP_FUNCS
821 NCURSES_EXPORT(bool)
822 has_colors(void)
823 {
824     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
825 }
826 #endif
827
828 static int
829 _nc_color_content(SCREEN *sp, int color, int *r, int *g, int *b)
830 {
831     int result = ERR;
832
833     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
834        (void *) sp,
835        color,
836        (void *) r,
837        (void *) g,
838        (void *) b));
839
840     if (sp != 0) {
841         int maxcolors = MaxColors;
842
843         if (color >= 0 && OkColorHi(color) && sp->_coloron) {
844             int c_r, c_g, c_b;
845
846             if (sp->_direct_color.value) {
847                 rgb_bits_t *work = &(sp->_direct_color);
848
849 #define max_direct_color(name)  ((1 << work->bits.name) - 1)
850 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
851
852                 int max_r = max_direct_color(red);
853                 int max_g = max_direct_color(green);
854                 int max_b = max_direct_color(blue);
855
856                 int bitoff = 0;
857
858                 c_b = value_direct_color(max_b);
859                 bitoff += work->bits.blue;
860
861                 c_g = value_direct_color(max_g);
862                 bitoff += work->bits.green;
863
864                 c_r = value_direct_color(max_r);
865
866             } else {
867                 c_r = sp->_color_table[color].red;
868                 c_g = sp->_color_table[color].green;
869                 c_b = sp->_color_table[color].blue;
870             }
871
872             if (r)
873                 *r = c_r;
874             if (g)
875                 *g = c_g;
876             if (b)
877                 *b = c_b;
878
879             TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
880                              color, c_r, c_g, c_b));
881             result = OK;
882         }
883     }
884     if (result != OK) {
885         if (r)
886             *r = 0;
887         if (g)
888             *g = 0;
889         if (b)
890             *b = 0;
891     }
892     returnCode(result);
893 }
894
895 NCURSES_EXPORT(int)
896 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
897                                 NCURSES_COLOR_T color,
898                                 NCURSES_COLOR_T *r,
899                                 NCURSES_COLOR_T *g,
900                                 NCURSES_COLOR_T *b)
901 {
902     int my_r, my_g, my_b;
903     int rc = _nc_color_content(SP_PARM, color, &my_r, &my_g, &my_b);
904     if (rc == OK) {
905         *r = limit_COLOR(my_r);
906         *g = limit_COLOR(my_g);
907         *b = limit_COLOR(my_b);
908     }
909     return rc;
910 }
911
912 #if NCURSES_SP_FUNCS
913 NCURSES_EXPORT(int)
914 color_content(NCURSES_COLOR_T color,
915               NCURSES_COLOR_T *r,
916               NCURSES_COLOR_T *g,
917               NCURSES_COLOR_T *b)
918 {
919     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
920 }
921 #endif
922
923 NCURSES_EXPORT(int)
924 _nc_pair_content(SCREEN *sp, int pair, int *f, int *b)
925 {
926     int result;
927
928     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
929        (void *) sp,
930        (int) pair,
931        (void *) f,
932        (void *) b));
933
934     if (!ValidPair(sp, pair)) {
935         result = ERR;
936     } else {
937         int fg;
938         int bg;
939
940         ReservePairs(sp, pair);
941         fg = FORE_OF(sp->_color_pairs[pair]);
942         bg = BACK_OF(sp->_color_pairs[pair]);
943 #if NCURSES_EXT_FUNCS
944         if (isDefaultColor(fg))
945             fg = -1;
946         if (isDefaultColor(bg))
947             bg = -1;
948 #endif
949
950         if (f)
951             *f = fg;
952         if (b)
953             *b = bg;
954
955         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
956                          (void *) sp,
957                          (int) pair,
958                          (int) fg, (int) bg));
959         result = OK;
960     }
961     returnCode(result);
962 }
963
964 NCURSES_EXPORT(int)
965 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
966                                NCURSES_PAIRS_T pair,
967                                NCURSES_COLOR_T *f,
968                                NCURSES_COLOR_T *b)
969 {
970     int my_f, my_b;
971     int rc = _nc_pair_content(SP_PARM, pair, &my_f, &my_b);
972     if (rc == OK) {
973         *f = limit_COLOR(my_f);
974         *b = limit_COLOR(my_b);
975     }
976     return rc;
977 }
978
979 #if NCURSES_SP_FUNCS
980 NCURSES_EXPORT(int)
981 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
982 {
983     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
984 }
985 #endif
986
987 NCURSES_EXPORT(void)
988 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
989                                int old_pair,
990                                int pair,
991                                int reverse,
992                                NCURSES_SP_OUTC outc)
993 {
994 #ifdef USE_TERM_DRIVER
995     CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
996 #else
997     int fg = COLOR_DEFAULT;
998     int bg = COLOR_DEFAULT;
999     int old_fg = -1;
1000     int old_bg = -1;
1001
1002     if (!ValidPair(SP_PARM, pair)) {
1003         return;
1004     } else if (pair != 0) {
1005         if (set_color_pair) {
1006             TPUTS_TRACE("set_color_pair");
1007             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1008                                     TIPARM_1(set_color_pair, pair),
1009                                     1, outc);
1010             return;
1011         } else if (SP_PARM != 0) {
1012             if (_nc_pair_content(SP_PARM, pair, &fg, &bg) == ERR)
1013                 return;
1014         }
1015     }
1016
1017     if (old_pair >= 0
1018         && SP_PARM != 0
1019         && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
1020         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
1021             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
1022 #if NCURSES_EXT_FUNCS
1023             /*
1024              * A minor optimization - but extension.  If "AX" is specified in
1025              * the terminal description, treat it as screen's indicator of ECMA
1026              * SGR 39 and SGR 49, and assume the two sequences are independent.
1027              */
1028             if (SP_PARM->_has_sgr_39_49
1029                 && isDefaultColor(old_bg)
1030                 && !isDefaultColor(old_fg)) {
1031                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
1032             } else if (SP_PARM->_has_sgr_39_49
1033                        && isDefaultColor(old_fg)
1034                        && !isDefaultColor(old_bg)) {
1035                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
1036             } else
1037 #endif
1038                 reset_color_pair(NCURSES_SP_ARG);
1039         }
1040     } else {
1041         reset_color_pair(NCURSES_SP_ARG);
1042         if (old_pair < 0 && pair <= 0)
1043             return;
1044     }
1045
1046 #if NCURSES_EXT_FUNCS
1047     if (isDefaultColor(fg))
1048         fg = default_fg(NCURSES_SP_ARG);
1049     if (isDefaultColor(bg))
1050         bg = default_bg(NCURSES_SP_ARG);
1051 #endif
1052
1053     if (reverse) {
1054         int xx = fg;
1055         fg = bg;
1056         bg = xx;
1057     }
1058
1059     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
1060                      fg, bg));
1061
1062     if (!isDefaultColor(fg)) {
1063         set_foreground_color(NCURSES_SP_ARGx fg, outc);
1064     }
1065     if (!isDefaultColor(bg)) {
1066         set_background_color(NCURSES_SP_ARGx bg, outc);
1067     }
1068 #endif
1069 }
1070
1071 #if NCURSES_SP_FUNCS
1072 NCURSES_EXPORT(void)
1073 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
1074 {
1075     SetSafeOutcWrapper(outc);
1076     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
1077                                    old_pair,
1078                                    pair,
1079                                    reverse,
1080                                    _nc_outc_wrapper);
1081 }
1082 #endif
1083
1084 #if NCURSES_EXT_COLORS
1085 NCURSES_EXPORT(int)
1086 NCURSES_SP_NAME(init_extended_pair) (NCURSES_SP_DCLx int pair, int f, int b)
1087 {
1088     return _nc_init_pair(SP_PARM, pair, f, b);
1089 }
1090
1091 NCURSES_EXPORT(int)
1092 NCURSES_SP_NAME(init_extended_color) (NCURSES_SP_DCLx
1093                                       int color,
1094                                       int r, int g, int b)
1095 {
1096     return _nc_init_color(SP_PARM, color, r, g, b);
1097 }
1098
1099 NCURSES_EXPORT(int)
1100 NCURSES_SP_NAME(extended_color_content) (NCURSES_SP_DCLx
1101                                          int color,
1102                                          int *r, int *g, int *b)
1103 {
1104     return _nc_color_content(SP_PARM, color, r, g, b);
1105 }
1106
1107 NCURSES_EXPORT(int)
1108 NCURSES_SP_NAME(extended_pair_content) (NCURSES_SP_DCLx
1109                                         int pair,
1110                                         int *f, int *b)
1111 {
1112     return _nc_pair_content(SP_PARM, pair, f, b);
1113 }
1114
1115 NCURSES_EXPORT(void)
1116 NCURSES_SP_NAME(reset_color_pairs) (NCURSES_SP_DCL0)
1117 {
1118     if (SP_PARM != 0) {
1119         if (SP_PARM->_color_pairs) {
1120             _nc_free_ordered_pairs(SP_PARM);
1121             free(SP_PARM->_color_pairs);
1122             SP_PARM->_color_pairs = 0;
1123             SP_PARM->_pair_alloc = 0;
1124             ReservePairs(SP_PARM, 16);
1125             clearok(CurScreen(SP_PARM), TRUE);
1126             touchwin(StdScreen(SP_PARM));
1127         }
1128     }
1129 }
1130
1131 #if NCURSES_SP_FUNCS
1132 NCURSES_EXPORT(int)
1133 init_extended_pair(int pair, int f, int b)
1134 {
1135     return NCURSES_SP_NAME(init_extended_pair) (CURRENT_SCREEN, pair, f, b);
1136 }
1137
1138 NCURSES_EXPORT(int)
1139 init_extended_color(int color, int r, int g, int b)
1140 {
1141     return NCURSES_SP_NAME(init_extended_color) (CURRENT_SCREEN,
1142                                                  color,
1143                                                  r, g, b);
1144 }
1145
1146 NCURSES_EXPORT(int)
1147 extended_color_content(int color, int *r, int *g, int *b)
1148 {
1149     return NCURSES_SP_NAME(extended_color_content) (CURRENT_SCREEN,
1150                                                     color,
1151                                                     r, g, b);
1152 }
1153
1154 NCURSES_EXPORT(int)
1155 extended_pair_content(int pair, int *f, int *b)
1156 {
1157     return NCURSES_SP_NAME(extended_pair_content) (CURRENT_SCREEN, pair, f, b);
1158 }
1159
1160 NCURSES_EXPORT(void)
1161 reset_color_pairs(void)
1162 {
1163     NCURSES_SP_NAME(reset_color_pairs) (CURRENT_SCREEN);
1164 }
1165 #endif /* NCURSES_SP_FUNCS */
1166 #endif /* NCURSES_EXT_COLORS */