1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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.53 2000/09/02 18:02:15 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.
55 * Given a RGB range of 0..1000, we'll normally set the individual values
56 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
61 static const color_t cga_palette[] =
64 {RGB_OFF, RGB_OFF, RGB_OFF}, /* COLOR_BLACK */
65 {RGB_ON, RGB_OFF, RGB_OFF}, /* COLOR_RED */
66 {RGB_OFF, RGB_ON, RGB_OFF}, /* COLOR_GREEN */
67 {RGB_ON, RGB_ON, RGB_OFF}, /* COLOR_YELLOW */
68 {RGB_OFF, RGB_OFF, RGB_ON}, /* COLOR_BLUE */
69 {RGB_ON, RGB_OFF, RGB_ON}, /* COLOR_MAGENTA */
70 {RGB_OFF, RGB_ON, RGB_ON}, /* COLOR_CYAN */
71 {RGB_ON, RGB_ON, RGB_ON}, /* COLOR_WHITE */
74 static const color_t hls_palette[] =
77 { 0, 0, 0}, /* COLOR_BLACK */
78 { 120, 50, 100}, /* COLOR_RED */
79 { 240, 50, 100}, /* COLOR_GREEN */
80 { 180, 50, 100}, /* COLOR_YELLOW */
81 { 330, 50, 100}, /* COLOR_BLUE */
82 { 60, 50, 100}, /* COLOR_MAGENTA */
83 { 300, 50, 100}, /* COLOR_CYAN */
84 { 0, 50, 100}, /* COLOR_WHITE */
90 * These are called from _nc_do_color(), which in turn is called from
91 * vidattr - so we have to assume that SP may be null.
96 return (SP != 0) ? SP->_default_fg : COLOR_WHITE;
102 return SP != 0 ? SP->_default_bg : COLOR_BLACK;
105 #define default_fg() COLOR_WHITE
106 #define default_bg() COLOR_BLACK
110 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
111 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
112 * also used in the FreeBSD syscons.
115 toggled_colors(int c)
118 static const int table[] =
119 {0, 4, 2, 6, 1, 5, 3, 7,
120 8, 12, 10, 14, 9, 13, 11, 15};
127 set_background_color(int bg, int (*outc) (int))
129 if (set_a_background) {
130 TPUTS_TRACE("set_a_background");
131 tputs(tparm(set_a_background, bg), 1, outc);
133 TPUTS_TRACE("set_background");
134 tputs(tparm(set_background, toggled_colors(bg)), 1, outc);
139 set_foreground_color(int fg, int (*outc) (int))
141 if (set_a_foreground) {
142 TPUTS_TRACE("set_a_foreground");
143 tputs(tparm(set_a_foreground, fg), 1, outc);
145 TPUTS_TRACE("set_foreground");
146 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
151 set_original_colors(void)
153 if (orig_pair != 0) {
154 TPUTS_TRACE("orig_pair");
157 } else if (orig_colors != NULL) {
158 TPUTS_TRACE("orig_colors");
171 T((T_CALLED("start_color()")));
173 if (set_original_colors() != TRUE) {
174 set_foreground_color(default_fg(), _nc_outch);
175 set_background_color(default_bg(), _nc_outch);
178 if (VALID_NUMERIC(max_pairs))
179 COLOR_PAIRS = SP->_pair_count = max_pairs;
182 if ((SP->_color_pairs = typeCalloc(unsigned short, max_pairs)) == 0)
184 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg());
185 if (VALID_NUMERIC(max_colors))
186 COLORS = SP->_color_count = max_colors;
191 if ((SP->_color_table = typeMalloc(color_t, COLORS)) == 0)
193 tp = (hue_lightness_saturation) ? hls_palette : cga_palette;
194 for (n = 0; n < COLORS; n++) {
196 SP->_color_table[n] = tp[n];
198 SP->_color_table[n] = tp[n % 8];
199 if (hue_lightness_saturation) {
200 SP->_color_table[n].green = 100;
202 if (SP->_color_table[n].red)
203 SP->_color_table[n].red = 1000;
204 if (SP->_color_table[n].green)
205 SP->_color_table[n].green = 1000;
206 if (SP->_color_table[n].blue)
207 SP->_color_table[n].blue = 1000;
212 T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS));
217 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
219 rgb2hls(short r, short g, short b, short *h, short *l, short *s)
220 /* convert RGB to HLS system */
224 if ((min = g < r ? g : r) > b)
226 if ((max = g > r ? g : r) < b)
229 /* calculate lightness */
230 *l = (min + max) / 20;
232 if (min == max) { /* black, white and all shades of gray */
238 /* calculate saturation */
240 *s = ((max - min) * 100) / (max + min);
242 *s = ((max - min) * 100) / (2000 - max - min);
246 t = 120 + ((g - b) * 60) / (max - min);
248 t = 240 + ((b - r) * 60) / (max - min);
250 t = 360 + ((r - g) * 60) / (max - min);
256 * Extension (1997/1/18) - Allow negative f/b values to set default color
260 init_pair(short pair, short f, short b)
264 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
266 if ((pair < 0) || (pair >= COLOR_PAIRS))
268 #if NCURSES_EXT_FUNCS
269 if (SP->_default_color) {
274 if (f >= COLORS && f != C_MASK)
276 if (b >= COLORS && b != C_MASK)
281 if ((f < 0) || (f >= COLORS)
282 || (b < 0) || (b >= COLORS)
288 * When a pair's content is changed, replace its colors (if pair was
289 * initialized before a screen update is performed replacing original
290 * pair colors with the new ones).
292 result = PAIR_OF(f, b);
293 if (SP->_color_pairs[pair] != 0
294 && SP->_color_pairs[pair] != result) {
296 attr_t z = COLOR_PAIR(pair);
298 for (y = 0; y <= curscr->_maxy; y++) {
299 struct ldat *ptr = &(curscr->_line[y]);
300 bool changed = FALSE;
301 for (x = 0; x <= curscr->_maxx; x++) {
302 if ((ptr->text[x] & A_COLOR) == z) {
303 /* Set the old cell to zero to ensure it will be
304 updated on the next doupdate() */
306 CHANGED_CELL(ptr, x);
314 SP->_color_pairs[pair] = result;
315 if ((int) (SP->_current_attr & A_COLOR) == COLOR_PAIR(pair))
316 SP->_current_attr |= A_COLOR; /* force attribute update */
318 if (initialize_pair) {
319 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
321 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
323 tp[f].red, tp[f].green, tp[f].blue,
324 tp[b].red, tp[b].green, tp[b].blue));
326 if (initialize_pair) {
327 TPUTS_TRACE("initialize_pair");
328 putp(tparm(initialize_pair,
330 tp[f].red, tp[f].green, tp[f].blue,
331 tp[b].red, tp[b].green, tp[b].blue));
339 init_color(short color, short r, short g, short b)
341 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
343 if (initialize_color == NULL)
346 if (color < 0 || color >= COLORS)
348 if (r < 0 || r > 1000 || g < 0 || g > 1000 || b < 0 || b > 1000)
351 if (hue_lightness_saturation)
353 &SP->_color_table[color].red,
354 &SP->_color_table[color].green,
355 &SP->_color_table[color].blue);
357 SP->_color_table[color].red = r;
358 SP->_color_table[color].green = g;
359 SP->_color_table[color].blue = b;
362 if (initialize_color) {
363 TPUTS_TRACE("initialize_color");
364 putp(tparm(initialize_color, color, r, g, b));
370 can_change_color(void)
372 T((T_CALLED("can_change_color()")));
373 returnCode((can_change != 0) ? TRUE : FALSE);
379 T((T_CALLED("has_colors()")));
380 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
381 && (((set_foreground != NULL)
382 && (set_background != NULL))
383 || ((set_a_foreground != NULL)
384 && (set_a_background != NULL))
385 || set_color_pair)) ? TRUE : FALSE);
389 color_content(short color, short *r, short *g, short *b)
391 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
392 if (color < 0 || color >= COLORS)
396 *r = SP->_color_table[color].red;
398 *g = SP->_color_table[color].green;
400 *b = SP->_color_table[color].blue;
405 pair_content(short pair, short *f, short *b)
407 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
409 if ((pair < 0) || (pair >= COLOR_PAIRS))
412 *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
414 *b = (SP->_color_pairs[pair] & C_MASK);
420 _nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int))
422 NCURSES_COLOR_T fg = C_MASK, bg = C_MASK;
423 NCURSES_COLOR_T old_fg, old_bg;
425 if (pair < 0 || pair >= COLOR_PAIRS) {
427 } else if (pair != 0) {
428 if (set_color_pair) {
429 TPUTS_TRACE("set_color_pair");
430 tputs(tparm(set_color_pair, pair), 1, outc);
432 } else if (SP != 0) {
433 pair_content(pair, &fg, &bg);
437 if (old_pair >= 0 && SP != 0) {
438 pair_content(old_pair, &old_fg, &old_bg);
439 if ((fg == C_MASK && old_fg != C_MASK)
440 || (bg == C_MASK && old_bg != C_MASK)) {
441 #if NCURSES_EXT_FUNCS
443 * A minor optimization - but extension. If "AX" is specified in
444 * the terminal description, treat it as screen's indicator of ECMA
445 * SGR 39 and SGR 49, and assume the two sequences are independent.
447 if (SP->_has_sgr_39_49 && old_bg == C_MASK && old_fg != C_MASK) {
448 tputs("\033[39m", 1, outc);
449 } else if (SP->_has_sgr_39_49 && old_fg == C_MASK && old_bg != C_MASK) {
450 tputs("\033[49m", 1, outc);
453 set_original_colors();
456 set_original_colors();
461 #if NCURSES_EXT_FUNCS
469 NCURSES_COLOR_T xx = fg;
474 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg));
477 set_foreground_color(fg, outc);
480 set_background_color(bg, outc);