1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
36 * Handles color emulation of SYS V curses
40 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_color.c,v 1.24 1998/02/11 12:13:58 tom Exp $")
47 * Only 8 ANSI colors are defined; the ISO 6429 control sequences work only
50 #define MAX_ANSI_COLOR 8
53 * These should be screen structure members. They need to be globals for
54 * hystorical reasons. So we assign them in start_color() and also in
55 * set_term()'s screen-switching logic.
61 * Given a RGB range of 0..1000, we'll normally set the individual values
62 * to about 2/3 of the maximum, leaving full-range for bold/bright colors.
67 static const color_t cga_palette[] =
70 {RGB_OFF, RGB_OFF, RGB_OFF}, /* COLOR_BLACK */
71 {RGB_ON, RGB_OFF, RGB_OFF}, /* COLOR_RED */
72 {RGB_OFF, RGB_ON, RGB_OFF}, /* COLOR_GREEN */
73 {RGB_ON, RGB_ON, RGB_OFF}, /* COLOR_YELLOW */
74 {RGB_OFF, RGB_OFF, RGB_ON}, /* COLOR_BLUE */
75 {RGB_ON, RGB_OFF, RGB_ON}, /* COLOR_MAGENTA */
76 {RGB_OFF, RGB_ON, RGB_ON}, /* COLOR_CYAN */
77 {RGB_ON, RGB_ON, RGB_ON}, /* COLOR_WHITE */
79 static const color_t hls_palette[] =
82 {0, 0, 0}, /* COLOR_BLACK */
83 {120, 50, 100}, /* COLOR_RED */
84 {240, 50, 100}, /* COLOR_GREEN */
85 {180, 50, 100}, /* COLOR_YELLOW */
86 {330, 50, 100}, /* COLOR_BLUE */
87 {60, 50, 100}, /* COLOR_MAGENTA */
88 {300, 50, 100}, /* COLOR_CYAN */
89 {0, 50, 100}, /* COLOR_WHITE */
94 T((T_CALLED("start_color()")));
97 if (orig_pair != NULL)
99 TPUTS_TRACE("orig_pair");
102 #endif /* orig_pair */
104 if (orig_colors != NULL)
106 TPUTS_TRACE("orig_colors");
109 #endif /* orig_colors */
110 #if defined(orig_pair) && defined(orig_colors)
111 if (!orig_pair && !orig_colors)
113 #endif /* defined(orig_pair) && defined(orig_colors) */
115 COLOR_PAIRS = SP->_pair_count = max_pairs;
118 SP->_color_pairs = typeCalloc(unsigned short, max_pairs);
119 SP->_color_pairs[0] = PAIR_OF(COLOR_WHITE, COLOR_BLACK);
120 if (max_colors != -1)
121 COLORS = SP->_color_count = max_colors;
126 SP->_color_table = malloc(sizeof(color_t) * COLORS);
127 #ifdef hue_lightness_saturation
128 if (hue_lightness_saturation)
129 memcpy(SP->_color_table, hls_palette, sizeof(color_t) * COLORS);
131 #endif /* hue_lightness_saturation */
132 memcpy(SP->_color_table, cga_palette, sizeof(color_t) * COLORS);
136 TPUTS_TRACE("orig_colors");
140 T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS));
145 #ifdef hue_lightness_saturation
146 /* This function was originally written by Daniel Weaver <danw@znyx.com> */
147 static void rgb2hls(short r, short g, short b, short *h, short *l, short *s)
148 /* convert RGB to HLS system */
152 if ((min = g < r ? g : r) > b) min = b;
153 if ((max = g > r ? g : r) < b) max = b;
155 /* calculate lightness */
156 *l = (min + max) / 20;
158 if (min == max) /* black, white and all shades of gray */
165 /* calculate saturation */
167 *s = ((max - min) * 100) / (max + min);
168 else *s = ((max - min) * 100) / (2000 - max - min);
172 t = 120 + ((g - b) * 60) / (max - min);
175 t = 240 + ((b - r) * 60) / (max - min);
177 t = 360 + ((r - g) * 60) / (max - min);
181 #endif /* hue_lightness_saturation */
184 * Extension (1997/1/18) - Allow negative f/b values to set default color
187 int init_pair(short pair, short f, short b)
189 T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
191 if ((pair < 1) || (pair >= COLOR_PAIRS))
193 if (SP->_default_color)
199 if (f >= COLORS && f != C_MASK)
201 if (b >= COLORS && b != C_MASK)
205 if ((f < 0) || (f >= COLORS)
206 || (b < 0) || (b >= COLORS))
210 * FIXME: when a pair's content is changed, replace its colors
211 * (if pair was initialized before a screen update is performed
212 * replacing original pair colors with the new ones)
215 SP->_color_pairs[pair] = PAIR_OF(f,b);
219 const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
221 T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
223 tp[f].red, tp[f].green, tp[f].blue,
224 tp[b].red, tp[b].green, tp[b].blue));
228 TPUTS_TRACE("initialize_pair");
229 putp(tparm(initialize_pair,
231 tp[f].red, tp[f].green, tp[f].blue,
232 tp[b].red, tp[b].green, tp[b].blue));
239 int init_color(short color, short r, short g, short b)
241 T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
242 #ifdef initialize_color
243 if (initialize_color == NULL)
245 #endif /* initialize_color */
247 if (color < 0 || color >= COLORS)
249 if (r < 0 || r > 1000 || g < 0 || g > 1000 || b < 0 || b > 1000)
252 #ifdef hue_lightness_saturation
253 if (hue_lightness_saturation)
255 &SP->_color_table[color].red,
256 &SP->_color_table[color].green,
257 &SP->_color_table[color].blue);
259 #endif /* hue_lightness_saturation */
261 SP->_color_table[color].red = r;
262 SP->_color_table[color].green = g;
263 SP->_color_table[color].blue = b;
266 #ifdef initialize_color
267 if (initialize_color)
269 TPUTS_TRACE("initialize_color");
270 putp(tparm(initialize_color, color, r, g, b));
272 #endif /* initialize_color */
276 bool can_change_color(void)
278 T((T_CALLED("can_change_color()")));
279 returnCode ((can_change != 0) ? TRUE : FALSE);
282 bool has_colors(void)
284 T((T_CALLED("has_colors()")));
285 returnCode (((orig_pair != NULL || orig_colors != NULL)
286 && (max_colors != -1) && (max_pairs != -1)
287 && (((set_foreground != NULL)
288 && (set_background != NULL))
289 || ((set_a_foreground != NULL)
290 && (set_a_background != NULL))
291 || set_color_pair)) ? TRUE : FALSE);
294 int color_content(short color, short *r, short *g, short *b)
296 T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
297 if (color < 0 || color > COLORS)
300 if (r) *r = SP->_color_table[color].red;
301 if (g) *g = SP->_color_table[color].green;
302 if (b) *b = SP->_color_table[color].blue;
306 int pair_content(short pair, short *f, short *b)
308 T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
310 if ((pair < 0) || (pair > COLOR_PAIRS))
312 if (f) *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
313 if (b) *b = (SP->_color_pairs[pair] & C_MASK);
319 * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
320 * to maintain compatibility with a pre-ANSI scheme. The same scheme is
321 * also used in the FreeBSD syscons.
323 static int toggled_colors(int c)
326 static const int table[] =
327 { 0, 4, 2, 6, 1, 5, 3, 7,
328 8, 12, 10, 14, 9, 13, 11, 15};
334 void _nc_do_color(int pair, bool reverse, int (*outc)(int))
345 TPUTS_TRACE("orig_pair");
346 tputs(orig_pair, 1, outc);
353 TPUTS_TRACE("set_color_pair");
354 tputs(tparm(set_color_pair, pair), 1, outc);
358 pair_content(pair, &fg, &bg);
365 T(("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg));
367 if (fg == C_MASK || bg == C_MASK)
371 TPUTS_TRACE("orig_pair");
372 tputs(orig_pair, 1, outc);
376 TPUTS_TRACE("orig_colors");
377 tputs(orig_colors, 1, outc);
382 if (set_a_foreground && fg <= MAX_ANSI_COLOR)
384 TPUTS_TRACE("set_a_foreground");
385 tputs(tparm(set_a_foreground, fg), 1, outc);
389 TPUTS_TRACE("set_foreground");
390 tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
395 if (set_a_background && bg <= MAX_ANSI_COLOR)
397 TPUTS_TRACE("set_a_background");
398 tputs(tparm(set_a_background, bg), 1, outc);
402 TPUTS_TRACE("set_background");
403 tputs(tparm(set_background, toggled_colors(bg)), 1, outc);