]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
df8424b41107aa9d8f8a7b791d3871e297105f4e
[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.139 2018/12/29 20:10:07 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             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                     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(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         sp->_color_pairs = TYPE_CALLOC(colorpair_t, have);
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                       TPARM_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                       TPARM_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()")));
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     int maxcolors;
833
834     T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
835        (void *) sp,
836        color,
837        (void *) r,
838        (void *) g,
839        (void *) b));
840
841     if (sp == 0)
842         returnCode(result);
843
844     maxcolors = MaxColors;
845
846     if (color < 0 || !OkColorHi(color) || !sp->_coloron) {
847         result = ERR;
848     } else {
849         int c_r, c_g, c_b;
850
851         if (sp->_direct_color.value) {
852             rgb_bits_t *work = &(sp->_direct_color);
853
854 #define max_direct_color(name)  ((1 << work->bits.name) - 1)
855 #define value_direct_color(max) (1000 * ((color >> bitoff) & max)) / max
856
857             int max_r = max_direct_color(red);
858             int max_g = max_direct_color(green);
859             int max_b = max_direct_color(blue);
860
861             int bitoff = 0;
862
863             c_b = value_direct_color(max_b);
864             bitoff += work->bits.blue;
865
866             c_g = value_direct_color(max_g);
867             bitoff += work->bits.green;
868
869             c_r = value_direct_color(max_r);
870
871         } else {
872             c_r = sp->_color_table[color].red;
873             c_g = sp->_color_table[color].green;
874             c_b = sp->_color_table[color].blue;
875         }
876
877         if (r)
878             *r = c_r;
879         if (g)
880             *g = c_g;
881         if (b)
882             *b = c_b;
883
884         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
885                          color, c_r, c_g, c_b));
886         result = OK;
887     }
888     returnCode(result);
889 }
890
891 NCURSES_EXPORT(int)
892 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
893                                 NCURSES_COLOR_T color,
894                                 NCURSES_COLOR_T *r,
895                                 NCURSES_COLOR_T *g,
896                                 NCURSES_COLOR_T *b)
897 {
898     int my_r, my_g, my_b;
899     int rc = _nc_color_content(SP_PARM, color, &my_r, &my_g, &my_b);
900     if (rc == OK) {
901         *r = limit_COLOR(my_r);
902         *g = limit_COLOR(my_g);
903         *b = limit_COLOR(my_b);
904     }
905     return rc;
906 }
907
908 #if NCURSES_SP_FUNCS
909 NCURSES_EXPORT(int)
910 color_content(NCURSES_COLOR_T color,
911               NCURSES_COLOR_T *r,
912               NCURSES_COLOR_T *g,
913               NCURSES_COLOR_T *b)
914 {
915     return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
916 }
917 #endif
918
919 NCURSES_EXPORT(int)
920 _nc_pair_content(SCREEN *sp, int pair, int *f, int *b)
921 {
922     int result;
923
924     T((T_CALLED("pair_content(%p,%d,%p,%p)"),
925        (void *) sp,
926        (int) pair,
927        (void *) f,
928        (void *) b));
929
930     if (!ValidPair(sp, pair)) {
931         result = ERR;
932     } else {
933         int fg;
934         int bg;
935
936         ReservePairs(sp, pair);
937         fg = FORE_OF(sp->_color_pairs[pair]);
938         bg = BACK_OF(sp->_color_pairs[pair]);
939 #if NCURSES_EXT_FUNCS
940         if (isDefaultColor(fg))
941             fg = -1;
942         if (isDefaultColor(bg))
943             bg = -1;
944 #endif
945
946         if (f)
947             *f = fg;
948         if (b)
949             *b = bg;
950
951         TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
952                          (void *) sp,
953                          (int) pair,
954                          (int) fg, (int) bg));
955         result = OK;
956     }
957     returnCode(result);
958 }
959
960 NCURSES_EXPORT(int)
961 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
962                                NCURSES_PAIRS_T pair,
963                                NCURSES_COLOR_T *f,
964                                NCURSES_COLOR_T *b)
965 {
966     int my_f, my_b;
967     int rc = _nc_pair_content(SP_PARM, pair, &my_f, &my_b);
968     if (rc == OK) {
969         *f = limit_COLOR(my_f);
970         *b = limit_COLOR(my_b);
971     }
972     return rc;
973 }
974
975 #if NCURSES_SP_FUNCS
976 NCURSES_EXPORT(int)
977 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
978 {
979     return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
980 }
981 #endif
982
983 NCURSES_EXPORT(void)
984 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
985                                int old_pair,
986                                int pair,
987                                int reverse,
988                                NCURSES_SP_OUTC outc)
989 {
990 #ifdef USE_TERM_DRIVER
991     CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
992 #else
993     int fg = COLOR_DEFAULT;
994     int bg = COLOR_DEFAULT;
995     int old_fg = -1;
996     int old_bg = -1;
997
998     if (!ValidPair(SP_PARM, pair)) {
999         return;
1000     } else if (pair != 0) {
1001         if (set_color_pair) {
1002             TPUTS_TRACE("set_color_pair");
1003             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
1004                                     TPARM_1(set_color_pair, pair),
1005                                     1, outc);
1006             return;
1007         } else if (SP_PARM != 0) {
1008             if (_nc_pair_content(SP_PARM, pair, &fg, &bg) == ERR)
1009                 return;
1010         }
1011     }
1012
1013     if (old_pair >= 0
1014         && SP_PARM != 0
1015         && _nc_pair_content(SP_PARM, old_pair, &old_fg, &old_bg) != ERR) {
1016         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
1017             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
1018 #if NCURSES_EXT_FUNCS
1019             /*
1020              * A minor optimization - but extension.  If "AX" is specified in
1021              * the terminal description, treat it as screen's indicator of ECMA
1022              * SGR 39 and SGR 49, and assume the two sequences are independent.
1023              */
1024             if (SP_PARM->_has_sgr_39_49
1025                 && isDefaultColor(old_bg)
1026                 && !isDefaultColor(old_fg)) {
1027                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
1028             } else if (SP_PARM->_has_sgr_39_49
1029                        && isDefaultColor(old_fg)
1030                        && !isDefaultColor(old_bg)) {
1031                 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
1032             } else
1033 #endif
1034                 reset_color_pair(NCURSES_SP_ARG);
1035         }
1036     } else {
1037         reset_color_pair(NCURSES_SP_ARG);
1038         if (old_pair < 0 && pair <= 0)
1039             return;
1040     }
1041
1042 #if NCURSES_EXT_FUNCS
1043     if (isDefaultColor(fg))
1044         fg = default_fg(NCURSES_SP_ARG);
1045     if (isDefaultColor(bg))
1046         bg = default_bg(NCURSES_SP_ARG);
1047 #endif
1048
1049     if (reverse) {
1050         int xx = fg;
1051         fg = bg;
1052         bg = xx;
1053     }
1054
1055     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
1056                      fg, bg));
1057
1058     if (!isDefaultColor(fg)) {
1059         set_foreground_color(NCURSES_SP_ARGx fg, outc);
1060     }
1061     if (!isDefaultColor(bg)) {
1062         set_background_color(NCURSES_SP_ARGx bg, outc);
1063     }
1064 #endif
1065 }
1066
1067 #if NCURSES_SP_FUNCS
1068 NCURSES_EXPORT(void)
1069 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
1070 {
1071     SetSafeOutcWrapper(outc);
1072     NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
1073                                    old_pair,
1074                                    pair,
1075                                    reverse,
1076                                    _nc_outc_wrapper);
1077 }
1078 #endif
1079
1080 #if NCURSES_EXT_COLORS
1081 NCURSES_EXPORT(int)
1082 NCURSES_SP_NAME(init_extended_pair) (NCURSES_SP_DCLx int pair, int f, int b)
1083 {
1084     return _nc_init_pair(SP_PARM, pair, f, b);
1085 }
1086
1087 NCURSES_EXPORT(int)
1088 NCURSES_SP_NAME(init_extended_color) (NCURSES_SP_DCLx
1089                                       int color,
1090                                       int r, int g, int b)
1091 {
1092     return _nc_init_color(SP_PARM, color, r, g, b);
1093 }
1094
1095 NCURSES_EXPORT(int)
1096 NCURSES_SP_NAME(extended_color_content) (NCURSES_SP_DCLx
1097                                          int color,
1098                                          int *r, int *g, int *b)
1099 {
1100     return _nc_color_content(SP_PARM, color, r, g, b);
1101 }
1102
1103 NCURSES_EXPORT(int)
1104 NCURSES_SP_NAME(extended_pair_content) (NCURSES_SP_DCLx
1105                                         int pair,
1106                                         int *f, int *b)
1107 {
1108     return _nc_pair_content(SP_PARM, pair, f, b);
1109 }
1110
1111 NCURSES_EXPORT(void)
1112 NCURSES_SP_NAME(reset_color_pairs) (NCURSES_SP_DCL0)
1113 {
1114     if (SP_PARM != 0) {
1115         if (SP_PARM->_color_pairs) {
1116             _nc_free_ordered_pairs(SP_PARM);
1117             free(SP_PARM->_color_pairs);
1118             SP_PARM->_color_pairs = 0;
1119             SP_PARM->_pair_alloc = 0;
1120             ReservePairs(SP_PARM, 16);
1121             clearok(CurScreen(SP_PARM), TRUE);
1122             touchwin(StdScreen(SP_PARM));
1123         }
1124     }
1125 }
1126
1127 #if NCURSES_SP_FUNCS
1128 NCURSES_EXPORT(int)
1129 init_extended_pair(int pair, int f, int b)
1130 {
1131     return NCURSES_SP_NAME(init_extended_pair) (CURRENT_SCREEN, pair, f, b);
1132 }
1133
1134 NCURSES_EXPORT(int)
1135 init_extended_color(int color, int r, int g, int b)
1136 {
1137     return NCURSES_SP_NAME(init_extended_color) (CURRENT_SCREEN,
1138                                                  color,
1139                                                  r, g, b);
1140 }
1141
1142 NCURSES_EXPORT(int)
1143 extended_color_content(int color, int *r, int *g, int *b)
1144 {
1145     return NCURSES_SP_NAME(extended_color_content) (CURRENT_SCREEN,
1146                                                     color,
1147                                                     r, g, b);
1148 }
1149
1150 NCURSES_EXPORT(int)
1151 extended_pair_content(int pair, int *f, int *b)
1152 {
1153     return NCURSES_SP_NAME(extended_pair_content) (CURRENT_SCREEN, pair, f, b);
1154 }
1155
1156 NCURSES_EXPORT(void)
1157 reset_color_pairs(void)
1158 {
1159     NCURSES_SP_NAME(reset_color_pairs) (CURRENT_SCREEN);
1160 }
1161 #endif /* NCURSES_SP_FUNCS */
1162 #endif /* NCURSES_EXT_COLORS */