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