]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_color.c
ncurses 5.6 - patch 20070217
[ncurses.git] / ncurses / base / lib_color.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 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.83 2007/02/03 23:10:06 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         TR(TRACE_ATTRS,
372            ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
373             pair,
374             tp[f].red, tp[f].green, tp[f].blue,
375             tp[b].red, tp[b].green, tp[b].blue));
376
377         TPUTS_TRACE("initialize_pair");
378         putp(TPARM_7(initialize_pair,
379                      pair,
380                      tp[f].red, tp[f].green, tp[f].blue,
381                      tp[b].red, tp[b].green, tp[b].blue));
382     }
383
384     returnCode(OK);
385 }
386
387 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
388
389 NCURSES_EXPORT(int)
390 init_color(short color, short r, short g, short b)
391 {
392     int result = ERR;
393
394     T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
395
396     if (initialize_color != NULL
397         && SP != 0
398         && SP->_coloron
399         && (color >= 0 && OkColorHi(color))
400         && (okRGB(r) && okRGB(g) && okRGB(b))) {
401
402         SP->_color_table[color].init = 1;
403         SP->_color_table[color].r = r;
404         SP->_color_table[color].g = g;
405         SP->_color_table[color].b = b;
406
407         if (hue_lightness_saturation) {
408             rgb2hls(r, g, b,
409                     &SP->_color_table[color].red,
410                     &SP->_color_table[color].green,
411                     &SP->_color_table[color].blue);
412         } else {
413             SP->_color_table[color].red = r;
414             SP->_color_table[color].green = g;
415             SP->_color_table[color].blue = b;
416         }
417
418         TPUTS_TRACE("initialize_color");
419         putp(TPARM_4(initialize_color, color, r, g, b));
420         SP->_color_defs = max(color + 1, SP->_color_defs);
421         result = OK;
422     }
423     returnCode(result);
424 }
425
426 NCURSES_EXPORT(bool)
427 can_change_color(void)
428 {
429     T((T_CALLED("can_change_color()")));
430     returnCode((can_change != 0) ? TRUE : FALSE);
431 }
432
433 NCURSES_EXPORT(bool)
434 has_colors(void)
435 {
436     T((T_CALLED("has_colors()")));
437     returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
438                 && (((set_foreground != NULL)
439                      && (set_background != NULL))
440                     || ((set_a_foreground != NULL)
441                         && (set_a_background != NULL))
442                     || set_color_pair)) ? TRUE : FALSE);
443 }
444
445 NCURSES_EXPORT(int)
446 color_content(short color, short *r, short *g, short *b)
447 {
448     int result;
449
450     T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
451     if (color < 0 || !OkColorHi(color) || SP == 0 || !SP->_coloron) {
452         result = ERR;
453     } else {
454         NCURSES_COLOR_T c_r = SP->_color_table[color].red;
455         NCURSES_COLOR_T c_g = SP->_color_table[color].green;
456         NCURSES_COLOR_T c_b = SP->_color_table[color].blue;
457
458         if (r)
459             *r = c_r;
460         if (g)
461             *g = c_g;
462         if (b)
463             *b = c_b;
464
465         TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
466                          color, c_r, c_g, c_b));
467         result = OK;
468     }
469     returnCode(result);
470 }
471
472 NCURSES_EXPORT(int)
473 pair_content(short pair, short *f, short *b)
474 {
475     int result;
476
477     T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
478
479     if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
480         result = ERR;
481     } else {
482         NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
483         NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
484
485 #if NCURSES_EXT_FUNCS
486         if (fg == COLOR_DEFAULT)
487             fg = -1;
488         if (bg == COLOR_DEFAULT)
489             bg = -1;
490 #endif
491
492         if (f)
493             *f = fg;
494         if (b)
495             *b = bg;
496
497         TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
498         result = OK;
499     }
500     returnCode(result);
501 }
502
503 NCURSES_EXPORT(void)
504 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
505 {
506     NCURSES_COLOR_T fg = COLOR_DEFAULT;
507     NCURSES_COLOR_T bg = COLOR_DEFAULT;
508     NCURSES_COLOR_T old_fg, old_bg;
509
510     if (pair < 0 || pair >= COLOR_PAIRS) {
511         return;
512     } else if (pair != 0) {
513         if (set_color_pair) {
514             TPUTS_TRACE("set_color_pair");
515             tputs(TPARM_1(set_color_pair, pair), 1, outc);
516             return;
517         } else if (SP != 0) {
518             pair_content((short) pair, &fg, &bg);
519         }
520     }
521
522     if (old_pair >= 0
523         && SP != 0
524         && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
525         if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
526             || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
527 #if NCURSES_EXT_FUNCS
528             /*
529              * A minor optimization - but extension.  If "AX" is specified in
530              * the terminal description, treat it as screen's indicator of ECMA
531              * SGR 39 and SGR 49, and assume the two sequences are independent.
532              */
533             if (SP->_has_sgr_39_49
534                 && isDefaultColor(old_bg)
535                 && !isDefaultColor(old_fg)) {
536                 tputs("\033[39m", 1, outc);
537             } else if (SP->_has_sgr_39_49
538                        && isDefaultColor(old_fg)
539                        && !isDefaultColor(old_bg)) {
540                 tputs("\033[49m", 1, outc);
541             } else
542 #endif
543                 reset_color_pair();
544         }
545     } else {
546         reset_color_pair();
547         if (old_pair < 0)
548             return;
549     }
550
551 #if NCURSES_EXT_FUNCS
552     if (isDefaultColor(fg))
553         fg = default_fg();
554     if (isDefaultColor(bg))
555         bg = default_bg();
556 #endif
557
558     if (reverse) {
559         NCURSES_COLOR_T xx = fg;
560         fg = bg;
561         bg = xx;
562     }
563
564     TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
565                      fg, bg));
566
567     if (!isDefaultColor(fg)) {
568         set_foreground_color(fg, outc);
569     }
570     if (!isDefaultColor(bg)) {
571         set_background_color(bg, outc);
572     }
573 }