1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
37 * Handles color emulation of SYS V curses
40 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_color.c,v 1.80 2006/11/26 01:33:16 tom Exp $")
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.
52 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
53 NCURSES_EXPORT_VAR(int) COLORS = 0;
55 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
57 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
59 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
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.
68 static const color_t cga_palette[] =
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 */
81 static const color_t hls_palette[] =
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 */
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.
103 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
109 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
112 #define default_fg() COLOR_WHITE
113 #define default_bg() COLOR_BLACK
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.
122 toggled_colors(int c)
125 static const int table[] =
126 {0, 4, 2, 6, 1, 5, 3, 7,
127 8, 12, 10, 14, 9, 13, 11, 15};
134 set_background_color(int bg, int (*outc) (int))
136 if (set_a_background) {
137 TPUTS_TRACE("set_a_background");
138 tputs(TPARM_1(set_a_background, bg), 1, outc);
140 TPUTS_TRACE("set_background");
141 tputs(TPARM_1(set_background, toggled_colors(bg)), 1, outc);
146 set_foreground_color(int fg, int (*outc) (int))
148 if (set_a_foreground) {
149 TPUTS_TRACE("set_a_foreground");
150 tputs(TPARM_1(set_a_foreground, fg), 1, outc);
152 TPUTS_TRACE("set_foreground");
153 tputs(TPARM_1(set_foreground, toggled_colors(fg)), 1, outc);
158 init_color_table(void)
163 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
164 for (n = 0; n < COLORS; n++) {
166 SP->_color_table[n] = tp[n];
168 SP->_color_table[n] = tp[n % 8];
169 if (hue_lightness_saturation) {
170 SP->_color_table[n].green = 100;
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;
184 * Reset the color pair, e.g., to whatever color pair 0 is.
187 reset_color_pair(void)
191 if (orig_pair != 0) {
192 TPUTS_TRACE("orig_pair");
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.
205 _nc_reset_colors(void)
209 T((T_CALLED("_nc_reset_colors()")));
210 if (SP->_color_defs > 0)
211 SP->_color_defs = -(SP->_color_defs);
213 if (reset_color_pair())
215 if (orig_colors != 0) {
216 TPUTS_TRACE("orig_colors");
228 T((T_CALLED("start_color()")));
232 } else if (SP->_coloron) {
236 if (reset_color_pair() != TRUE) {
237 set_foreground_color(default_fg(), _nc_outch);
238 set_background_color(default_bg(), _nc_outch);
241 if (max_pairs > 0 && max_colors > 0) {
242 COLOR_PAIRS = SP->_pair_count = max_pairs;
243 COLORS = SP->_color_count = max_colors;
245 if ((SP->_color_pairs = TYPE_CALLOC(colorpair_t,
247 if ((SP->_color_table = TYPE_CALLOC(color_t,
249 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
252 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
253 COLORS, COLOR_PAIRS));
257 } else if (SP->_color_pairs != 0) {
258 FreeAndNull(SP->_color_pairs);
268 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
270 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
271 /* convert RGB to HLS system */
275 if ((min = g < r ? g : r) > b)
277 if ((max = g > r ? g : r) < b)
280 /* calculate lightness */
281 *l = (min + max) / 20;
283 if (min == max) { /* black, white and all shades of gray */
289 /* calculate saturation */
291 *s = ((max - min) * 100) / (max + min);
293 *s = ((max - min) * 100) / (2000 - max - min);
297 t = 120 + ((g - b) * 60) / (max - min);
299 t = 240 + ((b - r) * 60) / (max - min);
301 t = 360 + ((r - g) * 60) / (max - min);
307 * Extension (1997/1/18) - Allow negative f/b values to set default color
311 init_pair(short pair, short f, short b)
315 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
317 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron)
319 #if NCURSES_EXT_FUNCS
320 if (SP->_default_color) {
325 if (!OkColorHi(f) && !isDefaultColor(f))
327 if (!OkColorHi(b) && !isDefaultColor(b))
332 if ((f < 0) || !OkColorHi(f)
333 || (b < 0) || !OkColorHi(b)
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).
343 result = PAIR_OF(f, b);
344 if (SP->_color_pairs[pair] != 0
345 && SP->_color_pairs[pair] != result) {
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);
364 SP->_color_pairs[pair] = result;
365 if (GET_SCREEN_PAIR(SP) == pair)
366 SET_SCREEN_PAIR(SP, (chtype) (~0)); /* force attribute update */
368 if (initialize_pair) {
369 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
371 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
373 tp[f].red, tp[f].green, tp[f].blue,
374 tp[b].red, tp[b].green, tp[b].blue));
376 TPUTS_TRACE("initialize_pair");
377 putp(TPARM_7(initialize_pair,
379 tp[f].red, tp[f].green, tp[f].blue,
380 tp[b].red, tp[b].green, tp[b].blue));
386 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
389 init_color(short color, short r, short g, short b)
393 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
395 if (initialize_color != NULL
398 && (color >= 0 && OkColorHi(color))
399 && (okRGB(r) && okRGB(g) && okRGB(b))) {
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;
406 if (hue_lightness_saturation) {
408 &SP->_color_table[color].red,
409 &SP->_color_table[color].green,
410 &SP->_color_table[color].blue);
412 SP->_color_table[color].red = r;
413 SP->_color_table[color].green = g;
414 SP->_color_table[color].blue = b;
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);
426 can_change_color(void)
428 T((T_CALLED("can_change_color()")));
429 returnCode((can_change != 0) ? TRUE : FALSE);
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);
445 color_content(short color, short *r, short *g, short *b)
449 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
450 if (color < 0 || !OkColorHi(color) || SP == 0 || !SP->_coloron) {
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;
464 T(("...color_content(%d,%d,%d,%d)", color, c_r, c_g, c_b));
471 pair_content(short pair, short *f, short *b)
475 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
477 if ((pair < 0) || (pair >= COLOR_PAIRS) || SP == 0 || !SP->_coloron) {
480 NCURSES_COLOR_T fg = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
481 NCURSES_COLOR_T bg = (SP->_color_pairs[pair] & C_MASK);
483 #if NCURSES_EXT_FUNCS
484 if (fg == COLOR_DEFAULT)
486 if (bg == COLOR_DEFAULT)
495 T(("...pair_content(%d,%d,%d)", pair, fg, bg));
502 _nc_do_color(short old_pair, short pair, bool reverse, int (*outc) (int))
504 NCURSES_COLOR_T fg = COLOR_DEFAULT;
505 NCURSES_COLOR_T bg = COLOR_DEFAULT;
506 NCURSES_COLOR_T old_fg, old_bg;
508 if (pair < 0 || pair >= COLOR_PAIRS) {
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);
515 } else if (SP != 0) {
516 pair_content((short) pair, &fg, &bg);
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
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.
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);
549 #if NCURSES_EXT_FUNCS
550 if (isDefaultColor(fg))
552 if (isDefaultColor(bg))
557 NCURSES_COLOR_T xx = fg;
562 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
565 if (!isDefaultColor(fg)) {
566 set_foreground_color(fg, outc);
568 if (!isDefaultColor(bg)) {
569 set_background_color(bg, outc);