1 /* $OpenBSD: lib_color.c,v 1.7 2000/03/26 16:45:03 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* lib_color.c 37 * 38 * Handles color emulation of SYS V curses 39 */ 40 41 #include <curses.priv.h> 42 43 #include <term.h> 44 #include <tic.h> 45 46 MODULE_ID("$From: lib_color.c,v 1.49 2000/03/26 03:12:12 tom Exp $") 47 48 /* 49 * These should be screen structure members. They need to be globals for 50 * historical reasons. So we assign them in start_color() and also in 51 * set_term()'s screen-switching logic. 52 */ 53 int COLOR_PAIRS = 0; 54 int COLORS = 0; 55 56 /* 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. 59 */ 60 #define RGB_ON 680 61 #define RGB_OFF 0 62 /* *INDENT-OFF* */ 63 static const color_t cga_palette[] = 64 { 65 /* R G B */ 66 {RGB_OFF, RGB_OFF, RGB_OFF}, /* COLOR_BLACK */ 67 {RGB_ON, RGB_OFF, RGB_OFF}, /* COLOR_RED */ 68 {RGB_OFF, RGB_ON, RGB_OFF}, /* COLOR_GREEN */ 69 {RGB_ON, RGB_ON, RGB_OFF}, /* COLOR_YELLOW */ 70 {RGB_OFF, RGB_OFF, RGB_ON}, /* COLOR_BLUE */ 71 {RGB_ON, RGB_OFF, RGB_ON}, /* COLOR_MAGENTA */ 72 {RGB_OFF, RGB_ON, RGB_ON}, /* COLOR_CYAN */ 73 {RGB_ON, RGB_ON, RGB_ON}, /* COLOR_WHITE */ 74 }; 75 76 static const color_t hls_palette[] = 77 { 78 /* H L S */ 79 { 0, 0, 0}, /* COLOR_BLACK */ 80 { 120, 50, 100}, /* COLOR_RED */ 81 { 240, 50, 100}, /* COLOR_GREEN */ 82 { 180, 50, 100}, /* COLOR_YELLOW */ 83 { 330, 50, 100}, /* COLOR_BLUE */ 84 { 60, 50, 100}, /* COLOR_MAGENTA */ 85 { 300, 50, 100}, /* COLOR_CYAN */ 86 { 0, 50, 100}, /* COLOR_WHITE */ 87 }; 88 /* *INDENT-ON* */ 89 90 #ifdef NCURSES_EXT_FUNCS 91 /* 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. 94 */ 95 static int 96 default_fg(void) 97 { 98 return (SP != 0) ? SP->_default_fg : COLOR_WHITE; 99 } 100 101 static int 102 default_bg(void) 103 { 104 return SP != 0 ? SP->_default_bg : COLOR_BLACK; 105 } 106 #else 107 #define default_fg() COLOR_WHITE 108 #define default_bg() COLOR_BLACK 109 #endif 110 111 /* 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. 115 */ 116 static int 117 toggled_colors(int c) 118 { 119 if (c < 16) { 120 static const int table[] = 121 {0, 4, 2, 6, 1, 5, 3, 7, 122 8, 12, 10, 14, 9, 13, 11, 15}; 123 c = table[c]; 124 } 125 return c; 126 } 127 128 static void 129 set_background_color(int bg, int (*outc) (int)) 130 { 131 if (set_a_background) { 132 TPUTS_TRACE("set_a_background"); 133 tputs(tparm(set_a_background, bg), 1, outc); 134 } else { 135 TPUTS_TRACE("set_background"); 136 tputs(tparm(set_background, toggled_colors(bg)), 1, outc); 137 } 138 } 139 140 static void 141 set_foreground_color(int fg, int (*outc) (int)) 142 { 143 if (set_a_foreground) { 144 TPUTS_TRACE("set_a_foreground"); 145 tputs(tparm(set_a_foreground, fg), 1, outc); 146 } else { 147 TPUTS_TRACE("set_foreground"); 148 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc); 149 } 150 } 151 152 static bool 153 set_original_colors(void) 154 { 155 if (orig_pair != 0) { 156 TPUTS_TRACE("orig_pair"); 157 putp(orig_pair); 158 return TRUE; 159 } else if (orig_colors != NULL) { 160 TPUTS_TRACE("orig_colors"); 161 putp(orig_colors); 162 return TRUE; 163 } 164 return FALSE; 165 } 166 167 int 168 start_color(void) 169 { 170 int n; 171 const color_t *tp; 172 173 T((T_CALLED("start_color()"))); 174 175 if (set_original_colors() != TRUE) { 176 set_foreground_color(default_fg(), _nc_outch); 177 set_background_color(default_bg(), _nc_outch); 178 } 179 180 if (VALID_NUMERIC(max_pairs)) 181 COLOR_PAIRS = SP->_pair_count = max_pairs; 182 else 183 returnCode(ERR); 184 if ((SP->_color_pairs = typeCalloc(unsigned short, max_pairs)) == 0) 185 returnCode(ERR); 186 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg()); 187 if (VALID_NUMERIC(max_colors)) 188 COLORS = SP->_color_count = max_colors; 189 else 190 returnCode(ERR); 191 SP->_coloron = 1; 192 193 if ((SP->_color_table = typeMalloc(color_t, COLORS)) == 0) 194 returnCode(ERR); 195 tp = (hue_lightness_saturation) ? hls_palette : cga_palette; 196 for (n = 0; n < COLORS; n++) { 197 if (n < 8) { 198 SP->_color_table[n] = tp[n]; 199 } else { 200 SP->_color_table[n] = tp[n % 8]; 201 if (hue_lightness_saturation) { 202 SP->_color_table[n].green = 100; 203 } else { 204 if (SP->_color_table[n].red) 205 SP->_color_table[n].red = 1000; 206 if (SP->_color_table[n].green) 207 SP->_color_table[n].green = 1000; 208 if (SP->_color_table[n].blue) 209 SP->_color_table[n].blue = 1000; 210 } 211 } 212 } 213 214 T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS)); 215 216 returnCode(OK); 217 } 218 219 /* This function was originally written by Daniel Weaver <danw@znyx.com> */ 220 static void 221 rgb2hls(short r, short g, short b, short *h, short *l, short *s) 222 /* convert RGB to HLS system */ 223 { 224 short min, max, t; 225 226 if ((min = g < r ? g : r) > b) 227 min = b; 228 if ((max = g > r ? g : r) < b) 229 max = b; 230 231 /* calculate lightness */ 232 *l = (min + max) / 20; 233 234 if (min == max) { /* black, white and all shades of gray */ 235 *h = 0; 236 *s = 0; 237 return; 238 } 239 240 /* calculate saturation */ 241 if (*l < 50) 242 *s = ((max - min) * 100) / (max + min); 243 else 244 *s = ((max - min) * 100) / (2000 - max - min); 245 246 /* calculate hue */ 247 if (r == max) 248 t = 120 + ((g - b) * 60) / (max - min); 249 else if (g == max) 250 t = 240 + ((b - r) * 60) / (max - min); 251 else 252 t = 360 + ((r - g) * 60) / (max - min); 253 254 *h = t % 360; 255 } 256 257 /* 258 * Extension (1997/1/18) - Allow negative f/b values to set default color 259 * values. 260 */ 261 int 262 init_pair(short pair, short f, short b) 263 { 264 unsigned result; 265 266 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b)); 267 268 if ((pair < 0) || (pair >= COLOR_PAIRS)) 269 returnCode(ERR); 270 #ifdef NCURSES_EXT_FUNCS 271 if (SP->_default_color) { 272 if (f < 0) 273 f = C_MASK; 274 if (b < 0) 275 b = C_MASK; 276 if (f >= COLORS && f != C_MASK) 277 returnCode(ERR); 278 if (b >= COLORS && b != C_MASK) 279 returnCode(ERR); 280 } else 281 #endif 282 { 283 if ((f < 0) || (f >= COLORS) 284 || (b < 0) || (b >= COLORS) 285 || (pair < 1)) 286 returnCode(ERR); 287 } 288 289 /* 290 * When a pair's content is changed, replace its colors (if pair was 291 * initialized before a screen update is performed replacing original 292 * pair colors with the new ones). 293 */ 294 result = PAIR_OF(f, b); 295 if (SP->_color_pairs[pair] != 0 296 && SP->_color_pairs[pair] != result) { 297 int y, x; 298 attr_t z = COLOR_PAIR(pair); 299 300 for (y = 0; y <= curscr->_maxy; y++) { 301 struct ldat *ptr = &(curscr->_line[y]); 302 bool changed = FALSE; 303 for (x = 0; x <= curscr->_maxx; x++) { 304 if ((ptr->text[x] & A_COLOR) == z) { 305 /* Set the old cell to zero to ensure it will be 306 updated on the next doupdate() */ 307 ptr->text[x] = 0; 308 CHANGED_CELL(ptr, x); 309 changed = TRUE; 310 } 311 } 312 if (changed) 313 _nc_make_oldhash(y); 314 } 315 } 316 SP->_color_pairs[pair] = result; 317 if ((int)(SP->_current_attr & A_COLOR) == COLOR_PAIR(pair)) 318 SP->_current_attr |= A_COLOR; /* force attribute update */ 319 320 if (initialize_pair) { 321 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette; 322 323 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)", 324 pair, 325 tp[f].red, tp[f].green, tp[f].blue, 326 tp[b].red, tp[b].green, tp[b].blue)); 327 328 if (initialize_pair) { 329 TPUTS_TRACE("initialize_pair"); 330 putp(tparm(initialize_pair, 331 pair, 332 tp[f].red, tp[f].green, tp[f].blue, 333 tp[b].red, tp[b].green, tp[b].blue)); 334 } 335 } 336 337 returnCode(OK); 338 } 339 340 int 341 init_color(short color, short r, short g, short b) 342 { 343 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b)); 344 345 if (initialize_color == NULL) 346 returnCode(ERR); 347 348 if (color < 0 || color >= COLORS) 349 returnCode(ERR); 350 if (r < 0 || r > 1000 || g < 0 || g > 1000 || b < 0 || b > 1000) 351 returnCode(ERR); 352 353 if (hue_lightness_saturation) 354 rgb2hls(r, g, b, 355 &SP->_color_table[color].red, 356 &SP->_color_table[color].green, 357 &SP->_color_table[color].blue); 358 else { 359 SP->_color_table[color].red = r; 360 SP->_color_table[color].green = g; 361 SP->_color_table[color].blue = b; 362 } 363 364 if (initialize_color) { 365 TPUTS_TRACE("initialize_color"); 366 putp(tparm(initialize_color, color, r, g, b)); 367 } 368 returnCode(OK); 369 } 370 371 bool 372 can_change_color(void) 373 { 374 T((T_CALLED("can_change_color()"))); 375 returnCode((can_change != 0) ? TRUE : FALSE); 376 } 377 378 bool 379 has_colors(void) 380 { 381 T((T_CALLED("has_colors()"))); 382 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs) 383 && (((set_foreground != NULL) 384 && (set_background != NULL)) 385 || ((set_a_foreground != NULL) 386 && (set_a_background != NULL)) 387 || set_color_pair)) ? TRUE : FALSE); 388 } 389 390 int 391 color_content(short color, short *r, short *g, short *b) 392 { 393 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b)); 394 if (color < 0 || color >= COLORS) 395 returnCode(ERR); 396 397 if (r) 398 *r = SP->_color_table[color].red; 399 if (g) 400 *g = SP->_color_table[color].green; 401 if (b) 402 *b = SP->_color_table[color].blue; 403 returnCode(OK); 404 } 405 406 int 407 pair_content(short pair, short *f, short *b) 408 { 409 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b)); 410 411 if ((pair < 0) || (pair >= COLOR_PAIRS)) 412 returnCode(ERR); 413 if (f) 414 *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK); 415 if (b) 416 *b = (SP->_color_pairs[pair] & C_MASK); 417 418 returnCode(OK); 419 } 420 421 void 422 _nc_do_color(int old_pair, int pair, bool reverse, int (*outc) (int)) 423 { 424 short fg = C_MASK, bg = C_MASK; 425 short old_fg, old_bg; 426 427 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 short 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