]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 6.1 - patch 20180303
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2017,2018 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.137 2018/03/01 15:02:12 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 static bool
251 init_direct_colors(NCURSES_SP_DCL0)
252 {
253     static NCURSES_CONST char name[] = "RGB";
254
255     int n;
256     const char *s;
257     int width;
258     rgb_bits_t *result = &(SP_PARM->_direct_color);
259
260     result->value = 0;
261
262     if (COLORS >= 8) {
263         /* find the number of bits needed for the maximum color value */
264         for (width = 0; (1 << width) - 1 < (COLORS - 1); ++width) {
265             ;
266         }
267
268         if ((n = tigetflag(name)) > 0) {
269             n = (width + 2) / 3;
270             result->bits.red = UChar(n);
271             result->bits.green = UChar(n);
272             result->bits.blue = UChar(width - (2 * n));
273         } else if ((n = tigetnum(name)) > 0) {
274             result->bits.red = UChar(n);
275             result->bits.green = UChar(n);
276             result->bits.blue = UChar(n);
277         } else if ((s = tigetstr(name)) != 0 && VALID_STRING(s)) {
278             int red = n;
279             int green = n;
280             int blue = width - (2 * n);
281
282             switch (sscanf(s, "%d/%d/%d", &red, &green, &blue)) {
283             default:
284                 blue = width - (2 * n);
285                 /* FALLTHRU */
286             case 1:
287                 green = n;
288                 /* FALLTHRU */
289             case 2:
290                 red = n;
291                 /* FALLTHRU */
292             case 3:
293                 /* okay */
294                 break;
295             }
296             result->bits.red = UChar(red);
297             result->bits.green = UChar(green);
298             result->bits.blue = UChar(blue);
299         }
300     }
301     return (result->value != 0);
302 }
303
304 /*
305  * Reset the color pair, e.g., to whatever color pair 0 is.
306  */
307 static bool
308 reset_color_pair(NCURSES_SP_DCL0)
309 {
310 #ifdef USE_TERM_DRIVER
311     return CallDriver(SP_PARM, td_rescol);
312 #else
313     bool result = FALSE;
314
315     (void) SP_PARM;
316     if (orig_pair != 0) {
317         (void) NCURSES_PUTP2("orig_pair", orig_pair);
318         result = TRUE;
319     }
320     return result;
321 #endif
322 }
323
324 /*
325  * Reset color pairs and definitions.  Actually we do both more to accommodate
326  * badly-written terminal descriptions than for the relatively rare case where
327  * someone has changed the color definitions.
328  */
329 NCURSES_EXPORT(bool)
330 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
331 {
332     int result = FALSE;
333
334     T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
335     if (SP_PARM->_color_defs > 0)
336         SP_PARM->_color_defs = -(SP_PARM->_color_defs);
337     if (reset_color_pair(NCURSES_SP_ARG))
338         result = TRUE;
339
340 #ifdef USE_TERM_DRIVER
341     result = CallDriver(SP_PARM, td_rescolors);
342 #else
343     if (orig_colors != 0) {
344         NCURSES_PUTP2("orig_colors", orig_colors);
345         result = TRUE;
346     }
347 #endif
348     returnBool(result);
349 }
350
351 #if NCURSES_SP_FUNCS
352 NCURSES_EXPORT(bool)
353 _nc_reset_colors(void)
354 {
355     return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
356 }
357 #endif
358
359 NCURSES_EXPORT(int)
360 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
361 {
362     int result = ERR;
363     int maxpairs = 0, maxcolors = 0;
364
365     T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
366
367     if (SP_PARM == 0) {
368         result = ERR;
369     } else if (SP_PARM->_coloron) {
370         result = OK;
371     } else {
372         maxpairs = MaxPairs;
373         maxcolors = MaxColors;
374         if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
375             set_foreground_color(NCURSES_SP_ARGx
376                                  default_fg(NCURSES_SP_ARG),
377                                  NCURSES_SP_NAME(_nc_outch));
378             set_background_color(NCURSES_SP_ARGx
379                                  default_bg(NCURSES_SP_ARG),
380                                  NCURSES_SP_NAME(_nc_outch));
381         }
382 #if !NCURSES_EXT_COLORS
383         /*
384          * Without ext-colors, we cannot represent more than 256 color pairs.
385          */
386         if (maxpairs > 256)
387             maxpairs = 256;
388 #endif
389
390         if (maxpairs > 0 && maxcolors > 0) {
391             SP_PARM->_pair_limit = maxpairs;
392
393 #if NCURSES_EXT_FUNCS
394             /*
395              * If using default colors, allocate extra space in table to
396              * allow for default-color as a component of a color-pair.
397              */
398             SP_PARM->_pair_limit += (1 + (2 * maxcolors));
399             SP_PARM->_pair_limit = limit_PAIRS(SP_PARM->_pair_limit);
400 #endif
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             _nc_reserve_pairs(SP_PARM, 16);
409             if (SP_PARM->_color_pairs != 0) {
410                 if (init_direct_colors(NCURSES_SP_ARG)) {
411                     result = OK;
412                 } else {
413                     SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
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(colorpair_t *)
518 _nc_reserve_pairs(SCREEN *sp, int want)
519 {
520     colorpair_t *result = 0;
521     int have = sp->_pair_alloc;
522
523     if ((sp->_color_pairs != 0) && (want < sp->_pair_alloc)) {
524         result = &(sp->_color_pairs[want]);
525     } else {
526
527         if (have == 0)
528             have = 1;
529         while (have <= want)
530             have *= 2;
531         if (have > sp->_pair_limit)
532             have = sp->_pair_limit;
533
534         if (sp->_color_pairs == 0) {
535             sp->_color_pairs = TYPE_CALLOC(colorpair_t, have);
536         } else if (have > sp->_pair_alloc) {
537 #if NCURSES_EXT_COLORS
538             colorpair_t *next;
539
540             if ((next = typeCalloc(colorpair_t, have)) == 0)
541                 _nc_err_abort(MSG_NO_MEMORY);
542             memcpy(next, sp->_color_pairs, (size_t) sp->_pair_alloc * sizeof(*next));
543             _nc_copy_pairs(sp, next, sp->_color_pairs, sp->_pair_alloc);
544             free(sp->_color_pairs);
545             sp->_color_pairs = next;
546 #else
547             TYPE_REALLOC(colorpair_t, have, sp->_color_pairs);
548             if (sp->_color_pairs != 0) {
549                 memset(sp->_color_pairs + sp->_pair_alloc, 0,
550                        sizeof(colorpair_t) * (size_t) (have - sp->_pair_alloc));
551             }
552 #endif
553         }
554         if (sp->_color_pairs != 0) {
555             sp->_pair_alloc = have;
556             result = &(sp->_color_pairs[want]);
557         }
558     }
559     return result;
560 }
561
562 /*
563  * Extension (1997/1/18) - Allow negative f/b values to set default color
564  * values.
565  */
566 NCURSES_EXPORT(int)
567 _nc_init_pair(SCREEN *sp, int pair, int f, int b)
568 {
569     static colorpair_t null_pair;
570     colorpair_t result = null_pair;
571     colorpair_t previous;
572     int maxcolors;
573
574     T((T_CALLED("init_pair(%p,%d,%d,%d)"), (void *) sp, pair, f, b));
575
576     if (!ValidPair(sp, pair))
577         returnCode(ERR);
578
579     maxcolors = MaxColors;
580
581     _nc_reserve_pairs(sp, pair);
582     previous = sp->_color_pairs[pair];
583 #if NCURSES_EXT_FUNCS
584     if (sp->_default_color || sp->_assumed_color) {
585         bool isDefault = FALSE;
586         bool wasDefault = FALSE;
587         int default_pairs = sp->_default_pairs;
588
589         /*
590          * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
591          * internal unsigned values which we will store in the _color_pairs[]
592          * table.
593          */
594         if (isDefaultColor(f)) {
595             f = COLOR_DEFAULT;
596             isDefault = TRUE;
597         } else if (!OkColorHi(f)) {
598             returnCode(ERR);
599         }
600
601         if (isDefaultColor(b)) {
602             b = COLOR_DEFAULT;
603             isDefault = TRUE;
604         } else if (!OkColorHi(b)) {
605             returnCode(ERR);
606         }
607
608         /*
609          * Check if the table entry that we are going to init/update used
610          * default colors.
611          */
612         if (isDefaultColor(FORE_OF(previous))
613             || isDefaultColor(BACK_OF(previous)))
614             wasDefault = TRUE;
615
616         /*
617          * Keep track of the number of entries in the color pair table which
618          * used a default color.
619          */
620         if (isDefault && !wasDefault) {
621             ++default_pairs;
622         } else if (wasDefault && !isDefault) {
623             --default_pairs;
624         }
625
626         /*
627          * As an extension, ncurses allows the pair number to exceed the
628          * terminal's color_pairs value for pairs using a default color.
629          *
630          * Note that updating a pair which used a default color with one
631          * that does not will decrement the count - and possibly interfere
632          * with sequentially adding new pairs.
633          */
634         if (pair > (sp->_pair_count + default_pairs)) {
635             returnCode(ERR);
636         }
637         sp->_default_pairs = default_pairs;
638     } else
639 #endif
640     {
641         if ((f < 0) || !OkColorHi(f)
642             || (b < 0) || !OkColorHi(b)
643             || (pair < 1)) {
644             returnCode(ERR);
645         }
646     }
647
648     /*
649      * When a pair's content is changed, replace its colors (if pair was
650      * initialized before a screen update is performed replacing original
651      * pair colors with the new ones).
652      */
653     MakeColorPair(result, f, b);
654     if ((FORE_OF(previous) != 0
655          || BACK_OF(previous) != 0)
656         && !isSamePair(previous, result)) {
657         _nc_change_pair(sp, pair);
658     }
659
660     _nc_reset_color_pair(sp, pair, &result);
661     sp->_color_pairs[pair] = result;
662     _nc_set_color_pair(sp, pair, cpINIT);
663
664     if (GET_SCREEN_PAIR(sp) == pair)
665         SET_SCREEN_PAIR(sp, (int) (~0));        /* force attribute update */
666
667 #ifdef USE_TERM_DRIVER
668     CallDriver_3(sp, td_initpair, pair, f, b);
669 #else
670     if (initialize_pair && InPalette(f) && InPalette(b)) {
671         const color_t *tp = DefaultPalette;
672
673         TR(TRACE_ATTRS,
674            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
675             (int) pair,
676             (int) tp[f].red, (int) tp[f].green, (int) tp[f].blue,
677             (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
678
679         NCURSES_PUTP2("initialize_pair",
680                       TPARM_7(initialize_pair,
681                               pair,
682                               (int) tp[f].red,
683                               (int) tp[f].green,
684                               (int) tp[f].blue,
685                               (int) tp[b].red,
686                               (int) tp[b].green,
687                               (int) tp[b].blue));
688     }
689 #endif
690
691     returnCode(OK);
692 }
693
694 NCURSES_EXPORT(int)
695 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx
696                             NCURSES_PAIRS_T pair,
697                             NCURSES_COLOR_T f,
698                             NCURSES_COLOR_T b)
699 {
700     return _nc_init_pair(SP_PARM, pair, f, b);
701 }
702
703 #if NCURSES_SP_FUNCS
704 NCURSES_EXPORT(int)
705 init_pair(NCURSES_COLOR_T pair, NCURSES_COLOR_T f, NCURSES_COLOR_T b)
706 {
707     return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
708 }
709 #endif
710
711 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
712
713 NCURSES_EXPORT(int)
714 _nc_init_color(SCREEN *sp, int color, int r, int g, int b)
715 {
716     int result = ERR;
717     int maxcolors;
718
719     T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
720        (void *) sp,
721        color,
722        r, g, b));
723
724     if (sp == 0 || sp->_direct_color.value)
725         returnCode(result);
726
727     maxcolors = MaxColors;
728
729     if (InitColor
730         && sp->_coloron
731         && (color >= 0 && OkColorHi(color))
732         && (okRGB(r) && okRGB(g) && okRGB(b))) {
733
734         sp->_color_table[color].init = 1;
735         sp->_color_table[color].r = r;
736         sp->_color_table[color].g = g;
737         sp->_color_table[color].b = b;
738
739         if (UseHlsPalette) {
740             rgb2hls(r, g, b,
741                     &sp->_color_table[color].red,
742                     &sp->_color_table[color].green,
743                     &sp->_color_table[color].blue);
744         } else {
745             sp->_color_table[color].red = r;
746             sp->_color_table[color].green = g;
747             sp->_color_table[color].blue = b;
748         }
749
750 #ifdef USE_TERM_DRIVER
751         CallDriver_4(sp, td_initcolor, color, r, g, b);
752 #else
753         NCURSES_PUTP2("initialize_color",
754                       TPARM_4(initialize_color, color, r, g, b));
755 #endif
756         sp->_color_defs = max(color + 1, sp->_color_defs);
757
758         result = OK;
759     }
760     returnCode(result);
761 }
762
763 NCURSES_EXPORT(int)
764 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
765                              NCURSES_COLOR_T color,
766                              NCURSES_COLOR_T r,
767                              NCURSES_COLOR_T g,
768                              NCURSES_COLOR_T b)
769 {
770     return _nc_init_color(SP_PARM, color, r, g, b);
771 }
772
773 #if NCURSES_SP_FUNCS
774 NCURSES_EXPORT(int)
775 init_color(NCURSES_COLOR_T color,
776            NCURSES_COLOR_T r,
777            NCURSES_COLOR_T g,
778            NCURSES_COLOR_T b)
779 {
780     return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
781 }
782 #endif
783
784 NCURSES_EXPORT(bool)
785 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
786 {
787     int result = FALSE;
788
789     T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
790
791     if (HasTerminal(SP_PARM) && (CanChange != 0)) {
792         result = TRUE;
793     }
794
795     returnCode(result);
796 }
797
798 #if NCURSES_SP_FUNCS
799 NCURSES_EXPORT(bool)
800 can_change_color(void)
801 {
802     return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
803 }
804 #endif
805
806 NCURSES_EXPORT(bool)
807 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
808 {
809     int code = FALSE;
810
811     (void) SP_PARM;
812     T((T_CALLED("has_colors()")));
813     if (HasTerminal(SP_PARM)) {
814 #ifdef USE_TERM_DRIVER
815         code = HasColor;
816 #else
817         code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
818                  && (((set_foreground != NULL)
819                       && (set_background != NULL))
820                      || ((set_a_foreground != NULL)
821                          && (set_a_background != NULL))
822                      || set_color_pair)) ? TRUE : FALSE);
823 #endif
824     }
825     returnCode(code);
826 }
827
828 #if NCURSES_SP_FUNCS
829 NCURSES_EXPORT(bool)
830 has_colors(void)
831 {
832     return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
833 }
834 #endif
835
836 static int
837 _nc_color_content(SCREEN *sp, int color, int *r, int *g, int *b)
838 {
839     int result = ERR;
840     int maxcolors;
841
842     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
843        (void *) sp,
844        color,
845        (void *) r,
846        (void *) g,
847        (void *) b));
848
849     if (sp == 0)
850         returnCode(result);
851
852     maxcolors = MaxColors;
853
854     if (color < 0 || !OkColorHi(color) || !sp->_coloron) {
855         result = ERR;
856     } else {
857         int c_r, c_g, c_b;
858
859         if (sp->_direct_color.value) {
860             rgb_bits_t *work = &(sp->_direct_color);
861
862 #define max_direct_color(name)  ((1 << work->bits.name) - 1)
863 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
864
865             int max_r = max_direct_color(red);
866             int max_g = max_direct_color(green);
867             int max_b = max_direct_color(blue);
868
869             int bitoff = 0;
870
871             c_b = value_direct_color(max_b);
872             bitoff += work->bits.blue;
873
874             c_g = value_direct_color(max_g);
875             bitoff += work->bits.green;
876
877             c_r = value_direct_color(max_r);
878
879         } else {
880             c_r = sp->_color_table[color].red;
881             c_g = sp->_color_table[color].green;
882             c_b = sp->_color_table[color].blue;
883         }
884
885         if (r)
886             *r = c_r;
887         if (g)
888             *g = c_g;
889         if (b)
890             *b = c_b;
891
892         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
893                          color, c_r, c_g, c_b));
894         result = OK;
895     }
896     returnCode(result);
897 }
898
899 NCURSES_EXPORT(int)
900 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
901                                 NCURSES_COLOR_T color,
902                                 NCURSES_COLOR_T *r,
903                                 NCURSES_COLOR_T *g,
904                                 NCURSES_COLOR_T *b)
905 {
906     int my_r, my_g, my_b;
907     int rc = _nc_color_content(SP_PARM, color, &my_r, &my_g, &my_b);
908     if (rc == OK) {
909         *r = limit_COLOR(my_r);
910         *g = limit_COLOR(my_g);
911         *b = limit_COLOR(my_b);
912     }
913     return rc;
914 }
915
916 #if NCURSES_SP_FUNCS
917 NCURSES_EXPORT(int)
918 color_content(NCURSES_COLOR_T color,
919               NCURSES_COLOR_T *r,
920               NCURSES_COLOR_T *g,
921               NCURSES_COLOR_T *b)
922 {
923     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
924 }
925 #endif
926
927 NCURSES_EXPORT(int)
928 _nc_pair_content(SCREEN *sp, int pair, int *f, int *b)
929 {
930     int result;
931
932     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
933        (void *) sp,
934        (int) pair,
935        (void *) f,
936        (void *) b));
937
938     if (!ValidPair(sp, pair)) {
939         result = ERR;
940     } else {
941         int fg = FORE_OF(sp->_color_pairs[pair]);
942         int bg = BACK_OF(sp->_color_pairs[pair]);
943
944 #if NCURSES_EXT_FUNCS
945         if (isDefaultColor(fg))
946             fg = -1;
947         if (isDefaultColor(bg))
948             bg = -1;
949 #endif
950
951         if (f)
952             *f = fg;
953         if (b)
954             *b = bg;
955
956         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
957                          (void *) sp,
958                          (int) pair,
959                          (int) fg, (int) bg));
960         result = OK;
961     }
962     returnCode(result);
963 }
964
965 NCURSES_EXPORT(int)
966 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
967                                NCURSES_PAIRS_T pair,
968                                NCURSES_COLOR_T *f,
969                                NCURSES_COLOR_T *b)
970 {
971     int my_f, my_b;
972     int rc = _nc_pair_content(SP_PARM, pair, &my_f, &my_b);
973     if (rc == OK) {
974         *f = limit_COLOR(my_f);
975         *b = limit_COLOR(my_b);
976     }
977     return rc;
978 }
979
980 #if NCURSES_SP_FUNCS
981 NCURSES_EXPORT(int)
982 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
983 {
984     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
985 }
986 #endif
987
988 NCURSES_EXPORT(void)
989 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
990                                int old_pair,
991                                int pair,
992                                int reverse,
993                                NCURSES_SP_OUTC outc)
994 {
995 #ifdef USE_TERM_DRIVER
996     CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
997 #else
998     int fg = COLOR_DEFAULT;
999     int bg = COLOR_DEFAULT;
1000     int old_fg = -1;
1001     int old_bg = -1;
1002
1003     if (!ValidPair(SP_PARM, pair)) {
1004         return;
1005     } else if (pair != 0) {
1006         if (set_color_pair) {
1007             TPUTS_TRACE("set_color_pair");
1008             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1009                                     TPARM_1(set_color_pair, pair),
1010                                     1, outc);
1011             return;
1012         } else if (SP_PARM != 0) {
1013             if (_nc_pair_content(SP_PARM, pair, &fg, &bg) == ERR)
1014                 return;
1015         }
1016     }
1017
1018     if (old_pair >= 0
1019         && SP_PARM != 0
1020         && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
1021         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
1022             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
1023 #if NCURSES_EXT_FUNCS
1024             /*
1025              * A minor optimization - but extension.  If "AX" is specified in
1026              * the terminal description, treat it as screen's indicator of ECMA
1027              * SGR 39 and SGR 49, and assume the two sequences are independent.
1028              */
1029             if (SP_PARM->_has_sgr_39_49
1030                 && isDefaultColor(old_bg)
1031                 && !isDefaultColor(old_fg)) {
1032                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
1033             } else if (SP_PARM->_has_sgr_39_49
1034                        && isDefaultColor(old_fg)
1035                        && !isDefaultColor(old_bg)) {
1036                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
1037             } else
1038 #endif
1039                 reset_color_pair(NCURSES_SP_ARG);
1040         }
1041     } else {
1042         reset_color_pair(NCURSES_SP_ARG);
1043         if (old_pair < 0 && pair <= 0)
1044             return;
1045     }
1046
1047 #if NCURSES_EXT_FUNCS
1048     if (isDefaultColor(fg))
1049         fg = default_fg(NCURSES_SP_ARG);
1050     if (isDefaultColor(bg))
1051         bg = default_bg(NCURSES_SP_ARG);
1052 #endif
1053
1054     if (reverse) {
1055         int xx = fg;
1056         fg = bg;
1057         bg = xx;
1058     }
1059
1060     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
1061                      fg, bg));
1062
1063     if (!isDefaultColor(fg)) {
1064         set_foreground_color(NCURSES_SP_ARGx fg, outc);
1065     }
1066     if (!isDefaultColor(bg)) {
1067         set_background_color(NCURSES_SP_ARGx bg, outc);
1068     }
1069 #endif
1070 }
1071
1072 #if NCURSES_SP_FUNCS
1073 NCURSES_EXPORT(void)
1074 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
1075 {
1076     SetSafeOutcWrapper(outc);
1077     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
1078                                    old_pair,
1079                                    pair,
1080                                    reverse,
1081                                    _nc_outc_wrapper);
1082 }
1083 #endif
1084
1085 #if NCURSES_EXT_COLORS
1086 NCURSES_EXPORT(int)
1087 NCURSES_SP_NAME(init_extended_pair) (NCURSES_SP_DCLx int pair, int f, int b)
1088 {
1089     return _nc_init_pair(SP_PARM, pair, f, b);
1090 }
1091
1092 NCURSES_EXPORT(int)
1093 NCURSES_SP_NAME(init_extended_color) (NCURSES_SP_DCLx
1094                                       int color,
1095                                       int r, int g, int b)
1096 {
1097     return _nc_init_color(SP_PARM, color, r, g, b);
1098 }
1099
1100 NCURSES_EXPORT(int)
1101 NCURSES_SP_NAME(extended_color_content) (NCURSES_SP_DCLx
1102                                          int color,
1103                                          int *r, int *g, int *b)
1104 {
1105     return _nc_color_content(SP_PARM, color, r, g, b);
1106 }
1107
1108 NCURSES_EXPORT(int)
1109 NCURSES_SP_NAME(extended_pair_content) (NCURSES_SP_DCLx
1110                                         int pair,
1111                                         int *f, int *b)
1112 {
1113     return _nc_pair_content(SP_PARM, pair, f, b);
1114 }
1115
1116 NCURSES_EXPORT(void)
1117 NCURSES_SP_NAME(reset_color_pairs) (NCURSES_SP_DCL0)
1118 {
1119     if (SP_PARM != 0) {
1120         if (SP_PARM->_color_pairs) {
1121             _nc_free_ordered_pairs(SP_PARM);
1122             free(SP_PARM->_color_pairs);
1123             SP_PARM->_color_pairs = 0;
1124             SP_PARM->_pair_alloc = 0;
1125             _nc_reserve_pairs(SP_PARM, 16);
1126             clearok(CurScreen(SP_PARM), TRUE);
1127             touchwin(StdScreen(SP_PARM));
1128         }
1129     }
1130 }
1131
1132 #if NCURSES_SP_FUNCS
1133 NCURSES_EXPORT(int)
1134 init_extended_pair(int pair, int f, int b)
1135 {
1136     return NCURSES_SP_NAME(init_extended_pair) (CURRENT_SCREEN, pair, f, b);
1137 }
1138
1139 NCURSES_EXPORT(int)
1140 init_extended_color(int color, int r, int g, int b)
1141 {
1142     return NCURSES_SP_NAME(init_extended_color) (CURRENT_SCREEN,
1143                                                  color,
1144                                                  r, g, b);
1145 }
1146
1147 NCURSES_EXPORT(int)
1148 extended_color_content(int color, int *r, int *g, int *b)
1149 {
1150     return NCURSES_SP_NAME(extended_color_content) (CURRENT_SCREEN,
1151                                                     color,
1152                                                     r, g, b);
1153 }
1154
1155 NCURSES_EXPORT(int)
1156 extended_pair_content(int pair, int *f, int *b)
1157 {
1158     return NCURSES_SP_NAME(extended_pair_content) (CURRENT_SCREEN, pair, f, b);
1159 }
1160
1161 NCURSES_EXPORT(void)
1162 reset_color_pairs(void)
1163 {
1164     NCURSES_SP_NAME(reset_color_pairs) (CURRENT_SCREEN);
1165 }
1166 #endif /* NCURSES_SP_FUNCS */
1167 #endif /* NCURSES_EXT_COLORS */