1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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 ****************************************************************************/
36 * Handles color emulation of SYS V curses
39 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_color.c,v 1.62 2002/09/28 20:40:05 tom Exp $")
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.
51 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
52 NCURSES_EXPORT_VAR(int) COLORS = 0;
54 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
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.
63 static const color_t cga_palette[] =
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 */
76 static const color_t hls_palette[] =
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 */
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.
98 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
104 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
107 #define default_fg() COLOR_WHITE
108 #define default_bg() COLOR_BLACK
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.
117 toggled_colors(int c)
120 static const int table[] =
121 {0, 4, 2, 6, 1, 5, 3, 7,
122 8, 12, 10, 14, 9, 13, 11, 15};
129 set_background_color(int bg, int (*outc) (int))
131 if (set_a_background) {
132 TPUTS_TRACE("set_a_background");
133 tputs(tparm(set_a_background, bg), 1, outc);
135 TPUTS_TRACE("set_background");
136 tputs(tparm(set_background, toggled_colors(bg)), 1, outc);
141 set_foreground_color(int fg, int (*outc) (int))
143 if (set_a_foreground) {
144 TPUTS_TRACE("set_a_foreground");
145 tputs(tparm(set_a_foreground, fg), 1, outc);
147 TPUTS_TRACE("set_foreground");
148 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
153 init_color_table(void)
158 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
159 for (n = 0; n < COLORS; n++) {
161 SP->_color_table[n] = tp[n];
163 SP->_color_table[n] = tp[n % 8];
164 if (hue_lightness_saturation) {
165 SP->_color_table[n].green = 100;
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;
179 * Reset the color pair, e.g., to whatever color pair 0 is.
182 reset_color_pair(void)
186 if (orig_pair != 0) {
187 TPUTS_TRACE("orig_pair");
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.
200 _nc_reset_colors(void)
204 T((T_CALLED("_nc_reset_colors()")));
205 if (SP->_color_defs > 0)
206 SP->_color_defs = -(SP->_color_defs);
208 if (reset_color_pair())
210 if (orig_colors != 0) {
211 TPUTS_TRACE("orig_colors");
223 T((T_CALLED("start_color()")));
227 if (reset_color_pair() != TRUE) {
228 set_foreground_color(default_fg(), _nc_outch);
229 set_background_color(default_bg(), _nc_outch);
232 if (max_pairs > 0 && max_colors > 0) {
233 COLOR_PAIRS = SP->_pair_count = max_pairs;
234 COLORS = SP->_color_count = max_colors;
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());
242 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
243 COLORS, COLOR_PAIRS));
248 FreeAndNull(SP->_color_pairs);
256 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
258 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
259 /* convert RGB to HLS system */
263 if ((min = g < r ? g : r) > b)
265 if ((max = g > r ? g : r) < b)
268 /* calculate lightness */
269 *l = (min + max) / 20;
271 if (min == max) { /* black, white and all shades of gray */
277 /* calculate saturation */
279 *s = ((max - min) * 100) / (max + min);
281 *s = ((max - min) * 100) / (2000 - max - min);
285 t = 120 + ((g - b) * 60) / (max - min);
287 t = 240 + ((b - r) * 60) / (max - min);
289 t = 360 + ((r - g) * 60) / (max - min);
295 * Extension (1997/1/18) - Allow negative f/b values to set default color
299 init_pair(short pair, short f, short b)
303 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
305 if ((pair < 0) || (pair >= COLOR_PAIRS))
307 #if NCURSES_EXT_FUNCS
308 if (SP->_default_color) {
313 if (f >= COLORS && f != C_MASK)
315 if (b >= COLORS && b != C_MASK)
320 if ((f < 0) || (f >= COLORS)
321 || (b < 0) || (b >= COLORS)
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).
331 result = PAIR_OF(f, b);
332 if (SP->_color_pairs[pair] != 0
333 && SP->_color_pairs[pair] != result) {
335 attr_t z = COLOR_PAIR(pair);
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);
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 */
357 if (initialize_pair) {
358 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
360 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
362 tp[f].red, tp[f].green, tp[f].blue,
363 tp[b].red, tp[b].green, tp[b].blue));
365 if (initialize_pair) {
366 TPUTS_TRACE("initialize_pair");
367 putp(tparm(initialize_pair,
369 tp[f].red, tp[f].green, tp[f].blue,
370 tp[b].red, tp[b].green, tp[b].blue));
377 #define okRGB(n) ((n) >= 0 && (n) < 1000)
380 init_color(short color, short r, short g, short b)
384 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
386 if (initialize_color != NULL
387 && (color >= 0 && color < COLORS)
388 && (okRGB(r) && okRGB(g) && okRGB(b))) {
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;
395 if (hue_lightness_saturation) {
397 &SP->_color_table[color].red,
398 &SP->_color_table[color].green,
399 &SP->_color_table[color].blue);
401 SP->_color_table[color].red = r;
402 SP->_color_table[color].green = g;
403 SP->_color_table[color].blue = b;
406 TPUTS_TRACE("initialize_color");
407 putp(tparm(initialize_color, color, r, g, b));
408 SP->_color_defs = max(color + 1, SP->_color_defs);
415 can_change_color(void)
417 T((T_CALLED("can_change_color()")));
418 returnCode((can_change != 0) ? TRUE : FALSE);
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);
434 color_content(short color, short *r, short *g, short *b)
436 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
437 if (color < 0 || color >= COLORS)
441 *r = SP->_color_table[color].red;
443 *g = SP->_color_table[color].green;
445 *b = SP->_color_table[color].blue;
446 T(("...color_content(%d,%d,%d,%d)", color, *r, *g, *b));
451 pair_content(short pair, short *f, short *b)
453 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
455 if ((pair < 0) || (pair >= COLOR_PAIRS))
458 *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
460 *b = (SP->_color_pairs[pair] & C_MASK);
462 T(("...pair_content(%d,%d,%d)", pair, *f, *b));
467 _nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int))
469 NCURSES_COLOR_T fg = C_MASK, bg = C_MASK;
470 NCURSES_COLOR_T old_fg, old_bg;
472 if (pair < 0 || pair >= COLOR_PAIRS) {
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);
479 } else if (SP != 0) {
480 pair_content(pair, &fg, &bg);
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
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.
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);
509 #if NCURSES_EXT_FUNCS
517 NCURSES_COLOR_T xx = fg;
522 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
526 set_foreground_color(fg, outc);
529 set_background_color(bg, outc);