]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 5.6
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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  ****************************************************************************/
34
35 /* lib_color.c
36  *
37  * Handles color emulation of SYS V curses
38  */
39
40 #include <curses.priv.h>
41
42 #include <term.h>
43 #include <tic.h>
44
45 MODULE_ID("$Id: lib_color.c,v 1.80 2006/11/26 01:33:16 tom Exp $")
46
47 /*
48  * These should be screen structure members.  They need to be globals for
49  * historical reasons.  So we assign them in start_color() and also in
50  * set_term()'s screen-switching logic.
51  */
52 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
53 NCURSES_EXPORT_VAR(int) COLORS = 0;
54
55 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
56
57 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
58
59 #define OkColorHi(n)    (((n) < COLORS) && ((n) < max_colors))
60
61 /*
62  * Given a RGB range of 0..1000, we'll normally set the individual values
63  * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
64  */
65 #define RGB_ON  680
66 #define RGB_OFF 0
67 /* *INDENT-OFF* */
68 static const color_t cga_palette[] =
69 {
70     /*  R               G               B */
71     DATA(RGB_OFF,       RGB_OFF,        RGB_OFF),       /* COLOR_BLACK */
72     DATA(RGB_ON,        RGB_OFF,        RGB_OFF),       /* COLOR_RED */
73     DATA(RGB_OFF,       RGB_ON,         RGB_OFF),       /* COLOR_GREEN */
74     DATA(RGB_ON,        RGB_ON,         RGB_OFF),       /* COLOR_YELLOW */
75     DATA(RGB_OFF,       RGB_OFF,        RGB_ON),        /* COLOR_BLUE */
76     DATA(RGB_ON,        RGB_OFF,        RGB_ON),        /* COLOR_MAGENTA */
77     DATA(RGB_OFF,       RGB_ON,         RGB_ON),        /* COLOR_CYAN */
78     DATA(RGB_ON,        RGB_ON,         RGB_ON),        /* COLOR_WHITE */
79 };
80
81 static const color_t hls_palette[] =
82 {
83     /*          H       L       S */
84     DATA(       0,      0,      0),             /* COLOR_BLACK */
85     DATA(       120,    50,     100),           /* COLOR_RED */
86     DATA(       240,    50,     100),           /* COLOR_GREEN */
87     DATA(       180,    50,     100),           /* COLOR_YELLOW */
88     DATA(       330,    50,     100),           /* COLOR_BLUE */
89     DATA(       60,     50,     100),           /* COLOR_MAGENTA */
90     DATA(       300,    50,     100),           /* COLOR_CYAN */
91     DATA(       0,      50,     100),           /* COLOR_WHITE */
92 };
93 /* *INDENT-ON* */
94
95 #if NCURSES_EXT_FUNCS
96 /*
97  * These are called from _nc_do_color(), which in turn is called from
98  * vidattr - so we have to assume that SP may be null.
99  */
100 static int
101 default_fg(void)
102 {
103     return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
104 }
105
106 static int
107 default_bg(void)
108 {
109     return SP != 0 ? SP->_default_bg : COLOR_BLACK;
110 }
111 #else
112 #define default_fg() COLOR_WHITE
113 #define default_bg() COLOR_BLACK
114 #endif
115
116 /*
117  * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
118  * to maintain compatibility with a pre-ANSI scheme.  The same scheme is
119  * also used in the FreeBSD syscons.
120  */
121 static int
122 toggled_colors(int c)
123 {
124     if (c < 16) {
125         static const int table[] =
126         {0, 4, 2, 6, 1, 5, 3, 7,
127          8, 12, 10, 14, 9, 13, 11, 15};
128         c = table[c];
129     }
130     return c;
131 }
132
133 static void
134 set_background_color(int bg, int (*outc) (int))
135 {
136     if (set_a_background) {
137         TPUTS_TRACE("set_a_background");
138         tputs(TPARM_1(set_a_background, bg), 1, outc);
139     } else {
140         TPUTS_TRACE("set_background");
141         tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
142     }
143 }
144
145 static void
146 set_foreground_color(int fg, int (*outc) (int))
147 {
148     if (set_a_foreground) {
149         TPUTS_TRACE("set_a_foreground");
150         tputs(TPARM_1(set_a_foreground, fg), 1, outc);
151     } else {
152         TPUTS_TRACE("set_foreground");
153         tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
154     }
155 }
156
157 static void
158 init_color_table(void)
159 {
160     const color_t *tp;
161     int n;
162
163     tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
164     for (n = 0; n < COLORS; n++) {
165         if (n < 8) {
166             SP->_color_table[n] = tp[n];
167         } else {
168             SP->_color_table[n] = tp[n % 8];
169             if (hue_lightness_saturation) {
170                 SP->_color_table[n].green = 100;
171             } else {
172                 if (SP->_color_table[n].red)
173                     SP->_color_table[n].red = 1000;
174                 if (SP->_color_table[n].green)
175                     SP->_color_table[n].green = 1000;
176                 if (SP->_color_table[n].blue)
177                     SP->_color_table[n].blue = 1000;
178             }
179         }
180     }
181 }
182
183 /*
184  * Reset the color pair, e.g., to whatever color pair 0 is.
185  */
186 static bool
187 reset_color_pair(void)
188 {
189     bool result = FALSE;
190
191     if (orig_pair != 0) {
192         TPUTS_TRACE("orig_pair");
193         putp(orig_pair);
194         result = TRUE;
195     }
196     return result;
197 }
198
199 /*
200  * Reset color pairs and definitions.  Actually we do both more to accommodate
201  * badly-written terminal descriptions than for the relatively rare case where
202  * someone has changed the color definitions.
203  */
204 bool
205 _nc_reset_colors(void)
206 {
207     int result = FALSE;
208
209     T((T_CALLED("_nc_reset_colors()")));
210     if (SP->_color_defs > 0)
211         SP->_color_defs = -(SP->_color_defs);
212
213     if (reset_color_pair())
214         result = TRUE;
215     if (orig_colors != 0) {
216         TPUTS_TRACE("orig_colors");
217         putp(orig_colors);
218         result = TRUE;
219     }
220     returnBool(result);
221 }
222
223 NCURSES_EXPORT(int)
224 start_color(void)
225 {
226     int result = ERR;
227
228     T((T_CALLED("start_color()")));
229
230     if (SP == 0) {
231         result = ERR;
232     } else if (SP->_coloron) {
233         result = OK;
234     } else {
235
236         if (reset_color_pair() != TRUE) {
237             set_foreground_color(default_fg(), _nc_outch);
238             set_background_color(default_bg(), _nc_outch);
239         }
240
241         if (max_pairs > 0 && max_colors > 0) {
242             COLOR_PAIRS = SP->_pair_count = max_pairs;
243             COLORS = SP->_color_count = max_colors;
244
245             if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
246                                                 max_pairs)) != 0) {
247                 if ((SP->_color_table = TYPE_CALLOC(color_t,
248                                                     max_colors)) != 0) {
249                     SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
250                     init_color_table();
251
252                     T(("started color: COLORS = %d, COLOR_PAIRS = %d",
253                        COLORS, COLOR_PAIRS));
254
255                     SP->_coloron = 1;
256                     result = OK;
257                 } else if (SP->_color_pairs != 0) {
258                     FreeAndNull(SP->_color_pairs);
259                 }
260             }
261         } else {
262             result = OK;
263         }
264     }
265     returnCode(result);
266 }
267
268 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
269 static void
270 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
271 /* convert RGB to HLS system */
272 {
273     short min, max, t;
274
275     if ((min = g < r ? g : r) > b)
276         min = b;
277     if ((max = g > r ? g : r) < b)
278         max = b;
279
280     /* calculate lightness */
281     *l = (min + max) / 20;
282
283     if (min == max) {           /* black, white and all shades of gray */
284         *h = 0;
285         *s = 0;
286         return;
287     }
288
289     /* calculate saturation */
290     if (*l < 50)
291         *s = ((max - min) * 100) / (max + min);
292     else
293         *s = ((max - min) * 100) / (2000 - max - min);
294
295     /* calculate hue */
296     if (r == max)
297         t = 120 + ((g - b) * 60) / (max - min);
298     else if (g == max)
299         t = 240 + ((b - r) * 60) / (max - min);
300     else
301         t = 360 + ((r - g) * 60) / (max - min);
302
303     *h = t % 360;
304 }
305
306 /*
307  * Extension (1997/1/18) - Allow negative f/b values to set default color
308  * values.
309  */
310 NCURSES_EXPORT(int)
311 init_pair(short pair, short f, short b)
312 {
313     colorpair_t result;
314
315     T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
316
317     if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
318         returnCode(ERR);
319 #if NCURSES_EXT_FUNCS
320     if (SP->_default_color) {
321         if (f < 0)
322             f = COLOR_DEFAULT;
323         if (b < 0)
324             b = COLOR_DEFAULT;
325         if (!OkColorHi(f) && !isDefaultColor(f))
326             returnCode(ERR);
327         if (!OkColorHi(b) && !isDefaultColor(b))
328             returnCode(ERR);
329     } else
330 #endif
331     {
332         if ((f < 0) || !OkColorHi(f)
333             || (b < 0) || !OkColorHi(b)
334             || (pair < 1))
335             returnCode(ERR);
336     }
337
338     /*
339      * When a pair's content is changed, replace its colors (if pair was
340      * initialized before a screen update is performed replacing original
341      * pair colors with the new ones).
342      */
343     result = PAIR_OF(f, b);
344     if (SP->_color_pairs[pair] != 0
345         && SP->_color_pairs[pair] != result) {
346         int y, x;
347
348         for (y = 0; y <= curscr->_maxy; y++) {
349             struct ldat *ptr = &(curscr->_line[y]);
350             bool changed = FALSE;
351             for (x = 0; x <= curscr->_maxx; x++) {
352                 if (GetPair(ptr->text[x]) == pair) {
353                     /* Set the old cell to zero to ensure it will be
354                        updated on the next doupdate() */
355                     SetChar(ptr->text[x], 0, 0);
356                     CHANGED_CELL(ptr, x);
357                     changed = TRUE;
358                 }
359             }
360             if (changed)
361                 _nc_make_oldhash(y);
362         }
363     }
364     SP->_color_pairs[pair] = result;
365     if (GET_SCREEN_PAIR(SP) == pair)
366         SET_SCREEN_PAIR(SP, (chtype) (~0));     /* force attribute update */
367
368     if (initialize_pair) {
369         const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
370
371         T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
372            pair,
373            tp[f].red, tp[f].green, tp[f].blue,
374            tp[b].red, tp[b].green, tp[b].blue));
375
376         TPUTS_TRACE("initialize_pair");
377         putp(TPARM_7(initialize_pair,
378                      pair,
379                      tp[f].red, tp[f].green, tp[f].blue,
380                      tp[b].red, tp[b].green, tp[b].blue));
381     }
382
383     returnCode(OK);
384 }
385
386 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
387
388 NCURSES_EXPORT(int)
389 init_color(short color, short r, short g, short b)
390 {
391     int result = ERR;
392
393     T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
394
395     if (initialize_color != NULL
396         && SP != 0
397         && SP->_coloron
398         && (color >= 0 && OkColorHi(color))
399         && (okRGB(r) && okRGB(g) && okRGB(b))) {
400
401         SP->_color_table[color].init = 1;
402         SP->_color_table[color].r = r;
403         SP->_color_table[color].g = g;
404         SP->_color_table[color].b = b;
405
406         if (hue_lightness_saturation) {
407             rgb2hls(r, g, b,
408                     &SP->_color_table[color].red,
409                     &SP->_color_table[color].green,
410                     &SP->_color_table[color].blue);
411         } else {
412             SP->_color_table[color].red = r;
413             SP->_color_table[color].green = g;
414             SP->_color_table[color].blue = b;
415         }
416
417         TPUTS_TRACE("initialize_color");
418         putp(TPARM_4(initialize_color, color, r, g, b));
419         SP->_color_defs = max(color + 1, SP->_color_defs);
420         result = OK;
421     }
422     returnCode(result);
423 }
424
425 NCURSES_EXPORT(bool)
426 can_change_color(void)
427 {
428     T((T_CALLED("can_change_color()")));
429     returnCode((can_change != 0) ? TRUE : FALSE);
430 }
431
432 NCURSES_EXPORT(bool)
433 has_colors(void)
434 {
435     T((T_CALLED("has_colors()")));
436     returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
437                 && (((set_foreground != NULL)
438                      && (set_background != NULL))
439                     || ((set_a_foreground != NULL)
440                         && (set_a_background != NULL))
441                     || set_color_pair)) ? TRUE : FALSE);
442 }
443
444 NCURSES_EXPORT(int)
445 color_content(short color, short *r, short *g, short *b)
446 {
447     int result;
448
449     T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
450     if (color < 0 || !OkColorHi(color) || SP == 0 || !SP->_coloron) {
451         result = ERR;
452     } else {
453         NCURSES_COLOR_T c_r = SP->_color_table[color].red;
454         NCURSES_COLOR_T c_g = SP->_color_table[color].green;
455         NCURSES_COLOR_T c_b = SP->_color_table[color].blue;
456
457         if (r)
458             *r = c_r;
459         if (g)
460             *g = c_g;
461         if (b)
462             *b = c_b;
463
464         T(("...color_content(%d,%d,%d,%d)", color, c_r, c_g, c_b));
465         result = OK;
466     }
467     returnCode(result);
468 }
469
470 NCURSES_EXPORT(int)
471 pair_content(short pair, short *f, short *b)
472 {
473     int result;
474
475     T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
476
477     if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
478         result = ERR;
479     } else {
480         NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
481         NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
482
483 #if NCURSES_EXT_FUNCS
484         if (fg == COLOR_DEFAULT)
485             fg = -1;
486         if (bg == COLOR_DEFAULT)
487             bg = -1;
488 #endif
489
490         if (f)
491             *f = fg;
492         if (b)
493             *b = bg;
494
495         T(("...pair_content(%d,%d,%d)", pair, fg, bg));
496         result = OK;
497     }
498     returnCode(result);
499 }
500
501 NCURSES_EXPORT(void)
502 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
503 {
504     NCURSES_COLOR_T fg = COLOR_DEFAULT;
505     NCURSES_COLOR_T bg = COLOR_DEFAULT;
506     NCURSES_COLOR_T old_fg, old_bg;
507
508     if (pair < 0 || pair >= COLOR_PAIRS) {
509         return;
510     } else if (pair != 0) {
511         if (set_color_pair) {
512             TPUTS_TRACE("set_color_pair");
513             tputs(TPARM_1(set_color_pair, pair), 1, outc);
514             return;
515         } else if (SP != 0) {
516             pair_content((short) pair, &fg, &bg);
517         }
518     }
519
520     if (old_pair >= 0
521         && SP != 0
522         && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
523         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
524             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
525 #if NCURSES_EXT_FUNCS
526             /*
527              * A minor optimization - but extension.  If "AX" is specified in
528              * the terminal description, treat it as screen's indicator of ECMA
529              * SGR 39 and SGR 49, and assume the two sequences are independent.
530              */
531             if (SP->_has_sgr_39_49
532                 && isDefaultColor(old_bg)
533                 && !isDefaultColor(old_fg)) {
534                 tputs("\033[39m", 1, outc);
535             } else if (SP->_has_sgr_39_49
536                        && isDefaultColor(old_fg)
537                        && !isDefaultColor(old_bg)) {
538                 tputs("\033[49m", 1, outc);
539             } else
540 #endif
541                 reset_color_pair();
542         }
543     } else {
544         reset_color_pair();
545         if (old_pair < 0)
546             return;
547     }
548
549 #if NCURSES_EXT_FUNCS
550     if (isDefaultColor(fg))
551         fg = default_fg();
552     if (isDefaultColor(bg))
553         bg = default_bg();
554 #endif
555
556     if (reverse) {
557         NCURSES_COLOR_T xx = fg;
558         fg = bg;
559         bg = xx;
560     }
561
562     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
563                      fg, bg));
564
565     if (!isDefaultColor(fg)) {
566         set_foreground_color(fg, outc);
567     }
568     if (!isDefaultColor(bg)) {
569         set_background_color(bg, outc);
570     }
571 }