1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include "curses_inc.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate static	void	_rgb_to_hls(float, float, float, int *, int *, int *);
47*7c478bd9Sstevel@tonic-gate static float	MAX(float, float, float), MIN(float, float, float);
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int
init_color(short color,short r,short g,short b)51*7c478bd9Sstevel@tonic-gate init_color(short color, short r, short g, short b)
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	_Color *ctp = cur_term->_color_tbl;  /* color table pointer */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	/* check if terminal can change color and validity of the	    */
56*7c478bd9Sstevel@tonic-gate 	/* first argument						    */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if (!can_change || color >= COLORS || color < 0)
59*7c478bd9Sstevel@tonic-gate 		return (ERR);
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate 	/* if any of the last 3 arguments is out of 0 - 1000 range,	*/
62*7c478bd9Sstevel@tonic-gate 	/* adjust them accordingly					*/
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	if (r > 1000)	r = 1000;
65*7c478bd9Sstevel@tonic-gate 	if (g > 1000)	g = 1000;
66*7c478bd9Sstevel@tonic-gate 	if (b > 1000)	b = 1000;
67*7c478bd9Sstevel@tonic-gate 	if (r < 0)	r = 0;
68*7c478bd9Sstevel@tonic-gate 	if (g < 0)	g = 0;
69*7c478bd9Sstevel@tonic-gate 	if (b < 0)	b = 0;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/* if the call came from scr_reset, the color_table already	*/
72*7c478bd9Sstevel@tonic-gate 	/* contains desired values, but we should still send escape seq. */
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	/* if new color is exactly the same as the old one, return */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if (ctp[color].r == r && ctp[color].g == g && ctp[color].b == b)
77*7c478bd9Sstevel@tonic-gate 		return (OK);
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	/* update color table		*/
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	ctp[color].r = r;   ctp[color].g = g;    ctp[color].b = b;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	/* all the occurrences of color on the screen must be changed   */
84*7c478bd9Sstevel@tonic-gate 	/* to the new definition					*/
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* for terminals that can define individual colors (Tek model)  */
87*7c478bd9Sstevel@tonic-gate 	/* send an escape sequence to define that color			*/
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	if (initialize_color) {
90*7c478bd9Sstevel@tonic-gate 		if (hue_lightness_saturation) {
91*7c478bd9Sstevel@tonic-gate 			int	h, s, l;
92*7c478bd9Sstevel@tonic-gate 			_rgb_to_hls((float)r, (float)g, (float)b, &h, &l, &s);
93*7c478bd9Sstevel@tonic-gate 			(void) tputs(tparm_p4(initialize_color, color, h, l, s),
94*7c478bd9Sstevel@tonic-gate 			    1, _outch);
95*7c478bd9Sstevel@tonic-gate 		} else
96*7c478bd9Sstevel@tonic-gate 			(void) tputs(tparm_p4(initialize_color, color, r, g, b),
97*7c478bd9Sstevel@tonic-gate 			    1, _outch);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	/* for terminals that can only define color pairs, go through   */
103*7c478bd9Sstevel@tonic-gate 	/* pairs table, and re-initialize all pairs that use given color */
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	else {
106*7c478bd9Sstevel@tonic-gate 		short i;
107*7c478bd9Sstevel@tonic-gate 		_Color_pair *ptp = cur_term->_pairs_tbl;
108*7c478bd9Sstevel@tonic-gate 		/* pairs table pointer */
109*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < COLOR_PAIRS; i++) {
110*7c478bd9Sstevel@tonic-gate 			if (ptp[i].foreground == color ||
111*7c478bd9Sstevel@tonic-gate 			    ptp[i].background == color)
112*7c478bd9Sstevel@tonic-gate 				_init_HP_pair(i, ptp[i].foreground,
113*7c478bd9Sstevel@tonic-gate 				    ptp[i].background);
114*7c478bd9Sstevel@tonic-gate 		}
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	return (OK);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate static void
_rgb_to_hls(float r,float g,float b,int * hh,int * ll,int * ss)123*7c478bd9Sstevel@tonic-gate _rgb_to_hls(float r, float g, float b, int *hh, int *ll, int *ss)
124*7c478bd9Sstevel@tonic-gate {
125*7c478bd9Sstevel@tonic-gate 	float	rc, gc, bc, h, l, s;
126*7c478bd9Sstevel@tonic-gate 	double	max, min;
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	r /= 1000;  g /= 1000;  b /= 1000;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	max = MAX(r, g, b);
131*7c478bd9Sstevel@tonic-gate 	min = MIN(r, g, b);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	/* calculate lightness  */
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	l = (max + min) / 2;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	/* calculate saturation */
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	if (max == min) {
140*7c478bd9Sstevel@tonic-gate 		s = 0;
141*7c478bd9Sstevel@tonic-gate 		h = 0;
142*7c478bd9Sstevel@tonic-gate 	} else {
143*7c478bd9Sstevel@tonic-gate 		if (l < 0.5)
144*7c478bd9Sstevel@tonic-gate 			s = (max - min) / (max + min);
145*7c478bd9Sstevel@tonic-gate 		else
146*7c478bd9Sstevel@tonic-gate 			s = (max - min) / (2 - max - min);
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 		/* calculate hue   */
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		rc = (max - r) / (max - min);
151*7c478bd9Sstevel@tonic-gate 		gc = (max - g) / (max - min);
152*7c478bd9Sstevel@tonic-gate 		bc = (max - b) / (max - min);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 		if (r == max)
155*7c478bd9Sstevel@tonic-gate 			h = bc - gc;
156*7c478bd9Sstevel@tonic-gate 		else if (g == max)
157*7c478bd9Sstevel@tonic-gate 			h = 2 + rc - bc;
158*7c478bd9Sstevel@tonic-gate 		else /* if (b == max) */
159*7c478bd9Sstevel@tonic-gate 			h = 4 + gc - rc;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		h = h * 60;
162*7c478bd9Sstevel@tonic-gate 		if (h < 0.0)
163*7c478bd9Sstevel@tonic-gate 			h = h + 360;
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		/* until here we have converted into HSL.  */
166*7c478bd9Sstevel@tonic-gate 		/* Now, to convert into */
167*7c478bd9Sstevel@tonic-gate 		/* Tektronix HLS, add 120 to h		*/
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		h = ((int)(h+120))%360;
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 	*hh = (int) h;
172*7c478bd9Sstevel@tonic-gate 	*ss = (int) (s * 100);
173*7c478bd9Sstevel@tonic-gate 	*ll = (int) (l * 100);
174*7c478bd9Sstevel@tonic-gate }
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate static float
MAX(float a,float b,float c)178*7c478bd9Sstevel@tonic-gate MAX(float a, float b, float c)
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate 	if (a >= b)
181*7c478bd9Sstevel@tonic-gate 		if (a >= c)
182*7c478bd9Sstevel@tonic-gate 			return (a);
183*7c478bd9Sstevel@tonic-gate 		else return (c);
184*7c478bd9Sstevel@tonic-gate 	else if (c >= b)
185*7c478bd9Sstevel@tonic-gate 		return (c);
186*7c478bd9Sstevel@tonic-gate 	else return (b);
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate static float
MIN(float a,float b,float c)190*7c478bd9Sstevel@tonic-gate MIN(float a, float b, float c)
191*7c478bd9Sstevel@tonic-gate {
192*7c478bd9Sstevel@tonic-gate 	if (a > b)
193*7c478bd9Sstevel@tonic-gate 		if (b > c)
194*7c478bd9Sstevel@tonic-gate 			return (c);
195*7c478bd9Sstevel@tonic-gate 		else return (b);
196*7c478bd9Sstevel@tonic-gate 	else if (a < c)
197*7c478bd9Sstevel@tonic-gate 		return (a);
198*7c478bd9Sstevel@tonic-gate 	else return (c);
199*7c478bd9Sstevel@tonic-gate }
200