]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_color.c
ncurses 4.1
[ncurses.git] / ncurses / lib_color.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22 /* lib_color.c
23  *
24  * Handles color emulation of SYS V curses
25  *
26  */
27
28 #include <curses.priv.h>
29
30 #include <term.h>
31
32 MODULE_ID("$Id: lib_color.c,v 1.17 1997/05/03 19:16:05 tom Exp $")
33
34 /*
35  * These should be screen structure members.  They need to be globals for
36  * hystorical reasons.  So we assign them in start_color() and also in
37  * set_term()'s screen-switching logic.
38  */
39 int COLOR_PAIRS;
40 int COLORS;
41
42 static const color_t cga_palette[] =
43 {
44     /*  R       G       B */
45         {0,     0,      0},     /* COLOR_BLACK */
46         {1000,  0,      0},     /* COLOR_RED */
47         {0,     1000,   0},     /* COLOR_GREEN */
48         {1000,  1000,   0},     /* COLOR_YELLOW */
49         {0,     0,      1000},  /* COLOR_BLUE */
50         {1000,  0,      1000},  /* COLOR_MAGENTA */
51         {0,     1000,   1000},  /* COLOR_CYAN */
52         {1000,  1000,   1000},  /* COLOR_WHITE */
53 };
54 static const color_t hls_palette[] =
55 {
56     /*  H       L       S */
57         {0,     0,      0},     /* COLOR_BLACK */
58         {120,   50,     100},   /* COLOR_RED */
59         {240,   50,     100},   /* COLOR_GREEN */
60         {180,   50,     100},   /* COLOR_YELLOW */
61         {330,   50,     100},   /* COLOR_BLUE */
62         {60,    50,     100},   /* COLOR_MAGENTA */
63         {300,   50,     100},   /* COLOR_CYAN */
64         {0,     50,     100},   /* COLOR_WHITE */
65 };
66
67 int start_color(void)
68 {
69         T((T_CALLED("start_color()")));
70
71 #ifdef orig_pair
72         if (orig_pair != NULL)
73         {
74                 TPUTS_TRACE("orig_pair");
75                 putp(orig_pair);
76         }
77 #endif /* orig_pair */
78 #ifdef orig_colors
79         if (orig_colors != NULL)
80         {
81                 TPUTS_TRACE("orig_colors");
82                 putp(orig_colors);
83         }
84 #endif /* orig_colors */
85 #if defined(orig_pair) && defined(orig_colors)
86         if (!orig_pair && !orig_colors)
87                 returnCode(ERR);
88 #endif /* defined(orig_pair) && defined(orig_colors) */
89         if (max_pairs != -1)
90                 COLOR_PAIRS = SP->_pair_count = max_pairs;
91         else
92                 returnCode(ERR);
93         SP->_color_pairs = typeCalloc(unsigned short, max_pairs);
94         SP->_color_pairs[0] = PAIR_OF(COLOR_WHITE, COLOR_BLACK);
95         if (max_colors != -1)
96                 COLORS = SP->_color_count = max_colors;
97         else
98                 returnCode(ERR);
99         SP->_coloron = 1;
100
101         SP->_color_table = malloc(sizeof(color_t) * COLORS);
102 #ifdef hue_lightness_saturation
103         if (hue_lightness_saturation)
104             memcpy(SP->_color_table, hls_palette, sizeof(color_t) * COLORS);
105         else
106 #endif /* hue_lightness_saturation */
107             memcpy(SP->_color_table, cga_palette, sizeof(color_t) * COLORS);
108
109         if (orig_colors)
110         {
111             TPUTS_TRACE("orig_colors");
112             putp(orig_colors);
113         }
114
115         T(("started color: COLORS = %d, COLOR_PAIRS = %d", COLORS, COLOR_PAIRS));
116
117         returnCode(OK);
118 }
119
120 #ifdef hue_lightness_saturation
121 static void rgb2hls(short r, short g, short b, short *h, short *l, short *s)
122 /* convert RGB to HLS system */
123 {
124     short min, max, t;
125
126     if ((min = g < r ? g : r) > b) min = b;
127     if ((max = g > r ? g : r) < b) max = b;
128
129     /* calculate lightness */
130     *l = (min + max) / 20;
131
132     if (min == max)             /* black, white and all shades of gray */
133     {
134         *h = 0;
135         *s = 0;
136         return;
137     }
138
139     /* calculate saturation */
140     if (*l < 50)
141         *s = ((max - min) * 100) / (max + min);
142     else *s = ((max - min) * 100) / (2000 - max - min);
143
144     /* calculate hue */
145     if (r == max)
146         t = 120 + ((g - b) * 60) / (max - min);
147     else
148         if (g == max)
149             t = 240 + ((b - r) * 60) / (max - min);
150         else
151             t = 360 + ((r - g) * 60) / (max - min);
152
153     *h = t % 360;
154 }
155 #endif /* hue_lightness_saturation */
156
157 /*
158  * Extension (1997/1/18) - Allow negative f/b values to set default color
159  * values.
160  */
161 int init_pair(short pair, short f, short b)
162 {
163         T((T_CALLED("init_pair(%d,%d,%d)"), pair, f, b));
164
165         if ((pair < 1) || (pair >= COLOR_PAIRS))
166                 returnCode(ERR);
167         if (SP->_default_color)
168         {
169                 if (f < 0)
170                         f = C_MASK;
171                 if (b < 0)
172                         b = C_MASK;
173                 if (f >= COLORS && f != C_MASK)
174                         returnCode(ERR);
175                 if (b >= COLORS && b != C_MASK)
176                         returnCode(ERR);
177         }
178         else
179         if ((f < 0) || (f >= COLORS)
180          || (b < 0) || (b >= COLORS))
181                 returnCode(ERR);
182
183         /*
184          * FIXME: when a pair's content is changed, replace its colors
185          * (if pair was initialized before a screen update is performed
186          * replacing original pair colors with the new ones)
187          */
188
189         SP->_color_pairs[pair] = PAIR_OF(f,b);
190
191         if (initialize_pair)
192         {
193             const color_t       *tp = hue_lightness_saturation ? hls_palette : cga_palette;
194
195             T(("initializing pair: pair = %d, fg=(%d,%d,%d), bg=(%d,%d,%d)",
196                pair,
197                tp[f].red, tp[f].green, tp[f].blue,
198                tp[b].red, tp[b].green, tp[b].blue));
199
200             if (initialize_pair)
201             {
202                 TPUTS_TRACE("initialize_pair");
203                 putp(tparm(initialize_pair,
204                             pair,
205                             tp[f].red, tp[f].green, tp[f].blue,
206                             tp[b].red, tp[b].green, tp[b].blue));
207             }
208         }
209
210         returnCode(OK);
211 }
212
213 int init_color(short color, short r, short g, short b)
214 {
215         T((T_CALLED("init_color(%d,%d,%d,%d)"), color, r, g, b));
216 #ifdef initialize_color
217         if (initialize_color == NULL)
218                 returnCode(ERR);
219 #endif /* initialize_color */
220
221         if (color < 0 || color >= COLORS)
222                 returnCode(ERR);
223 #ifdef hue_lightness_saturation
224         if (hue_lightness_saturation == TRUE)
225                 if (r < 0 || r > 360 || g < 0 || g > 100 || b < 0 || b > 100)
226                         returnCode(ERR);
227         if (hue_lightness_saturation == FALSE)
228 #endif /* hue_lightness_saturation */
229                 if (r < 0 || r > 1000 || g < 0 ||  g > 1000 || b < 0 || b > 1000)
230                         returnCode(ERR);
231
232 #ifdef hue_lightness_saturation
233         if (hue_lightness_saturation)
234             rgb2hls(r, g, b,
235                       &SP->_color_table[color].red,
236                       &SP->_color_table[color].green,
237                       &SP->_color_table[color].blue);
238         else
239 #endif /* hue_lightness_saturation */
240         {
241                 SP->_color_table[color].red = r;
242                 SP->_color_table[color].green = g;
243                 SP->_color_table[color].blue = b;
244         }
245
246 #ifdef initialize_color
247         if (initialize_color)
248         {
249                 TPUTS_TRACE("initialize_color");
250                 putp(tparm(initialize_color, color, r, g, b));
251         }
252 #endif /* initialize_color */
253         returnCode(OK);
254 }
255
256 bool can_change_color(void)
257 {
258         T((T_CALLED("can_change_color()")));
259         returnCode(can_change != 0);
260 }
261
262 bool has_colors(void)
263 {
264         T((T_CALLED("has_colors()")));
265         returnCode((orig_pair != NULL || orig_colors != NULL)
266                 && (max_colors != -1) && (max_pairs != -1)
267                 &&
268                 (((set_foreground != NULL) && (set_background != NULL))
269                 || ((set_a_foreground != NULL) && (set_a_background != NULL))
270                 || set_color_pair)
271                 );
272 }
273
274 int color_content(short color, short *r, short *g, short *b)
275 {
276     T((T_CALLED("color_content(%d,%p,%p,%p)"), color, r, g, b));
277     if (color < 0 || color > COLORS)
278         returnCode(ERR);
279
280     *r = SP->_color_table[color].red;
281     *g = SP->_color_table[color].green;
282     *b = SP->_color_table[color].blue;
283     returnCode(OK);
284 }
285
286 int pair_content(short pair, short *f, short *b)
287 {
288         T((T_CALLED("pair_content(%d,%p,%p)"), pair, f, b));
289
290         if ((pair < 0) || (pair > COLOR_PAIRS))
291                 returnCode(ERR);
292         *f = ((SP->_color_pairs[pair] >> C_SHIFT) & C_MASK);
293         *b =  (SP->_color_pairs[pair] & C_MASK);
294
295         returnCode(OK);
296 }
297
298 /*
299  * SVr4 curses is known to interchange color codes (1,4) and (3,6), possibly
300  * to maintain compatibility with a pre-ANSI scheme.  The same scheme is
301  * also used in the FreeBSD syscons.
302  */
303 static int toggled_colors(int c)
304 {
305     if (c < 16) {
306         static const int table[] =
307                 { 0,  4,  2,  6,  1,  5,  3,  7,
308                   8, 12, 10, 14,  9, 13, 11, 15};
309         c = table[c];
310     }
311     return c;
312 }
313
314 void _nc_do_color(int pair, int  (*outc)(int))
315 {
316     short fg, bg;
317
318     if (pair == 0)
319     {
320         if (orig_pair)
321         {
322             TPUTS_TRACE("orig_pair");
323             tputs(orig_pair, 1, outc);
324         }
325     }
326     else
327     {
328         if (set_color_pair)
329         {
330             TPUTS_TRACE("set_color_pair");
331             tputs(tparm(set_color_pair, pair), 1, outc);
332         }
333         else
334         {
335             pair_content(pair, &fg, &bg);
336
337             T(("setting colors: pair = %d, fg = %d, bg = %d", pair, fg, bg));
338
339             if (fg == C_MASK || bg == C_MASK)
340             {
341                 if (orig_pair)
342                 {
343                     TPUTS_TRACE("orig_pair");
344                     tputs(orig_pair, 1, outc);
345                 }
346                 else
347                 {
348                     TPUTS_TRACE("orig_colors");
349                     tputs(orig_colors, 1, outc);
350                 }
351             }
352             if (fg != C_MASK)
353             {
354                 if (set_a_foreground)
355                 {
356                     TPUTS_TRACE("set_a_foreground");
357                     tputs(tparm(set_a_foreground, fg), 1, outc);
358                 }
359                 else
360                 {
361                     TPUTS_TRACE("set_foreground");
362                     tputs(tparm(set_foreground, toggled_colors(fg)), 1, outc);
363                 }
364             }
365             if (bg != C_MASK)
366             {
367                 if (set_a_background)
368                 {
369                     TPUTS_TRACE("set_a_background");
370                     tputs(tparm(set_a_background, bg), 1, outc);
371                 }
372                 else
373                 {
374                     TPUTS_TRACE("set_background");
375                     tputs(tparm(set_background, toggled_colors(bg)), 1, outc);
376                 }
377             }
378         }
379     }
380 }