lib_color.c revision 62449
1/**************************************************************************** 2 * Copyright (c) 1998,1999,2000 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 44MODULE_ID("$Id: lib_color.c,v 1.51 2000/05/20 20:09:22 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 */ 51int COLOR_PAIRS = 0; 52int COLORS = 0; 53 54/* 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. 57 */ 58#define RGB_ON 680 59#define RGB_OFF 0 60/* *INDENT-OFF* */ 61static const color_t cga_palette[] = 62{ 63 /* R G B */ 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 */ 72}; 73 74static const color_t hls_palette[] = 75{ 76 /* H L S */ 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 */ 85}; 86/* *INDENT-ON* */ 87 88#ifdef NCURSES_EXT_FUNCS 89/* 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. 92 */ 93static int 94default_fg(void) 95{ 96 return (SP != 0) ? SP->_default_fg : COLOR_WHITE; 97} 98 99static int 100default_bg(void) 101{ 102 return SP != 0 ? SP->_default_bg : COLOR_BLACK; 103} 104#else 105#define default_fg() COLOR_WHITE 106#define default_bg() COLOR_BLACK 107#endif 108 109/* 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. 113 */ 114static int 115toggled_colors(int c) 116{ 117 if (c < 16) { 118 static const int table[] = 119 {0, 4, 2, 6, 1, 5, 3, 7, 120 8, 12, 10, 14, 9, 13, 11, 15}; 121 c = table[c]; 122 } 123 return c; 124} 125 126static void 127set_background_color(int bg, int (*outc) (int)) 128{ 129 if (set_a_background) { 130 TPUTS_TRACE("set_a_background"); 131 tputs(tparm(set_a_background, bg), 1, outc); 132 } else { 133 TPUTS_TRACE("set_background"); 134 tputs(tparm(set_background, toggled_colors(bg)), 1, outc); 135 } 136} 137 138static void 139set_foreground_color(int fg, int (*outc) (int)) 140{ 141 if (set_a_foreground) { 142 TPUTS_TRACE("set_a_foreground"); 143 tputs(tparm(set_a_foreground, fg), 1, outc); 144 } else { 145 TPUTS_TRACE("set_foreground"); 146 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc); 147 } 148} 149 150static bool 151set_original_colors(void) 152{ 153 if (orig_pair != 0) { 154 TPUTS_TRACE("orig_pair"); 155 putp(orig_pair); 156 return TRUE; 157 } else if (orig_colors != NULL) { 158 TPUTS_TRACE("orig_colors"); 159 putp(orig_colors); 160 return TRUE; 161 } 162 return FALSE; 163} 164 165int 166start_color(void) 167{ 168 int n; 169 const color_t *tp; 170 171 T((T_CALLED("start_color()"))); 172 173 if (set_original_colors() != TRUE) { 174 set_foreground_color(default_fg(), _nc_outch); 175 set_background_color(default_bg(), _nc_outch); 176 } 177 178 if (VALID_NUMERIC(max_pairs)) 179 COLOR_PAIRS = SP->_pair_count = max_pairs; 180 else 181 returnCode(ERR); 182 if ((SP->_color_pairs = typeCalloc(unsigned short, max_pairs)) == 0) 183 returnCode(ERR); 184 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg()); 185 if (VALID_NUMERIC(max_colors)) 186 COLORS = SP->_color_count = max_colors; 187 else 188 returnCode(ERR); 189 SP->_coloron = 1; 190 191 if ((SP->_color_table = typeMalloc(color_t, COLORS)) == 0) 192 returnCode(ERR); 193 tp = (hue_lightness_saturation) ? hls_palette : cga_palette; 194 for (n = 0; n < COLORS; n++) { 195 if (n < 8) { 196 SP->_color_table[n] = tp[n]; 197 } else { 198 SP->_color_table[n] = tp[n % 8]; 199 if (hue_lightness_saturation) { 200 SP->_color_table[n].green = 100; 201 } else { 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; 208 } 209 } 210 } 211 212 T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS)); 213 214 returnCode(OK); 215} 216 217/* This function was originally written by Daniel Weaver <danw@znyx.com> */ 218static void 219rgb2hls(short r, short g, short b, short *h, short *l, short *s) 220/* convert RGB to HLS system */ 221{ 222 short min, max, t; 223 224 if ((min = g < r ? g : r) > b) 225 min = b; 226 if ((max = g > r ? g : r) < b) 227 max = b; 228 229 /* calculate lightness */ 230 *l = (min + max) / 20; 231 232 if (min == max) { /* black, white and all shades of gray */ 233 *h = 0; 234 *s = 0; 235 return; 236 } 237 238 /* calculate saturation */ 239 if (*l < 50) 240 *s = ((max - min) * 100) / (max + min); 241 else 242 *s = ((max - min) * 100) / (2000 - max - min); 243 244 /* calculate hue */ 245 if (r == max) 246 t = 120 + ((g - b) * 60) / (max - min); 247 else if (g == max) 248 t = 240 + ((b - r) * 60) / (max - min); 249 else 250 t = 360 + ((r - g) * 60) / (max - min); 251 252 *h = t % 360; 253} 254 255/* 256 * Extension (1997/1/18) - Allow negative f/b values to set default color 257 * values. 258 */ 259int 260init_pair(short pair, short f, short b) 261{ 262 unsigned result; 263 264 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b)); 265 266 if ((pair < 0) || (pair >= COLOR_PAIRS)) 267 returnCode(ERR); 268#ifdef NCURSES_EXT_FUNCS 269 if (SP->_default_color) { 270 if (f < 0) 271 f = C_MASK; 272 if (b < 0) 273 b = C_MASK; 274 if (f >= COLORS && f != C_MASK) 275 returnCode(ERR); 276 if (b >= COLORS && b != C_MASK) 277 returnCode(ERR); 278 } else 279#endif 280 { 281 if ((f < 0) || (f >= COLORS) 282 || (b < 0) || (b >= COLORS) 283 || (pair < 1)) 284 returnCode(ERR); 285 } 286 287 /* 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). 291 */ 292 result = PAIR_OF(f, b); 293 if (SP->_color_pairs[pair] != 0 294 && SP->_color_pairs[pair] != result) { 295 int y, x; 296 attr_t z = COLOR_PAIR(pair); 297 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() */ 305 ptr->text[x] = 0; 306 CHANGED_CELL(ptr, x); 307 changed = TRUE; 308 } 309 } 310 if (changed) 311 _nc_make_oldhash(y); 312 } 313 } 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 */ 317 318 if (initialize_pair) { 319 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette; 320 321 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)", 322 pair, 323 tp[f].red, tp[f].green, tp[f].blue, 324 tp[b].red, tp[b].green, tp[b].blue)); 325 326 if (initialize_pair) { 327 TPUTS_TRACE("initialize_pair"); 328 putp(tparm(initialize_pair, 329 pair, 330 tp[f].red, tp[f].green, tp[f].blue, 331 tp[b].red, tp[b].green, tp[b].blue)); 332 } 333 } 334 335 returnCode(OK); 336} 337 338int 339init_color(short color, short r, short g, short b) 340{ 341 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b)); 342 343 if (initialize_color == NULL) 344 returnCode(ERR); 345 346 if (color < 0 || color >= COLORS) 347 returnCode(ERR); 348 if (r < 0 || r > 1000 || g < 0 || g > 1000 || b < 0 || b > 1000) 349 returnCode(ERR); 350 351 if (hue_lightness_saturation) 352 rgb2hls(r, g, b, 353 &SP->_color_table[color].red, 354 &SP->_color_table[color].green, 355 &SP->_color_table[color].blue); 356 else { 357 SP->_color_table[color].red = r; 358 SP->_color_table[color].green = g; 359 SP->_color_table[color].blue = b; 360 } 361 362 if (initialize_color) { 363 TPUTS_TRACE("initialize_color"); 364 putp(tparm(initialize_color, color, r, g, b)); 365 } 366 returnCode(OK); 367} 368 369bool 370can_change_color(void) 371{ 372 T((T_CALLED("can_change_color()"))); 373 returnCode((can_change != 0) ? TRUE : FALSE); 374} 375 376bool 377has_colors(void) 378{ 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); 386} 387 388int 389color_content(short color, short *r, short *g, short *b) 390{ 391 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b)); 392 if (color < 0 || color >= COLORS) 393 returnCode(ERR); 394 395 if (r) 396 *r = SP->_color_table[color].red; 397 if (g) 398 *g = SP->_color_table[color].green; 399 if (b) 400 *b = SP->_color_table[color].blue; 401 returnCode(OK); 402} 403 404int 405pair_content(short pair, short *f, short *b) 406{ 407 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b)); 408 409 if ((pair < 0) || (pair >= COLOR_PAIRS)) 410 returnCode(ERR); 411 if (f) 412 *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK); 413 if (b) 414 *b = (SP->_color_pairs[pair] & C_MASK); 415 416 returnCode(OK); 417} 418 419void 420_nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int)) 421{ 422 NCURSES_COLOR_T fg = C_MASK, bg = C_MASK; 423 NCURSES_COLOR_T old_fg, old_bg; 424 425 if (pair < 0 || pair >= COLOR_PAIRS) { 426 return; 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); 431 return; 432 } else if (SP != 0) { 433 pair_content(pair, &fg, &bg); 434 } 435 } 436 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#ifdef NCURSES_EXT_FUNCS 442 /* 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. 446 */ 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); 451 } else 452#endif 453 set_original_colors(); 454 } 455 } else { 456 set_original_colors(); 457 if (old_pair < 0) 458 return; 459 } 460 461#ifdef NCURSES_EXT_FUNCS 462 if (fg == C_MASK) 463 fg = default_fg(); 464 if (bg == C_MASK) 465 bg = default_bg(); 466#endif 467 468 if (reverse) { 469 NCURSES_COLOR_T xx = fg; 470 fg = bg; 471 bg = xx; 472 } 473 474 T(("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg)); 475 476 if (fg != C_MASK) { 477 set_foreground_color(fg, outc); 478 } 479 if (bg != C_MASK) { 480 set_background_color(bg, outc); 481 } 482} 483