xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/color.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1995, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * color.c
31  *
32  * XCurses Library
33  *
34  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  */
37 
38 #ifdef M_RCSID
39 #ifndef lint
40 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/color.c 1.2 1995/10/02 15:15:02 ant Exp $";
41 #endif
42 #endif
43 
44 #include <private.h>
45 #include <stdlib.h>
46 
47 int
48 start_color()
49 {
50 	int code = ERR;
51 
52 #ifdef M_CURSES_TRACE
53 	__m_trace("start_color(void)");
54 #endif
55 
56 	COLORS = max_colors;
57 	COLOR_PAIRS = max_pairs;
58 
59 	if (orig_colors != (char *) 0)
60 		(void) tputs(orig_colors, 1, __m_outc);
61 
62 	if (orig_pair != (char *) 0)
63 		(void) tputs(orig_pair, 1, __m_outc);
64 
65 	if (0 < max_colors) {
66 		cur_term->_color = calloc(max_colors, sizeof *cur_term->_color);
67 		if (cur_term->_color == (short (*)[3]) 0)
68 			goto error1;
69 	}
70 
71 	if (0 < max_pairs) {
72 		cur_term->_pair = calloc(max_pairs, sizeof *cur_term->_pair);
73 		if (cur_term->_pair == (short (*)[2]) 0)
74 			goto error2;
75 	}
76 
77 	return __m_return_code("start_color", OK);
78 error2:
79 	if (cur_term->_color != (short (*)[3]) 0)
80 		free(cur_term->_color);
81 error1:
82 	return __m_return_code("start_color", ERR);
83 }
84 
85 int
86 init_color(short color, short r, short g, short b)
87 {
88 	int code = ERR;
89 
90 #ifdef M_CURSES_TRACE
91 	__m_trace("init_color(%d, %d, %d, %d)", color, r, g, b);
92 #endif
93 
94 	if (!can_change || color < 0 || max_colors <= color
95 	|| r < 0 || 1000 < r
96 	|| g < 0 || 1000 < g
97 	|| b < 0 || 1000 < b)
98 		goto error;
99 
100 	/* Remember color settings for future queries. */
101 	cur_term->_color[color][0] = r;
102 	cur_term->_color[color][1] = g;
103 	cur_term->_color[color][2] = b;
104 
105 	code = OK;
106 
107 	/* Set the color. */
108 	if (initialize_color != (char *) 0) {
109 		code = tputs(
110 			tparm(
111 				initialize_color, (long) color,
112 				(long) r, (long) g, (long) b,
113 				0L, 0L, 0L, 0L, 0L
114 			), 1, __m_outc
115 		);
116 	}
117 error:
118 	return __m_return_code("init_color", code);
119 }
120 
121 int
122 init_pair(short pair, short f, short b)
123 {
124 	int code = ERR;
125 
126 #ifdef M_CURSES_TRACE
127 	__m_trace("init_pair(%d, %d, %d)", pair, f, b);
128 #endif
129 
130 	if (pair < 0 || max_pairs <= pair
131 	|| f < 0 || max_colors <= f
132 	|| b < 0 || max_colors <= b)
133 		goto error;
134 
135 	/* Remember color-pair settings for future queries. */
136 	cur_term->_pair[pair][0] = f;
137 	cur_term->_pair[pair][1] = b;
138 
139 	code = OK;
140 
141 	/* Set color-pair (foreground-background). */
142 	if (initialize_pair == (char *) 0) {
143 		code = tputs(
144 			tparm(
145 				initialize_pair,
146 				(long) cur_term->_pair[f][0],
147 				(long) cur_term->_pair[f][1],
148 				(long) cur_term->_pair[f][2],
149 				(long) cur_term->_pair[b][0],
150 				(long) cur_term->_pair[b][1],
151 				(long) cur_term->_pair[b][2],
152 				0L, 0L, 0L
153 			), 1, __m_outc
154 		);
155 	}
156 error:
157 	return __m_return_code("init_pair", code);
158 }
159 
160 int
161 color_content(short color, short *r, short *g, short *b)
162 {
163 #ifdef M_CURSES_TRACE
164 	__m_trace("color_content(%d, %p, %p, %p)", color, r, g, b);
165 #endif
166 
167 	if (color < 0 || max_colors <= color)
168 		return __m_return_code("color_content", ERR);
169 
170 	/* There does not appear to be a terminfo entry to query the
171 	 * color settings, so we retain them in an array for quick
172 	 * access.
173 	 */
174 	*r = cur_term->_color[color][0];
175 	*g = cur_term->_color[color][1];
176 	*b = cur_term->_color[color][2];
177 
178 	return __m_return_code("color_content", OK);
179 }
180 
181 int
182 pair_content(short pair, short *f, short *b)
183 {
184 #ifdef M_CURSES_TRACE
185 	__m_trace("pair_content(%d, %p, %p)", pair, f, b);
186 #endif
187 	if (pair < 0 || max_pairs <= pair)
188 		return __m_return_code("pair_content", ERR);
189 
190 	/* There does not appear to be a terminfo entry to query the
191 	 * color-pair settings, so we retain them in an array for quick
192 	 * access.
193 	 */
194 	*f = cur_term->_pair[pair][0];
195 	*b = cur_term->_pair[pair][1];
196 
197 	return __m_return_code("pair_content", OK);
198 }
199 
200