xref: /openbsd-src/lib/libcurses/base/lib_color.c (revision 92dd1ec0a89df25171bc5d61a3d95ea1a68cef0b)
1 /*	$OpenBSD: lib_color.c,v 1.1 1999/01/18 19:09:39 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.33 1998/09/20 00:51:51 tom 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 = malloc(sizeof(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 			ptr->text[x] &= ~A_COLOR;
264 			CHANGED_CELL(ptr,x);
265 			changed = TRUE;
266 		    }
267 		}
268 		if (changed)
269 			_nc_make_oldhash(y);
270 	    }
271 	}
272 	SP->_color_pairs[pair] = result;
273 
274 	if (initialize_pair)
275 	{
276 	    const color_t *tp = hue_lightness_saturation ? hls_palette : cga_palette;
277 
278 	    T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
279 	       pair,
280 	       tp[f].red, tp[f].green, tp[f].blue,
281 	       tp[b].red, tp[b].green, tp[b].blue));
282 
283 	    if (initialize_pair)
284 	    {
285 		TPUTS_TRACE("initialize_pair");
286 		putp(tparm(initialize_pair,
287 			    pair,
288 			    tp[f].red, tp[f].green, tp[f].blue,
289 			    tp[b].red, tp[b].green, tp[b].blue));
290 	    }
291 	}
292 
293 	returnCode(OK);
294 }
295 
296 int init_color(short color, short r, short g, short b)
297 {
298 	T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
299 
300 	if (initialize_color == NULL)
301 		returnCode(ERR);
302 
303 	if (color < 0 || color >= COLORS)
304 		returnCode(ERR);
305 	if (r < 0 || r > 1000 || g < 0 ||  g > 1000 || b < 0 || b > 1000)
306 		returnCode(ERR);
307 
308 	if (hue_lightness_saturation)
309 	    rgb2hls(r, g, b,
310 		      &SP->_color_table[color].red,
311 		      &SP->_color_table[color].green,
312 		      &SP->_color_table[color].blue);
313 	else
314 	{
315 		SP->_color_table[color].red = r;
316 		SP->_color_table[color].green = g;
317 		SP->_color_table[color].blue = b;
318 	}
319 
320 	if (initialize_color)
321 	{
322 		TPUTS_TRACE("initialize_color");
323 		putp(tparm(initialize_color, color, r, g, b));
324 	}
325 	returnCode(OK);
326 }
327 
328 bool can_change_color(void)
329 {
330 	T((T_CALLED("can_change_color()")));
331 	returnCode ((can_change != 0) ? TRUE : FALSE);
332 }
333 
334 bool has_colors(void)
335 {
336 	T((T_CALLED("has_colors()")));
337 	returnCode (((max_colors != -1) && (max_pairs != -1)
338 		     && (((set_foreground != NULL)
339 			  && (set_background != NULL))
340 			 || ((set_a_foreground != NULL)
341 			     && (set_a_background != NULL))
342 			 || set_color_pair)) ? TRUE : FALSE);
343 }
344 
345 int color_content(short color, short *r, short *g, short *b)
346 {
347     T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
348     if (color < 0 || color > COLORS)
349 	returnCode(ERR);
350 
351     if (r) *r = SP->_color_table[color].red;
352     if (g) *g = SP->_color_table[color].green;
353     if (b) *b = SP->_color_table[color].blue;
354     returnCode(OK);
355 }
356 
357 int pair_content(short pair, short *f, short *b)
358 {
359 	T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
360 
361 	if ((pair < 0) || (pair > COLOR_PAIRS))
362 		returnCode(ERR);
363 	if (f) *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
364 	if (b) *b =  (SP->_color_pairs[pair] & C_MASK);
365 
366 	returnCode(OK);
367 }
368 
369 void _nc_do_color(int pair, bool reverse, int (*outc)(int))
370 {
371     short fg, bg;
372 
373     if (pair == 0)
374     {
375 	if (orig_pair)
376 	{
377 	    TPUTS_TRACE("orig_pair");
378 	    tputs(orig_pair, 1, outc);
379 	}
380 	else if (set_color_pair)
381 	{
382 	    TPUTS_TRACE("set_color_pair");
383 	    tputs(tparm(set_color_pair, pair), 1, outc);
384 	}
385 	else
386 	{
387 	    set_foreground_color(COLOR_WHITE, outc);
388 	    set_background_color(COLOR_BLACK, outc);
389 	}
390     }
391     else
392     {
393 	if (set_color_pair)
394 	{
395 	    TPUTS_TRACE("set_color_pair");
396 	    tputs(tparm(set_color_pair, pair), 1, outc);
397 	}
398 	else
399 	{
400 	    pair_content(pair, &fg, &bg);
401 	    if (reverse) {
402 		short xx = fg;
403 		fg = bg;
404 		bg = xx;
405 	    }
406 
407 	    T(("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg));
408 
409 	    if (fg == C_MASK || bg == C_MASK)
410 	    {
411 		if (set_original_colors() != TRUE)
412 		{
413 			if (fg == C_MASK)
414 				set_foreground_color(COLOR_WHITE, outc);
415 			if (bg == C_MASK)
416 				set_background_color(COLOR_BLACK, outc);
417 		}
418 	    }
419 	    if (fg != C_MASK)
420 	    {
421 		set_foreground_color(fg, outc);
422 	    }
423 	    if (bg != C_MASK)
424 	    {
425 		set_background_color(bg, outc);
426 	    }
427 	}
428     }
429 }
430