xref: /illumos-gate/usr/src/lib/libxcurses2/src/libc/xcurses/color.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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-1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /* LINTLIBRARY */
30 
31 /*
32  * color.c
33  *
34  * XCurses Library
35  *
36  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
37  *
38  */
39 
40 #ifdef M_RCSID
41 #ifndef lint
42 static char rcsID[] =
43 "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
44 "libxcurses/src/libc/xcurses/rcs/color.c 1.6 1998/05/28 17:10:14 "
45 "cbates Exp $";
46 #endif
47 #endif
48 
49 #include <private.h>
50 #include <stdlib.h>
51 
52 int
53 start_color(void)
54 {
55 	COLORS = max_colors;
56 	COLOR_PAIRS = max_pairs;
57 
58 	if (orig_colors != (char *) 0)
59 		(void) TPUTS(orig_colors, 1, __m_outc);
60 
61 	if (orig_pair != (char *) 0)
62 		(void) TPUTS(orig_pair, 1, __m_outc);
63 
64 	if (0 < max_colors) {
65 		cur_term->_color = calloc(max_colors,
66 			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 	(void) init_color(COLOR_BLACK,		0,	0,	0);
78 	(void) init_color(COLOR_RED,		1000,	0,	0);
79 	(void) init_color(COLOR_GREEN,		0,	1000,	0);
80 	(void) init_color(COLOR_BLUE,		0,	0,	1000);
81 	(void) init_color(COLOR_YELLOW,	1000,	1000,	0);
82 	(void) init_color(COLOR_MAGENTA,	1000,	0,	1000);
83 	(void) init_color(COLOR_CYAN,		0,	1000,	1000);
84 	(void) init_color(COLOR_WHITE,		1000,	1000,	1000);
85 
86 	return (OK);
87 error2:
88 	if (cur_term->_color != (short (*)[3]) 0)
89 		free(cur_term->_color);
90 error1:
91 	return (ERR);
92 }
93 
94 int
95 init_color(short color, short r, short g, short b)
96 {
97 	int code = ERR;
98 
99 	if (!can_change || color < 0 || max_colors <= color ||
100 		r < 0 || 1000 < r ||
101 		g < 0 || 1000 < g ||
102 		b < 0 || 1000 < b)
103 		goto error;
104 
105 	/* Remember color settings for future queries. */
106 	cur_term->_color[color][0] = r;
107 	cur_term->_color[color][1] = g;
108 	cur_term->_color[color][2] = b;
109 
110 	code = OK;
111 
112 	/* Set the color. */
113 	if (initialize_color != (char *) 0) {
114 		code = tputs(tparm(initialize_color, (long) color,
115 			(long) r, (long) g, (long) b, 0L, 0L, 0L, 0L, 0L),
116 			1, __m_outc);
117 	}
118 error:
119 	return (code);
120 }
121 
122 int
123 init_pair(short pair, short f, short b)
124 {
125 	int code = ERR;
126 
127 	if (pair < 0 || max_pairs <= pair ||
128 		f < 0 || max_colors <= f ||
129 		b < 0 || max_colors <= b)
130 		goto error;
131 
132 	/* Remember color-pair settings for future queries. */
133 	cur_term->_pair[pair][0] = f;
134 	cur_term->_pair[pair][1] = b;
135 
136 	code = OK;
137 
138 	/* Set color-pair (foreground-background). */
139 	if (initialize_pair != (char *) 0) {
140 		code = tputs(tparm(initialize_pair,
141 			(long) cur_term->_color[f][0],
142 			(long) cur_term->_color[f][1],
143 			(long) cur_term->_color[f][2],
144 			(long) cur_term->_color[b][0],
145 			(long) cur_term->_color[b][1],
146 			(long) cur_term->_color[b][2],
147 			0L, 0L, 0L), 1, __m_outc);
148 	}
149 error:
150 	return (code);
151 }
152 
153 int
154 color_content(short color, short *r, short *g, short *b)
155 {
156 	if (color < 0 || max_colors <= color)
157 		return (ERR);
158 
159 	/*
160 	 * There does not appear to be a terminfo entry to query the
161 	 * color settings, so we retain them in an array for quick
162 	 * access.
163 	 */
164 	*r = cur_term->_color[color][0];
165 	*g = cur_term->_color[color][1];
166 	*b = cur_term->_color[color][2];
167 
168 	return (OK);
169 }
170 
171 int
172 pair_content(short pair, short *f, short *b)
173 {
174 	if (pair < 0 || max_pairs <= pair)
175 		return (ERR);
176 
177 	/*
178 	 * There does not appear to be a terminfo entry to query the
179 	 * color-pair settings, so we retain them in an array for quick
180 	 * access.
181 	 */
182 	*f = cur_term->_pair[pair][0];
183 	*b = cur_term->_pair[pair][1];
184 
185 	return (OK);
186 }
187