libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / base / lib_color.c
blob1322600c340054d288ad9f3e2bfb0c2ed7040267
1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 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 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
36 /* lib_color.c
38 * Handles color emulation of SYS V curses
41 #include <curses.priv.h>
42 #include <tic.h>
44 #ifndef CUR
45 #define CUR SP_TERMTYPE
46 #endif
48 MODULE_ID("$Id: lib_color.c,v 1.110 2014/03/08 20:04:44 tom Exp $")
50 #ifdef USE_TERM_DRIVER
51 #define CanChange InfoOf(SP_PARM).canchange
52 #define DefaultPalette InfoOf(SP_PARM).defaultPalette
53 #define HasColor InfoOf(SP_PARM).hascolor
54 #define InitColor InfoOf(SP_PARM).initcolor
55 #define MaxColors InfoOf(SP_PARM).maxcolors
56 #define MaxPairs InfoOf(SP_PARM).maxpairs
57 #define UseHlsPalette (DefaultPalette == _nc_hls_palette)
58 #else
59 #define CanChange can_change
60 #define DefaultPalette (hue_lightness_saturation ? hls_palette : cga_palette)
61 #define HasColor has_color
62 #define InitColor initialize_color
63 #define MaxColors max_colors
64 #define MaxPairs max_pairs
65 #define UseHlsPalette (hue_lightness_saturation)
66 #endif
68 #ifndef USE_TERM_DRIVER
70 * These should be screen structure members. They need to be globals for
71 * historical reasons. So we assign them in start_color() and also in
72 * set_term()'s screen-switching logic.
74 #if USE_REENTRANT
75 NCURSES_EXPORT(int)
76 NCURSES_PUBLIC_VAR(COLOR_PAIRS) (void)
78 return SP ? SP->_pair_count : -1;
80 NCURSES_EXPORT(int)
81 NCURSES_PUBLIC_VAR(COLORS) (void)
83 return SP ? SP->_color_count : -1;
85 #else
86 NCURSES_EXPORT_VAR(int) COLOR_PAIRS = 0;
87 NCURSES_EXPORT_VAR(int) COLORS = 0;
88 #endif
89 #endif /* !USE_TERM_DRIVER */
91 #define DATA(r,g,b) {r,g,b, 0,0,0, 0}
93 #define TYPE_CALLOC(type,elts) typeCalloc(type, (unsigned)(elts))
95 #define MAX_PALETTE 8
97 #define OkColorHi(n) (((n) < COLORS) && ((n) < maxcolors))
98 #define InPalette(n) ((n) >= 0 && (n) < MAX_PALETTE)
101 * Given a RGB range of 0..1000, we'll normally set the individual values
102 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
104 #define RGB_ON 680
105 #define RGB_OFF 0
106 /* *INDENT-OFF* */
107 static const color_t cga_palette[] =
109 /* R G B */
110 DATA(RGB_OFF, RGB_OFF, RGB_OFF), /* COLOR_BLACK */
111 DATA(RGB_ON, RGB_OFF, RGB_OFF), /* COLOR_RED */
112 DATA(RGB_OFF, RGB_ON, RGB_OFF), /* COLOR_GREEN */
113 DATA(RGB_ON, RGB_ON, RGB_OFF), /* COLOR_YELLOW */
114 DATA(RGB_OFF, RGB_OFF, RGB_ON), /* COLOR_BLUE */
115 DATA(RGB_ON, RGB_OFF, RGB_ON), /* COLOR_MAGENTA */
116 DATA(RGB_OFF, RGB_ON, RGB_ON), /* COLOR_CYAN */
117 DATA(RGB_ON, RGB_ON, RGB_ON), /* COLOR_WHITE */
120 static const color_t hls_palette[] =
122 /* H L S */
123 DATA( 0, 0, 0), /* COLOR_BLACK */
124 DATA( 120, 50, 100), /* COLOR_RED */
125 DATA( 240, 50, 100), /* COLOR_GREEN */
126 DATA( 180, 50, 100), /* COLOR_YELLOW */
127 DATA( 330, 50, 100), /* COLOR_BLUE */
128 DATA( 60, 50, 100), /* COLOR_MAGENTA */
129 DATA( 300, 50, 100), /* COLOR_CYAN */
130 DATA( 0, 50, 100), /* COLOR_WHITE */
133 #ifdef USE_TERM_DRIVER
134 NCURSES_EXPORT_VAR(const color_t*) _nc_cga_palette = cga_palette;
135 NCURSES_EXPORT_VAR(const color_t*) _nc_hls_palette = hls_palette;
136 #endif
138 /* *INDENT-ON* */
141 * Ensure that we use color pairs only when colors have been started, and also
142 * that the index is within the limits of the table which we allocated.
144 #define ValidPair(pair) \
145 ((SP_PARM != 0) && (pair >= 0) && (pair < SP_PARM->_pair_limit) && SP_PARM->_coloron)
147 #if NCURSES_EXT_FUNCS
149 * These are called from _nc_do_color(), which in turn is called from
150 * vidattr - so we have to assume that sp may be null.
152 static int
153 default_fg(NCURSES_SP_DCL0)
155 return (SP_PARM != 0) ? SP_PARM->_default_fg : COLOR_WHITE;
158 static int
159 default_bg(NCURSES_SP_DCL0)
161 return SP_PARM != 0 ? SP_PARM->_default_bg : COLOR_BLACK;
163 #else
164 #define default_fg(sp) COLOR_WHITE
165 #define default_bg(sp) COLOR_BLACK
166 #endif
168 #ifndef USE_TERM_DRIVER
170 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
171 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
172 * also used in the FreeBSD syscons.
174 static int
175 toggled_colors(int c)
177 if (c < 16) {
178 static const int table[] =
179 {0, 4, 2, 6, 1, 5, 3, 7,
180 8, 12, 10, 14, 9, 13, 11, 15};
181 c = table[c];
183 return c;
185 #endif
187 static void
188 set_background_color(NCURSES_SP_DCLx int bg, NCURSES_SP_OUTC outc)
190 #ifdef USE_TERM_DRIVER
191 CallDriver_3(SP_PARM, td_color, FALSE, bg, outc);
192 #else
193 if (set_a_background) {
194 TPUTS_TRACE("set_a_background");
195 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
196 TPARM_1(set_a_background, bg),
197 1, outc);
198 } else {
199 TPUTS_TRACE("set_background");
200 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
201 TPARM_1(set_background, toggled_colors(bg)),
202 1, outc);
204 #endif
207 static void
208 set_foreground_color(NCURSES_SP_DCLx int fg, NCURSES_SP_OUTC outc)
210 #ifdef USE_TERM_DRIVER
211 CallDriver_3(SP_PARM, td_color, TRUE, fg, outc);
212 #else
213 if (set_a_foreground) {
214 TPUTS_TRACE("set_a_foreground");
215 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
216 TPARM_1(set_a_foreground, fg),
217 1, outc);
218 } else {
219 TPUTS_TRACE("set_foreground");
220 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
221 TPARM_1(set_foreground, toggled_colors(fg)),
222 1, outc);
224 #endif
227 static void
228 init_color_table(NCURSES_SP_DCL0)
230 const color_t *tp = DefaultPalette;
231 int n;
233 assert(tp != 0);
235 for (n = 0; n < COLORS; n++) {
236 if (InPalette(n)) {
237 SP_PARM->_color_table[n] = tp[n];
238 } else {
239 SP_PARM->_color_table[n] = tp[n % MAX_PALETTE];
240 if (UseHlsPalette) {
241 SP_PARM->_color_table[n].green = 100;
242 } else {
243 if (SP_PARM->_color_table[n].red)
244 SP_PARM->_color_table[n].red = 1000;
245 if (SP_PARM->_color_table[n].green)
246 SP_PARM->_color_table[n].green = 1000;
247 if (SP_PARM->_color_table[n].blue)
248 SP_PARM->_color_table[n].blue = 1000;
255 * Reset the color pair, e.g., to whatever color pair 0 is.
257 static bool
258 reset_color_pair(NCURSES_SP_DCL0)
260 #ifdef USE_TERM_DRIVER
261 return CallDriver(SP_PARM, td_rescol);
262 #else
263 bool result = FALSE;
265 (void) SP_PARM;
266 if (orig_pair != 0) {
267 (void) NCURSES_PUTP2("orig_pair", orig_pair);
268 result = TRUE;
270 return result;
271 #endif
275 * Reset color pairs and definitions. Actually we do both more to accommodate
276 * badly-written terminal descriptions than for the relatively rare case where
277 * someone has changed the color definitions.
279 NCURSES_EXPORT(bool)
280 NCURSES_SP_NAME(_nc_reset_colors) (NCURSES_SP_DCL0)
282 int result = FALSE;
284 T((T_CALLED("_nc_reset_colors(%p)"), (void *) SP_PARM));
285 if (SP_PARM->_color_defs > 0)
286 SP_PARM->_color_defs = -(SP_PARM->_color_defs);
287 if (reset_color_pair(NCURSES_SP_ARG))
288 result = TRUE;
290 #ifdef USE_TERM_DRIVER
291 result = CallDriver(SP_PARM, td_rescolors);
292 #else
293 if (orig_colors != 0) {
294 NCURSES_PUTP2("orig_colors", orig_colors);
295 result = TRUE;
297 #endif
298 returnBool(result);
301 #if NCURSES_SP_FUNCS
302 NCURSES_EXPORT(bool)
303 _nc_reset_colors(void)
305 return NCURSES_SP_NAME(_nc_reset_colors) (CURRENT_SCREEN);
307 #endif
309 NCURSES_EXPORT(int)
310 NCURSES_SP_NAME(start_color) (NCURSES_SP_DCL0)
312 int result = ERR;
313 int maxpairs = 0, maxcolors = 0;
315 T((T_CALLED("start_color(%p)"), (void *) SP_PARM));
317 if (SP_PARM == 0) {
318 result = ERR;
319 } else if (SP_PARM->_coloron) {
320 result = OK;
321 } else {
322 maxpairs = MaxPairs;
323 maxcolors = MaxColors;
324 if (reset_color_pair(NCURSES_SP_ARG) != TRUE) {
325 set_foreground_color(NCURSES_SP_ARGx
326 default_fg(NCURSES_SP_ARG),
327 NCURSES_SP_NAME(_nc_outch));
328 set_background_color(NCURSES_SP_ARGx
329 default_bg(NCURSES_SP_ARG),
330 NCURSES_SP_NAME(_nc_outch));
332 #if !NCURSES_EXT_COLORS
334 * Without ext-colors, we cannot represent more than 256 color pairs.
336 if (maxpairs > 256)
337 maxpairs = 256;
338 #endif
340 if (maxpairs > 0 && maxcolors > 0) {
341 SP_PARM->_pair_limit = maxpairs;
343 #if NCURSES_EXT_FUNCS
345 * If using default colors, allocate extra space in table to
346 * allow for default-color as a component of a color-pair.
348 SP_PARM->_pair_limit += (1 + (2 * maxcolors));
349 #endif
350 SP_PARM->_pair_count = maxpairs;
351 SP_PARM->_color_count = maxcolors;
352 #if !USE_REENTRANT
353 COLOR_PAIRS = maxpairs;
354 COLORS = maxcolors;
355 #endif
357 SP_PARM->_color_pairs = TYPE_CALLOC(colorpair_t, SP_PARM->_pair_limit);
358 if (SP_PARM->_color_pairs != 0) {
359 SP_PARM->_color_table = TYPE_CALLOC(color_t, maxcolors);
360 if (SP_PARM->_color_table != 0) {
361 SP_PARM->_color_pairs[0] = PAIR_OF(default_fg(NCURSES_SP_ARG),
362 default_bg(NCURSES_SP_ARG));
363 init_color_table(NCURSES_SP_ARG);
365 T(("started color: COLORS = %d, COLOR_PAIRS = %d",
366 COLORS, COLOR_PAIRS));
368 SP_PARM->_coloron = 1;
369 result = OK;
370 } else if (SP_PARM->_color_pairs != 0) {
371 FreeAndNull(SP_PARM->_color_pairs);
374 } else {
375 result = OK;
378 returnCode(result);
381 #if NCURSES_SP_FUNCS
382 NCURSES_EXPORT(int)
383 start_color(void)
385 return NCURSES_SP_NAME(start_color) (CURRENT_SCREEN);
387 #endif
389 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
390 static void
391 rgb2hls(int r, int g, int b, NCURSES_COLOR_T *h, NCURSES_COLOR_T *l, NCURSES_COLOR_T *s)
392 /* convert RGB to HLS system */
394 int min, max, t;
396 if ((min = g < r ? g : r) > b)
397 min = b;
398 if ((max = g > r ? g : r) < b)
399 max = b;
401 /* calculate lightness */
402 *l = (NCURSES_COLOR_T) ((min + max) / 20);
404 if (min == max) { /* black, white and all shades of gray */
405 *h = 0;
406 *s = 0;
407 return;
410 /* calculate saturation */
411 if (*l < 50)
412 *s = (NCURSES_COLOR_T) (((max - min) * 100) / (max + min));
413 else
414 *s = (NCURSES_COLOR_T) (((max - min) * 100) / (2000 - max - min));
416 /* calculate hue */
417 if (r == max)
418 t = (NCURSES_COLOR_T) (120 + ((g - b) * 60) / (max - min));
419 else if (g == max)
420 t = (NCURSES_COLOR_T) (240 + ((b - r) * 60) / (max - min));
421 else
422 t = (NCURSES_COLOR_T) (360 + ((r - g) * 60) / (max - min));
424 *h = (NCURSES_COLOR_T) (t % 360);
428 * Extension (1997/1/18) - Allow negative f/b values to set default color
429 * values.
431 NCURSES_EXPORT(int)
432 NCURSES_SP_NAME(init_pair) (NCURSES_SP_DCLx
433 NCURSES_PAIRS_T pair,
434 NCURSES_COLOR_T f,
435 NCURSES_COLOR_T b)
437 colorpair_t result;
438 colorpair_t previous;
439 int maxcolors;
441 T((T_CALLED("init_pair(%p,%d,%d,%d)"),
442 (void *) SP_PARM,
443 (int) pair,
444 (int) f,
445 (int) b));
447 if (!ValidPair(pair))
448 returnCode(ERR);
450 maxcolors = MaxColors;
452 previous = SP_PARM->_color_pairs[pair];
453 #if NCURSES_EXT_FUNCS
454 if (SP_PARM->_default_color || SP_PARM->_assumed_color) {
455 bool isDefault = FALSE;
456 bool wasDefault = FALSE;
457 int default_pairs = SP_PARM->_default_pairs;
460 * Map caller's color number, e.g., -1, 0, 1, .., 7, etc., into
461 * internal unsigned values which we will store in the _color_pairs[]
462 * table.
464 if (isDefaultColor(f)) {
465 f = COLOR_DEFAULT;
466 isDefault = TRUE;
467 } else if (!OkColorHi(f)) {
468 returnCode(ERR);
471 if (isDefaultColor(b)) {
472 b = COLOR_DEFAULT;
473 isDefault = TRUE;
474 } else if (!OkColorHi(b)) {
475 returnCode(ERR);
479 * Check if the table entry that we are going to init/update used
480 * default colors.
482 if ((FORE_OF(previous) == COLOR_DEFAULT)
483 || (BACK_OF(previous) == COLOR_DEFAULT))
484 wasDefault = TRUE;
487 * Keep track of the number of entries in the color pair table which
488 * used a default color.
490 if (isDefault && !wasDefault) {
491 ++default_pairs;
492 } else if (wasDefault && !isDefault) {
493 --default_pairs;
497 * As an extension, ncurses allows the pair number to exceed the
498 * terminal's color_pairs value for pairs using a default color.
500 * Note that updating a pair which used a default color with one
501 * that does not will decrement the count - and possibly interfere
502 * with sequentially adding new pairs.
504 if (pair > (SP_PARM->_pair_count + default_pairs)) {
505 returnCode(ERR);
507 SP_PARM->_default_pairs = default_pairs;
508 } else
509 #endif
511 if ((f < 0) || !OkColorHi(f)
512 || (b < 0) || !OkColorHi(b)
513 || (pair < 1)) {
514 returnCode(ERR);
519 * When a pair's content is changed, replace its colors (if pair was
520 * initialized before a screen update is performed replacing original
521 * pair colors with the new ones).
523 result = PAIR_OF(f, b);
524 if (previous != 0
525 && previous != result) {
526 int y, x;
528 for (y = 0; y <= CurScreen(SP_PARM)->_maxy; y++) {
529 struct ldat *ptr = &(CurScreen(SP_PARM)->_line[y]);
530 bool changed = FALSE;
531 for (x = 0; x <= CurScreen(SP_PARM)->_maxx; x++) {
532 if (GetPair(ptr->text[x]) == pair) {
533 /* Set the old cell to zero to ensure it will be
534 updated on the next doupdate() */
535 SetChar(ptr->text[x], 0, 0);
536 CHANGED_CELL(ptr, x);
537 changed = TRUE;
540 if (changed)
541 NCURSES_SP_NAME(_nc_make_oldhash) (NCURSES_SP_ARGx y);
545 SP_PARM->_color_pairs[pair] = result;
546 if (GET_SCREEN_PAIR(SP_PARM) == pair)
547 SET_SCREEN_PAIR(SP_PARM, (chtype) (~0)); /* force attribute update */
549 #ifdef USE_TERM_DRIVER
550 CallDriver_3(SP_PARM, td_initpair, pair, f, b);
551 #else
552 if (initialize_pair && InPalette(f) && InPalette(b)) {
553 const color_t *tp = DefaultPalette;
555 TR(TRACE_ATTRS,
556 ("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
557 (int) pair,
558 (int) tp[f].red, (int) tp[f].green, (int) tp[f].blue,
559 (int) tp[b].red, (int) tp[b].green, (int) tp[b].blue));
561 NCURSES_PUTP2("initialize_pair",
562 TPARM_7(initialize_pair,
563 pair,
564 (int) tp[f].red,
565 (int) tp[f].green,
566 (int) tp[f].blue,
567 (int) tp[b].red,
568 (int) tp[b].green,
569 (int) tp[b].blue));
571 #endif
573 returnCode(OK);
576 #if NCURSES_SP_FUNCS
577 NCURSES_EXPORT(int)
578 init_pair(NCURSES_COLOR_T pair, NCURSES_COLOR_T f, NCURSES_COLOR_T b)
580 return NCURSES_SP_NAME(init_pair) (CURRENT_SCREEN, pair, f, b);
582 #endif
584 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
586 NCURSES_EXPORT(int)
587 NCURSES_SP_NAME(init_color) (NCURSES_SP_DCLx
588 NCURSES_COLOR_T color,
589 NCURSES_COLOR_T r,
590 NCURSES_COLOR_T g,
591 NCURSES_COLOR_T b)
593 int result = ERR;
594 int maxcolors;
596 T((T_CALLED("init_color(%p,%d,%d,%d,%d)"),
597 (void *) SP_PARM,
598 color,
599 r, g, b));
601 if (SP_PARM == 0)
602 returnCode(result);
604 maxcolors = MaxColors;
606 if (InitColor
607 && SP_PARM->_coloron
608 && (color >= 0 && OkColorHi(color))
609 && (okRGB(r) && okRGB(g) && okRGB(b))) {
611 SP_PARM->_color_table[color].init = 1;
612 SP_PARM->_color_table[color].r = r;
613 SP_PARM->_color_table[color].g = g;
614 SP_PARM->_color_table[color].b = b;
616 if (UseHlsPalette) {
617 rgb2hls(r, g, b,
618 &SP_PARM->_color_table[color].red,
619 &SP_PARM->_color_table[color].green,
620 &SP_PARM->_color_table[color].blue);
621 } else {
622 SP_PARM->_color_table[color].red = r;
623 SP_PARM->_color_table[color].green = g;
624 SP_PARM->_color_table[color].blue = b;
627 #ifdef USE_TERM_DRIVER
628 CallDriver_4(SP_PARM, td_initcolor, color, r, g, b);
629 #else
630 NCURSES_PUTP2("initialize_color",
631 TPARM_4(initialize_color, color, r, g, b));
632 #endif
633 SP_PARM->_color_defs = max(color + 1, SP_PARM->_color_defs);
635 result = OK;
637 returnCode(result);
640 #if NCURSES_SP_FUNCS
641 NCURSES_EXPORT(int)
642 init_color(NCURSES_COLOR_T color,
643 NCURSES_COLOR_T r,
644 NCURSES_COLOR_T g,
645 NCURSES_COLOR_T b)
647 return NCURSES_SP_NAME(init_color) (CURRENT_SCREEN, color, r, g, b);
649 #endif
651 NCURSES_EXPORT(bool)
652 NCURSES_SP_NAME(can_change_color) (NCURSES_SP_DCL)
654 int result = FALSE;
656 T((T_CALLED("can_change_color(%p)"), (void *) SP_PARM));
658 if (HasTerminal(SP_PARM) && (CanChange != 0)) {
659 result = TRUE;
662 returnCode(result);
665 #if NCURSES_SP_FUNCS
666 NCURSES_EXPORT(bool)
667 can_change_color(void)
669 return NCURSES_SP_NAME(can_change_color) (CURRENT_SCREEN);
671 #endif
673 NCURSES_EXPORT(bool)
674 NCURSES_SP_NAME(has_colors) (NCURSES_SP_DCL0)
676 int code = FALSE;
678 (void) SP_PARM;
679 T((T_CALLED("has_colors()")));
680 if (HasTerminal(SP_PARM)) {
681 #ifdef USE_TERM_DRIVER
682 code = HasColor;
683 #else
684 code = ((VALID_NUMERIC(max_colors) && VALID_NUMERIC(max_pairs)
685 && (((set_foreground != NULL)
686 && (set_background != NULL))
687 || ((set_a_foreground != NULL)
688 && (set_a_background != NULL))
689 || set_color_pair)) ? TRUE : FALSE);
690 #endif
692 returnCode(code);
695 #if NCURSES_SP_FUNCS
696 NCURSES_EXPORT(bool)
697 has_colors(void)
699 return NCURSES_SP_NAME(has_colors) (CURRENT_SCREEN);
701 #endif
703 NCURSES_EXPORT(int)
704 NCURSES_SP_NAME(color_content) (NCURSES_SP_DCLx
705 NCURSES_COLOR_T color,
706 NCURSES_COLOR_T *r,
707 NCURSES_COLOR_T *g,
708 NCURSES_COLOR_T *b)
710 int result = ERR;
711 int maxcolors;
713 T((T_CALLED("color_content(%p,%d,%p,%p,%p)"),
714 (void *) SP_PARM,
715 color,
716 (void *) r,
717 (void *) g,
718 (void *) b));
720 if (SP_PARM == 0)
721 returnCode(result);
723 maxcolors = MaxColors;
725 if (color < 0 || !OkColorHi(color) || !SP_PARM->_coloron) {
726 result = ERR;
727 } else {
728 NCURSES_COLOR_T c_r = SP_PARM->_color_table[color].red;
729 NCURSES_COLOR_T c_g = SP_PARM->_color_table[color].green;
730 NCURSES_COLOR_T c_b = SP_PARM->_color_table[color].blue;
732 if (r)
733 *r = c_r;
734 if (g)
735 *g = c_g;
736 if (b)
737 *b = c_b;
739 TR(TRACE_ATTRS, ("...color_content(%d,%d,%d,%d)",
740 color, c_r, c_g, c_b));
741 result = OK;
743 returnCode(result);
746 #if NCURSES_SP_FUNCS
747 NCURSES_EXPORT(int)
748 color_content(NCURSES_COLOR_T color,
749 NCURSES_COLOR_T *r,
750 NCURSES_COLOR_T *g,
751 NCURSES_COLOR_T *b)
753 return NCURSES_SP_NAME(color_content) (CURRENT_SCREEN, color, r, g, b);
755 #endif
757 NCURSES_EXPORT(int)
758 NCURSES_SP_NAME(pair_content) (NCURSES_SP_DCLx
759 NCURSES_PAIRS_T pair,
760 NCURSES_COLOR_T *f,
761 NCURSES_COLOR_T *b)
763 int result;
765 T((T_CALLED("pair_content(%p,%d,%p,%p)"),
766 (void *) SP_PARM,
767 (int) pair,
768 (void *) f,
769 (void *) b));
771 if (!ValidPair(pair)) {
772 result = ERR;
773 } else {
774 NCURSES_COLOR_T fg = (NCURSES_COLOR_T) FORE_OF(SP_PARM->_color_pairs[pair]);
775 NCURSES_COLOR_T bg = (NCURSES_COLOR_T) BACK_OF(SP_PARM->_color_pairs[pair]);
777 #if NCURSES_EXT_FUNCS
778 if (fg == COLOR_DEFAULT)
779 fg = -1;
780 if (bg == COLOR_DEFAULT)
781 bg = -1;
782 #endif
784 if (f)
785 *f = fg;
786 if (b)
787 *b = bg;
789 TR(TRACE_ATTRS, ("...pair_content(%p,%d,%d,%d)",
790 (void *) SP_PARM,
791 (int) pair,
792 (int) fg, (int) bg));
793 result = OK;
795 returnCode(result);
798 #if NCURSES_SP_FUNCS
799 NCURSES_EXPORT(int)
800 pair_content(NCURSES_COLOR_T pair, NCURSES_COLOR_T *f, NCURSES_COLOR_T *b)
802 return NCURSES_SP_NAME(pair_content) (CURRENT_SCREEN, pair, f, b);
804 #endif
806 NCURSES_EXPORT(void)
807 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_DCLx
808 int old_pair,
809 int pair,
810 int reverse,
811 NCURSES_SP_OUTC outc)
813 #ifdef USE_TERM_DRIVER
814 CallDriver_4(SP_PARM, td_docolor, old_pair, pair, reverse, outc);
815 #else
816 NCURSES_COLOR_T fg = COLOR_DEFAULT;
817 NCURSES_COLOR_T bg = COLOR_DEFAULT;
818 NCURSES_COLOR_T old_fg = -1;
819 NCURSES_COLOR_T old_bg = -1;
821 if (!ValidPair(pair)) {
822 return;
823 } else if (pair != 0) {
824 if (set_color_pair) {
825 TPUTS_TRACE("set_color_pair");
826 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
827 TPARM_1(set_color_pair, pair),
828 1, outc);
829 return;
830 } else if (SP_PARM != 0) {
831 if (pair_content((NCURSES_COLOR_T) pair, &fg, &bg) == ERR)
832 return;
836 if (old_pair >= 0
837 && SP_PARM != 0
838 && pair_content((NCURSES_COLOR_T) old_pair, &old_fg, &old_bg) != ERR) {
839 if ((isDefaultColor(fg) && !isDefaultColor(old_fg))
840 || (isDefaultColor(bg) && !isDefaultColor(old_bg))) {
841 #if NCURSES_EXT_FUNCS
843 * A minor optimization - but extension. If "AX" is specified in
844 * the terminal description, treat it as screen's indicator of ECMA
845 * SGR 39 and SGR 49, and assume the two sequences are independent.
847 if (SP_PARM->_has_sgr_39_49
848 && isDefaultColor(old_bg)
849 && !isDefaultColor(old_fg)) {
850 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[39m", 1, outc);
851 } else if (SP_PARM->_has_sgr_39_49
852 && isDefaultColor(old_fg)
853 && !isDefaultColor(old_bg)) {
854 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx "\033[49m", 1, outc);
855 } else
856 #endif
857 reset_color_pair(NCURSES_SP_ARG);
859 } else {
860 reset_color_pair(NCURSES_SP_ARG);
861 if (old_pair < 0)
862 return;
865 #if NCURSES_EXT_FUNCS
866 if (isDefaultColor(fg))
867 fg = (NCURSES_COLOR_T) default_fg(NCURSES_SP_ARG);
868 if (isDefaultColor(bg))
869 bg = (NCURSES_COLOR_T) default_bg(NCURSES_SP_ARG);
870 #endif
872 if (reverse) {
873 NCURSES_COLOR_T xx = fg;
874 fg = bg;
875 bg = xx;
878 TR(TRACE_ATTRS, ("setting colors: pair = %d, fg = %d, bg = %d", pair,
879 fg, bg));
881 if (!isDefaultColor(fg)) {
882 set_foreground_color(NCURSES_SP_ARGx fg, outc);
884 if (!isDefaultColor(bg)) {
885 set_background_color(NCURSES_SP_ARGx bg, outc);
887 #endif
890 #if NCURSES_SP_FUNCS
891 NCURSES_EXPORT(void)
892 _nc_do_color(int old_pair, int pair, int reverse, NCURSES_OUTC outc)
894 SetSafeOutcWrapper(outc);
895 NCURSES_SP_NAME(_nc_do_color) (CURRENT_SCREEN,
896 old_pair,
897 pair,
898 reverse,
899 _nc_outc_wrapper);
901 #endif