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