1 /* $OpenBSD: lib_color.c,v 1.6 2000/03/10 01:35:02 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.45 2000/02/27 00:20:31 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 static int 92 default_fg(void) 93 { 94 return (SP->_default_fg != C_MASK) ? SP->_default_fg : COLOR_WHITE; 95 } 96 97 static int 98 default_bg(void) 99 { 100 return (SP->_default_bg != C_MASK) ? SP->_default_bg : COLOR_BLACK; 101 } 102 #else 103 #define default_fg() COLOR_WHITE 104 #define default_bg() COLOR_BLACK 105 #endif 106 107 /* 108 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly 109 * to maintain compatibility with a pre-ANSI scheme. The same scheme is 110 * also used in the FreeBSD syscons. 111 */ 112 static int 113 toggled_colors(int c) 114 { 115 if (c < 16) { 116 static const int table[] = 117 {0, 4, 2, 6, 1, 5, 3, 7, 118 8, 12, 10, 14, 9, 13, 11, 15}; 119 c = table[c]; 120 } 121 return c; 122 } 123 124 static void 125 set_background_color(int bg, int (*outc) (int)) 126 { 127 if (set_a_background) { 128 TPUTS_TRACE("set_a_background"); 129 tputs(tparm(set_a_background, bg), 1, outc); 130 } else { 131 TPUTS_TRACE("set_background"); 132 tputs(tparm(set_background, toggled_colors(bg)), 1, outc); 133 } 134 } 135 136 static void 137 set_foreground_color(int fg, int (*outc) (int)) 138 { 139 if (set_a_foreground) { 140 TPUTS_TRACE("set_a_foreground"); 141 tputs(tparm(set_a_foreground, fg), 1, outc); 142 } else { 143 TPUTS_TRACE("set_foreground"); 144 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc); 145 } 146 } 147 148 static bool 149 set_original_colors(void) 150 { 151 if (orig_pair != 0) { 152 TPUTS_TRACE("orig_pair"); 153 putp(orig_pair); 154 return TRUE; 155 } else if (orig_colors != NULL) { 156 TPUTS_TRACE("orig_colors"); 157 putp(orig_colors); 158 return TRUE; 159 } 160 return FALSE; 161 } 162 163 int 164 start_color(void) 165 { 166 int n; 167 const color_t *tp; 168 169 T((T_CALLED("start_color()"))); 170 171 if (set_original_colors() != TRUE) { 172 set_foreground_color(default_fg(), _nc_outch); 173 set_background_color(default_bg(), _nc_outch); 174 } 175 176 if (VALID_NUMERIC(max_pairs)) 177 COLOR_PAIRS = SP->_pair_count = max_pairs; 178 else 179 returnCode(ERR); 180 if ((SP->_color_pairs = typeCalloc(unsigned short, max_pairs)) == 0) 181 returnCode(ERR); 182 SP->_color_pairs[0] = PAIR_OF(default_fg(), default_bg()); 183 if (VALID_NUMERIC(max_colors)) 184 COLORS = SP->_color_count = max_colors; 185 else 186 returnCode(ERR); 187 SP->_coloron = 1; 188 189 if ((SP->_color_table = typeMalloc(color_t, COLORS)) == 0) 190 returnCode(ERR); 191 tp = (hue_lightness_saturation) ? hls_palette : cga_palette; 192 for (n = 0; n < COLORS; n++) { 193 if (n < 8) { 194 SP->_color_table[n] = tp[n]; 195 } else { 196 SP->_color_table[n] = tp[n % 8]; 197 if (hue_lightness_saturation) { 198 SP->_color_table[n].green = 100; 199 } else { 200 if (SP->_color_table[n].red) 201 SP->_color_table[n].red = 1000; 202 if (SP->_color_table[n].green) 203 SP->_color_table[n].green = 1000; 204 if (SP->_color_table[n].blue) 205 SP->_color_table[n].blue = 1000; 206 } 207 } 208 } 209 210 T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS)); 211 212 returnCode(OK); 213 } 214 215 /* This function was originally written by Daniel Weaver <danw@znyx.com> */ 216 static void 217 rgb2hls(short r, short g, short b, short *h, short *l, short *s) 218 /* convert RGB to HLS system */ 219 { 220 short min, max, t; 221 222 if ((min = g < r ? g : r) > b) 223 min = b; 224 if ((max = g > r ? g : r) < b) 225 max = b; 226 227 /* calculate lightness */ 228 *l = (min + max) / 20; 229 230 if (min == max) { /* black, white and all shades of gray */ 231 *h = 0; 232 *s = 0; 233 return; 234 } 235 236 /* calculate saturation */ 237 if (*l < 50) 238 *s = ((max - min) * 100) / (max + min); 239 else 240 *s = ((max - min) * 100) / (2000 - max - min); 241 242 /* calculate hue */ 243 if (r == max) 244 t = 120 + ((g - b) * 60) / (max - min); 245 else if (g == max) 246 t = 240 + ((b - r) * 60) / (max - min); 247 else 248 t = 360 + ((r - g) * 60) / (max - min); 249 250 *h = t % 360; 251 } 252 253 /* 254 * Extension (1997/1/18) - Allow negative f/b values to set default color 255 * values. 256 */ 257 int 258 init_pair(short pair, short f, short b) 259 { 260 unsigned result; 261 262 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b)); 263 264 if ((pair < 1) || (pair >= COLOR_PAIRS)) 265 returnCode(ERR); 266 #ifdef NCURSES_EXT_FUNCS 267 if (SP->_default_color) { 268 if (f < 0) 269 f = C_MASK; 270 if (b < 0) 271 b = C_MASK; 272 if (f >= COLORS && f != C_MASK) 273 returnCode(ERR); 274 if (b >= COLORS && b != C_MASK) 275 returnCode(ERR); 276 } else 277 #endif 278 if ((f < 0) || (f >= COLORS) 279 || (b < 0) || (b >= COLORS)) 280 returnCode(ERR); 281 282 /* 283 * When a pair's content is changed, replace its colors (if pair was 284 * initialized before a screen update is performed replacing original 285 * pair colors with the new ones). 286 */ 287 result = PAIR_OF(f, b); 288 if (SP->_color_pairs[pair] != 0 289 && SP->_color_pairs[pair] != result) { 290 int y, x; 291 attr_t z = COLOR_PAIR(pair); 292 293 for (y = 0; y <= curscr->_maxy; y++) { 294 struct ldat *ptr = &(curscr->_line[y]); 295 bool changed = FALSE; 296 for (x = 0; x <= curscr->_maxx; x++) { 297 if ((ptr->text[x] & A_COLOR) == z) { 298 /* Set the old cell to zero to ensure it will be 299 updated on the next doupdate() */ 300 ptr->text[x] = 0; 301 CHANGED_CELL(ptr, x); 302 changed = TRUE; 303 } 304 } 305 if (changed) 306 _nc_make_oldhash(y); 307 } 308 } 309 SP->_color_pairs[pair] = result; 310 311 if (initialize_pair) { 312 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette; 313 314 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)", 315 pair, 316 tp[f].red, tp[f].green, tp[f].blue, 317 tp[b].red, tp[b].green, tp[b].blue)); 318 319 if (initialize_pair) { 320 TPUTS_TRACE("initialize_pair"); 321 putp(tparm(initialize_pair, 322 pair, 323 tp[f].red, tp[f].green, tp[f].blue, 324 tp[b].red, tp[b].green, tp[b].blue)); 325 } 326 } 327 328 returnCode(OK); 329 } 330 331 int 332 init_color(short color, short r, short g, short b) 333 { 334 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b)); 335 336 if (initialize_color == NULL) 337 returnCode(ERR); 338 339 if (color < 0 || color >= COLORS) 340 returnCode(ERR); 341 if (r < 0 || r > 1000 || g < 0 || g > 1000 || b < 0 || b > 1000) 342 returnCode(ERR); 343 344 if (hue_lightness_saturation) 345 rgb2hls(r, g, b, 346 &SP->_color_table[color].red, 347 &SP->_color_table[color].green, 348 &SP->_color_table[color].blue); 349 else { 350 SP->_color_table[color].red = r; 351 SP->_color_table[color].green = g; 352 SP->_color_table[color].blue = b; 353 } 354 355 if (initialize_color) { 356 TPUTS_TRACE("initialize_color"); 357 putp(tparm(initialize_color, color, r, g, b)); 358 } 359 returnCode(OK); 360 } 361 362 bool 363 can_change_color(void) 364 { 365 T((T_CALLED("can_change_color()"))); 366 returnCode((can_change != 0) ? TRUE : FALSE); 367 } 368 369 bool 370 has_colors(void) 371 { 372 T((T_CALLED("has_colors()"))); 373 returnCode((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs) 374 && (((set_foreground != NULL) 375 && (set_background != NULL)) 376 || ((set_a_foreground != NULL) 377 && (set_a_background != NULL)) 378 || set_color_pair)) ? TRUE : FALSE); 379 } 380 381 int 382 color_content(short color, short *r, short *g, short *b) 383 { 384 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b)); 385 if (color < 0 || color >= COLORS) 386 returnCode(ERR); 387 388 if (r) 389 *r = SP->_color_table[color].red; 390 if (g) 391 *g = SP->_color_table[color].green; 392 if (b) 393 *b = SP->_color_table[color].blue; 394 returnCode(OK); 395 } 396 397 int 398 pair_content(short pair, short *f, short *b) 399 { 400 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b)); 401 402 if ((pair < 0) || (pair >= COLOR_PAIRS)) 403 returnCode(ERR); 404 if (f) 405 *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK); 406 if (b) 407 *b = (SP->_color_pairs[pair] & C_MASK); 408 409 returnCode(OK); 410 } 411 412 void 413 _nc_do_color(int pair, bool reverse, int (*outc) (int)) 414 { 415 short fg, bg; 416 417 if (pair == 0) { 418 if ( 419 #ifdef NCURSES_EXT_FUNCS 420 !SP->_default_color || 421 #endif 422 !set_original_colors()) { 423 fg = default_fg(); 424 bg = default_bg(); 425 } else { 426 fg = C_MASK; 427 bg = C_MASK; 428 } 429 } else { 430 if (set_color_pair) { 431 TPUTS_TRACE("set_color_pair"); 432 tputs(tparm(set_color_pair, pair), 1, outc); 433 return; 434 } else { 435 pair_content(pair, &fg, &bg); 436 #ifdef NCURSES_EXT_FUNCS 437 if (SP->_default_color) { 438 if (fg == C_MASK) 439 fg = SP->_default_fg; 440 if (bg == C_MASK) 441 bg = SP->_default_bg; 442 } 443 #endif 444 } 445 } 446 447 if (fg == C_MASK || bg == C_MASK) { 448 if (set_original_colors() != TRUE) { 449 if (fg == C_MASK) 450 fg = default_fg(); 451 if (bg == C_MASK) 452 bg = default_bg(); 453 } 454 } 455 456 if (reverse) { 457 short xx = fg; 458 fg = bg; 459 bg = xx; 460 } 461 462 T(("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg)); 463 464 if (fg != C_MASK) { 465 set_foreground_color(fg, outc); 466 } 467 if (bg != C_MASK) { 468 set_background_color(bg, outc); 469 } 470 } 471