1 /****************************************************************************
2 * Copyright (c) 1998-2007,2009 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
38 * Handles color emulation of SYS V curses
41 #include <curses.priv.h>
47 #define CUR SP_TERMTYPE
50 MODULE_ID("$Id: lib_color.c,v 1.91 2009/05/10 00:48:29 tom Exp $")
53 * These should be screen structure members. They need to be globals for
54 * historical reasons. So we assign them in start_color() and also in
55 * set_term()'s screen-switching logic.
59 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
61 return SP ? SP->_pair_count : -1;
64 NCURSES_PUBLIC_VAR(COLORS) (void)
66 return SP ? SP->_color_count : -1;
69 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
70 NCURSES_EXPORT_VAR(int) COLORS = 0;
73 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
75 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
79 #define OkColorHi(n) (((n) < COLORS) && ((n) < max_colors))
80 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
83 * Given a RGB range of 0..1000, we'll normally set the individual values
84 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
89 static const color_t cga_palette[] =
92 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
93 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
94 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
95 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
96 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
97 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
98 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
99 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
102 static const color_t hls_palette[] =
105 DATA( 0, 0, 0), /* COLOR_BLACK */
106 DATA( 120, 50, 100), /* COLOR_RED */
107 DATA( 240, 50, 100), /* COLOR_GREEN */
108 DATA( 180, 50, 100), /* COLOR_YELLOW */
109 DATA( 330, 50, 100), /* COLOR_BLUE */
110 DATA( 60, 50, 100), /* COLOR_MAGENTA */
111 DATA( 300, 50, 100), /* COLOR_CYAN */
112 DATA( 0, 50, 100), /* COLOR_WHITE */
117 * Ensure that we use color pairs only when colors have been started, and also
118 * that the index is within the limits of the table which we allocated.
120 #define ValidPair(pair) \
121 ((SP != 0) && (pair >= 0) && (pair < SP->_pair_limit) && SP->_coloron)
123 #if NCURSES_EXT_FUNCS
125 * These are called from _nc_do_color(), which in turn is called from
126 * vidattr - so we have to assume that SP may be null.
131 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
137 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
140 #define default_fg() COLOR_WHITE
141 #define default_bg() COLOR_BLACK
145 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
146 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
147 * also used in the FreeBSD syscons.
150 toggled_colors(int c)
153 static const int table[] =
154 {0, 4, 2, 6, 1, 5, 3, 7,
155 8, 12, 10, 14, 9, 13, 11, 15};
162 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
164 if (set_a_background) {
165 TPUTS_TRACE("set_a_background");
166 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
167 TPARM_1(set_a_background, bg),
170 TPUTS_TRACE("set_background");
171 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
172 TPARM_1(set_background, toggled_colors(bg)),
178 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
180 if (set_a_foreground) {
181 TPUTS_TRACE("set_a_foreground");
182 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
183 TPARM_1(set_a_foreground, fg),
186 TPUTS_TRACE("set_foreground");
187 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
188 TPARM_1(set_foreground, toggled_colors(fg)),
194 init_color_table(NCURSES_SP_DCL0)
199 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
200 for (n = 0; n < COLORS; n++) {
202 SP->_color_table[n] = tp[n];
204 SP->_color_table[n] = tp[n % MAX_PALETTE];
205 if (hue_lightness_saturation) {
206 SP->_color_table[n].green = 100;
208 if (SP->_color_table[n].red)
209 SP->_color_table[n].red = 1000;
210 if (SP->_color_table[n].green)
211 SP->_color_table[n].green = 1000;
212 if (SP->_color_table[n].blue)
213 SP->_color_table[n].blue = 1000;
220 * Reset the color pair, e.g., to whatever color pair 0 is.
223 reset_color_pair(NCURSES_SP_DCL0)
227 if (orig_pair != 0) {
228 TPUTS_TRACE("orig_pair");
236 * Reset color pairs and definitions. Actually we do both more to accommodate
237 * badly-written terminal descriptions than for the relatively rare case where
238 * someone has changed the color definitions.
241 _nc_reset_colors(void)
244 SCREEN *sp = CURRENT_SCREEN;
248 T((T_CALLED("_nc_reset_colors()")));
249 if (SP->_color_defs > 0)
250 SP->_color_defs = -(SP->_color_defs);
252 if (reset_color_pair(NCURSES_SP_ARG))
254 if (orig_colors != 0) {
255 TPUTS_TRACE("orig_colors");
263 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
267 T((T_CALLED("start_color()")));
271 } else if (SP_PARM->_coloron) {
275 if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
276 set_foreground_color(NCURSES_SP_ARGx
278 NCURSES_SP_NAME(_nc_outch));
279 set_background_color(NCURSES_SP_ARGx
281 NCURSES_SP_NAME(_nc_outch));
284 if (max_pairs > 0 && max_colors > 0) {
285 SP_PARM->_pair_limit = max_pairs;
287 #if NCURSES_EXT_FUNCS
289 * If using default colors, allocate extra space in table to
290 * allow for default-color as a component of a color-pair.
292 SP_PARM->_pair_limit += (1 + (2 * max_colors));
294 SP_PARM->_pair_count = max_pairs;
295 SP_PARM->_color_count = max_colors;
297 COLOR_PAIRS = max_pairs;
301 SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t,
302 SP_PARM->_pair_limit);
303 if (SP_PARM->_color_pairs != 0) {
304 SP_PARM->_color_table = TYPE_CALLOC(color_t, max_colors);
305 if (SP_PARM->_color_table != 0) {
306 SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(),
308 init_color_table(NCURSES_SP_ARG);
310 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
311 COLORS, COLOR_PAIRS));
313 SP_PARM->_coloron = 1;
315 } else if (SP_PARM->_color_pairs != 0) {
316 FreeAndNull(SP_PARM->_color_pairs);
330 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
334 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
336 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
337 /* convert RGB to HLS system */
341 if ((min = g < r ? g : r) > b)
343 if ((max = g > r ? g : r) < b)
346 /* calculate lightness */
347 *l = (min + max) / 20;
349 if (min == max) { /* black, white and all shades of gray */
355 /* calculate saturation */
357 *s = ((max - min) * 100) / (max + min);
359 *s = ((max - min) * 100) / (2000 - max - min);
363 t = 120 + ((g - b) * 60) / (max - min);
365 t = 240 + ((b - r) * 60) / (max - min);
367 t = 360 + ((r - g) * 60) / (max - min);
373 * Extension (1997/1/18) - Allow negative f/b values to set default color
377 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx short pair, short f, short b)
380 colorpair_t previous;
382 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
384 if (!ValidPair(pair))
387 previous = SP_PARM->_color_pairs[pair];
388 #if NCURSES_EXT_FUNCS
389 if (SP_PARM->_default_color) {
390 bool isDefault = FALSE;
391 bool wasDefault = FALSE;
392 int default_pairs = SP_PARM->_default_pairs;
395 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
396 * internal unsigned values which we will store in the _color_pairs[]
399 if (isDefaultColor(f)) {
402 } else if (!OkColorHi(f)) {
406 if (isDefaultColor(b)) {
409 } else if (!OkColorHi(b)) {
414 * Check if the table entry that we are going to init/update used
417 if ((FORE_OF(previous) == COLOR_DEFAULT)
418 || (BACK_OF(previous) == COLOR_DEFAULT))
422 * Keep track of the number of entries in the color pair table which
423 * used a default color.
425 if (isDefault && !wasDefault) {
427 } else if (wasDefault && !isDefault) {
432 * As an extension, ncurses allows the pair number to exceed the
433 * terminal's color_pairs value for pairs using a default color.
435 * Note that updating a pair which used a default color with one
436 * that does not will decrement the count - and possibly interfere
437 * with sequentially adding new pairs.
439 if (pair > (SP_PARM->_pair_count + default_pairs)) {
442 SP_PARM->_default_pairs = default_pairs;
446 if ((f < 0) || !OkColorHi(f)
447 || (b < 0) || !OkColorHi(b)
453 * When a pair's content is changed, replace its colors (if pair was
454 * initialized before a screen update is performed replacing original
455 * pair colors with the new ones).
457 result = PAIR_OF(f, b);
459 && previous != result) {
462 for (y = 0; y <= curscr->_maxy; y++) {
463 struct ldat *ptr = &(curscr->_line[y]);
464 bool changed = FALSE;
465 for (x = 0; x <= curscr->_maxx; x++) {
466 if (GetPair(ptr->text[x]) == pair) {
467 /* Set the old cell to zero to ensure it will be
468 updated on the next doupdate() */
469 SetChar(ptr->text[x], 0, 0);
470 CHANGED_CELL(ptr, x);
478 SP_PARM->_color_pairs[pair] = result;
479 if (GET_SCREEN_PAIR(SP_PARM) == pair)
480 SET_SCREEN_PAIR(SP_PARM, (chtype) (~0)); /* force attribute update */
482 if (initialize_pair && InPalette(f) && InPalette(b)) {
483 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
486 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
488 tp[f].red, tp[f].green, tp[f].blue,
489 tp[b].red, tp[b].green, tp[b].blue));
491 TPUTS_TRACE("initialize_pair");
492 putp(TPARM_7(initialize_pair,
494 tp[f].red, tp[f].green, tp[f].blue,
495 tp[b].red, tp[b].green, tp[b].blue));
503 init_pair(short pair, short f, short b)
505 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
509 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
512 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
513 short color, short r, short g, short b)
517 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
519 if (initialize_color != NULL
522 && (color >= 0 && OkColorHi(color))
523 && (okRGB(r) && okRGB(g) && okRGB(b))) {
525 SP_PARM->_color_table[color].init = 1;
526 SP_PARM->_color_table[color].r = r;
527 SP_PARM->_color_table[color].g = g;
528 SP_PARM->_color_table[color].b = b;
530 if (hue_lightness_saturation) {
532 &SP_PARM->_color_table[color].red,
533 &SP_PARM->_color_table[color].green,
534 &SP_PARM->_color_table[color].blue);
536 SP_PARM->_color_table[color].red = r;
537 SP_PARM->_color_table[color].green = g;
538 SP_PARM->_color_table[color].blue = b;
541 TPUTS_TRACE("initialize_color");
542 putp(TPARM_4(initialize_color, color, r, g, b));
543 SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
551 init_color(short color, short r, short g, short b)
553 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
558 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL0)
560 T((T_CALLED("can_change_color()")));
561 returnCode((can_change != 0) ? TRUE : FALSE);
566 can_change_color(void)
568 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
573 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
575 T((T_CALLED("has_colors()")));
576 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
577 && (((set_foreground != NULL)
578 && (set_background != NULL))
579 || ((set_a_foreground != NULL)
580 && (set_a_background != NULL))
581 || set_color_pair)) ? TRUE : FALSE);
588 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
593 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx short color, short *r,
598 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
599 if (color < 0 || !OkColorHi(color) || SP_PARM == 0 || !SP_PARM->_coloron) {
602 NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
603 NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
604 NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
613 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
614 color, c_r, c_g, c_b));
622 color_content(short color, short *r, short *g, short *b)
624 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
629 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx short pair, short *f,
634 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
636 if (!ValidPair(pair)) {
639 NCURSES_COLOR_T fg = FORE_OF(SP_PARM->_color_pairs[pair]);
640 NCURSES_COLOR_T bg = BACK_OF(SP_PARM->_color_pairs[pair]);
642 #if NCURSES_EXT_FUNCS
643 if (fg == COLOR_DEFAULT)
645 if (bg == COLOR_DEFAULT)
654 TR(TRACE_ATTRS, ("...pair_content(%d,%d,%d)", pair, fg, bg));
662 pair_content(short pair, short *f, short *b)
664 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
669 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
673 NCURSES_SP_OUTC outc)
675 NCURSES_COLOR_T fg = COLOR_DEFAULT;
676 NCURSES_COLOR_T bg = COLOR_DEFAULT;
677 NCURSES_COLOR_T old_fg, old_bg;
679 if (!ValidPair(pair)) {
681 } else if (pair != 0) {
682 if (set_color_pair) {
683 TPUTS_TRACE("set_color_pair");
684 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
685 TPARM_1(set_color_pair, pair),
688 } else if (SP != 0) {
689 pair_content((short) pair, &fg, &bg);
695 && pair_content(old_pair, &old_fg, &old_bg) != ERR) {
696 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
697 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
698 #if NCURSES_EXT_FUNCS
700 * A minor optimization - but extension. If "AX" is specified in
701 * the terminal description, treat it as screen's indicator of ECMA
702 * SGR 39 and SGR 49, and assume the two sequences are independent.
704 if (SP->_has_sgr_39_49
705 && isDefaultColor(old_bg)
706 && !isDefaultColor(old_fg)) {
707 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
708 } else if (SP->_has_sgr_39_49
709 && isDefaultColor(old_fg)
710 && !isDefaultColor(old_bg)) {
711 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
714 reset_color_pair(NCURSES_SP_ARG);
717 reset_color_pair(NCURSES_SP_ARG);
722 #if NCURSES_EXT_FUNCS
723 if (isDefaultColor(fg))
725 if (isDefaultColor(bg))
730 NCURSES_COLOR_T xx = fg;
735 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
738 if (!isDefaultColor(fg)) {
739 set_foreground_color(NCURSES_SP_ARGx fg, outc);
741 if (!isDefaultColor(bg)) {
742 set_background_color(NCURSES_SP_ARGx bg, outc);
748 _nc_do_color(short old_pair, short pair, bool reverse, NCURSES_OUTC outc)
750 SetSafeOutcWrapper(outc);
751 NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,