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