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