]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_color.3x
ncurses 6.0 - patch 20171104
[ncurses.git] / man / curs_color.3x
1 .\"***************************************************************************
2 .\" Copyright (c) 1998-2016,2017 Free Software Foundation, Inc.              *
3 .\"                                                                          *
4 .\" Permission is hereby granted, free of charge, to any person obtaining a  *
5 .\" copy of this software and associated documentation files (the            *
6 .\" "Software"), to deal in the Software without restriction, including      *
7 .\" without limitation the rights to use, copy, modify, merge, publish,      *
8 .\" distribute, distribute with modifications, sublicense, and/or sell       *
9 .\" copies of the Software, and to permit persons to whom the Software is    *
10 .\" furnished to do so, subject to the following conditions:                 *
11 .\"                                                                          *
12 .\" The above copyright notice and this permission notice shall be included  *
13 .\" in all copies or substantial portions of the Software.                   *
14 .\"                                                                          *
15 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 .\"                                                                          *
23 .\" Except as contained in this notice, the name(s) of the above copyright   *
24 .\" holders shall not be used in advertising or otherwise to promote the     *
25 .\" sale, use or other dealings in this Software without prior written       *
26 .\" authorization.                                                           *
27 .\"***************************************************************************
28 .\"
29 .\" $Id: curs_color.3x,v 1.51 2017/08/12 21:40:14 tom Exp $
30 .TH curs_color 3X ""
31 .ie \n(.g .ds `` \(lq
32 .el       .ds `` ``
33 .ie \n(.g .ds '' \(rq
34 .el       .ds '' ''
35 .de bP
36 .IP \(bu 4
37 ..
38 .ds n 5
39 .na
40 .hy 0
41 .SH NAME
42 \fBstart_color\fR,
43 \fBhas_colors\fR,
44 \fBcan_change_color\fR,
45 \fBinit_pair\fR,
46 \fBinit_color\fR,
47 \fBcolor_content\fR,
48 \fBpair_content\fR,
49 \fBreset_color_pairs\fR,
50 \fBCOLOR_PAIR\fR,
51 \fBPAIR_NUMBER\fR \- \fBcurses\fR color manipulation routines
52 .ad
53 .hy
54 .SH SYNOPSIS
55 \fB#include <curses.h>\fR
56 .sp
57 \fBint start_color(void);\fR
58 .sp
59 \fBbool has_colors(void);\fR
60 .br
61 \fBbool can_change_color(void);\fR
62 .sp
63 \fBint init_pair(short pair, short f, short b);\fR
64 .br
65 \fBint init_color(short color, short r, short g, short b);\fR
66 .br
67 /* extensions */
68 .br
69 \fBint init_extended_pair(int pair, int f, int b);\fR
70 .br
71 \fBint init_extended_color(int color, int r, int g, int b);\fR
72 .sp
73 \fBint color_content(short color, short *r, short *g, short *b);\fR
74 .br
75 \fBint pair_content(short pair, short *f, short *b);\fR
76 .br
77 /* extensions */
78 .br
79 \fBint extended_color_content(int color, int *r, int *g, int *b);\fR
80 .br
81 \fBint extended_pair_content(int pair, int *f, int *b);\fR
82 .sp
83 /* extensions */
84 .br
85 \fBvoid reset_color_pairs(void);\fR
86 .sp
87 \fBint COLOR_PAIR(int n);\fR
88 .br
89 \fBPAIR_NUMBER(\fR\fIattrs\fR\fB);\fP
90 .br
91 .SH DESCRIPTION
92 .SS Overview
93 \fBcurses\fR supports color attributes on terminals with that capability.
94 To use these routines \fBstart_color\fR must be called, usually right after
95 \fBinitscr\fR.
96 Colors are always used in pairs (referred to as color-pairs).
97 A color-pair consists of a foreground color (for characters) and a background
98 color (for the blank field on which the characters are displayed).
99 A programmer initializes a color-pair with the routine \fBinit_pair\fR.
100 After it has been initialized, \fBCOLOR_PAIR\fR(\fIn\fR)
101 can be used to convert the pair to a video attribute.
102 .PP
103 If a terminal is capable of redefining colors, the programmer can use the
104 routine \fBinit_color\fR to change the definition of a color.
105 The routines \fBhas_colors\fR and \fBcan_change_color\fR
106 return \fBTRUE\fR or \fBFALSE\fR,
107 depending on whether the terminal has color capabilities and whether the
108 programmer can change the colors.
109 The routine \fBcolor_content\fR allows a
110 programmer to extract the amounts of red, green, and blue components in an
111 initialized color.
112 The routine \fBpair_content\fR allows a programmer to find
113 out how a given color-pair is currently defined.
114 .SS Color Rendering
115 The \fBcurses\fP library combines these inputs to produce the
116 actual foreground and background colors shown on the screen:
117 .bP
118 per-character video attributes (e.g., via \fBwaddch\fP),
119 .bP
120 the window attribute (e.g., by \fBwattrset\fP), and
121 .bP
122 the background character (e.g., \fBwbkgdset\fP).
123 .PP
124 Per-character and window attributes are usually set by a parameter containing
125 video attributes including a color pair value.
126 Some functions such as \fBwattr_set\fP use a separate parameter which
127 is the color pair number.
128 .PP
129 The background character is a special case: it includes a character value,
130 just as if it were passed to \fBwaddch\fP.
131 .PP
132 The \fBcurses\fP library does the actual work of combining these color
133 pairs in an internal function called from \fBwaddch\fP:
134 .bP
135 If the parameter passed to \fBwaddch\fP is \fIblank\fP,
136 and it uses the special color pair 0,
137 .RS
138 .bP
139 \fBcurses\fP next checks the window attribute.
140 .bP
141 If the window attribute does not use color pair 0,
142 \fBcurses\fP uses the color pair from the window attribute.
143 .bP
144 Otherwise, \fBcurses\fP uses the background character.
145 .RE
146 .bP
147 If the parameter passed to \fBwaddch\fP is \fInot blank\fP,
148 or it does not use the special color pair 0,
149 \fBcurses\fP prefers the color pair from the parameter,
150 if it is nonzero.
151 Otherwise, it tries the window attribute next, and finally the
152 background character.
153 .PP
154 Some \fBcurses\fP functions such as \fBwprintw\fP call \fBwaddch\fP.
155 Those do not combine its parameter with a color pair.
156 Consequently those calls use only the window attribute or
157 the background character.
158 .SH CONSTANTS
159 .PP
160 In \fB<curses.h>\fR the following macros are defined.
161 These are the standard colors (ISO-6429).
162 \fBcurses\fR also assumes that \fBCOLOR_BLACK\fR is the default
163 background color for all terminals.
164 .PP
165 .nf
166       \fBCOLOR_BLACK\fR
167       \fBCOLOR_RED\fR
168       \fBCOLOR_GREEN\fR
169       \fBCOLOR_YELLOW\fR
170       \fBCOLOR_BLUE\fR
171       \fBCOLOR_MAGENTA\fR
172       \fBCOLOR_CYAN\fR
173       \fBCOLOR_WHITE\fR
174 .fi
175 .PP
176 Some terminals support more than the eight (8) \*(lqANSI\*(rq colors.
177 There are no standard names for those additional colors. 
178 .SH VARIABLES
179 .SS COLORS
180 is initialized by \fBstart_color\fP to the maximum number of colors
181 the terminal can support.
182 .SS COLOR_PAIRS
183 is initialized by \fBstart_color\fP to the maximum number of color pairs
184 the terminal can support.
185 .SH FUNCTIONS
186 .SS start_color
187 The \fBstart_color\fR routine requires no arguments.
188 It must be called if the programmer wants to use colors, and before any other
189 color manipulation routine is called.
190 It is good practice to call this routine right after \fBinitscr\fR.
191 \fBstart_color\fR does this:
192 .bP
193 It initializes two global variables, \fBCOLORS\fR and
194 \fBCOLOR_PAIRS\fR (respectively defining the maximum number of colors
195 and color-pairs the terminal can support).
196 .bP
197 It initializes the special color pair \fB0\fP to the default foreground
198 and background colors.
199 No other color pairs are initialized.
200 .bP
201 It restores the colors on the terminal to the values
202 they had when the terminal was just turned on.
203 .bP
204 If the terminal supports the \fBinitc\fP (\fBinitialize_color\fP) capability,
205 \fBstart_color\fP
206 initializes its internal table representing the
207 red, green and blue components of the color palette.
208 .IP
209 The components depend on whether the terminal uses
210 CGA (aka \*(lqANSI\*(rq) or
211 HLS (i.e., the \fBhls\fP (\fBhue_lightness_saturation\fP) capability is set).
212 The table is initialized first for eight basic colors
213 (black, red, green, yellow, blue, magenta, cyan, and white),
214 and after that (if the terminal supports more than eight colors)
215 the components are initialized to \fB1000\fP.
216 .IP
217 \fBstart_color\fP does not attempt to set the terminal's color palette
218 to match its built-in table.
219 An application may use \fBinit_color\fP to alter the internal table
220 along with the terminal's color.
221 .PP
222 These limits apply to color values and color pairs.
223 Values outside these limits are not legal, and may result in a runtime error:
224 .bP
225 \fBCOLORS\fP corresponds to the terminal database's \fBmax_colors\fR capability,
226 (see \fBterminfo\fR(\*n)).
227 .bP
228 color values are expected to be in the range \fB0\fP to \fBCOLORS\-1\fP,
229 inclusive (including \fB0\fP and \fBCOLORS\-1\fP).
230 .bP
231 a special color value \fB\-1\fP is used in certain extended functions
232 to denote the \fIdefault color\fP (see \fBuse_default_colors\fP).
233 .bP
234 \fBCOLOR_PAIRS\fP corresponds to the terminal database's \fBmax_pairs\fP capability,
235 (see \fBterminfo\fR(\*n)).
236 .bP
237 legal color pair values are in the range \fB1\fP to \fBCOLOR_PAIRS\-1\fP,
238 inclusive.
239 .bP
240 color pair \fB0\fP is special; it denotes \*(``no color\*(''.
241 .IP
242 Color pair \fB0\fP is assumed to be white on black,
243 but is actually whatever the terminal implements before color is initialized.
244 It cannot be modified by the application.
245 .SS has_colors
246 .PP
247 The \fBhas_colors\fR routine requires no arguments.
248 It returns \fBTRUE\fR if
249 the terminal can manipulate colors; otherwise, it returns \fBFALSE\fR.
250 This routine facilitates writing terminal-independent programs.
251 For example, a programmer can use it to decide
252 whether to use color or some other video attribute.
253 .SS can_change_color
254 .PP
255 The \fBcan_change_color\fR routine requires no arguments.
256 It returns \fBTRUE\fR if the terminal supports colors
257 and can change their definitions;
258 other, it returns \fBFALSE\fR.
259 This routine facilitates writing terminal-independent programs.
260 .SS init_pair
261 .PP
262 The \fBinit_pair\fR routine changes the definition of a color-pair.
263 It takes three arguments: the number of the color-pair to be changed, the foreground
264 color number, and the background color number.
265 For portable applications:
266 .bP
267 The first argument must be a legal color pair value.
268 If default colors are used (see \fBuse_default_colors\fP)
269 the upper limit is adjusted to allow for extra pairs which use
270 a default color in foreground and/or background.
271 .bP
272 The second and third arguments must be legal color values.
273 .PP
274 If the color-pair was previously initialized,
275 the screen is refreshed and all occurrences of that color-pair
276 are changed to the new definition.
277 .PP
278 As an extension, ncurses allows you to set color pair \fB0\fP via
279 the \fBassume_default_colors\fR(3X) routine, or to specify the use of
280 default colors (color number \fB\-1\fR) if you first invoke the
281 \fBuse_default_colors\fR(3X) routine.
282 .PP
283 The extension \fBreset_color_pairs\fP tells ncurses to discard all
284 of the color-pair information which was set with \fBinit_pair\fP.
285 It also touches the current- and standard-screens, allowing an application to
286 switch color palettes rapidly.
287 .SS init_color
288 .PP
289 The \fBinit_color\fR routine changes the definition of a color.
290 It takes four arguments: the number of the color to be changed followed by three RGB values
291 (for the amounts of red, green, and blue components).
292 .bP
293 The first argument must be a legal color value;
294 default colors are not allowed here.
295 (See the section \fBColors\fR for the default color index.)
296 .bP
297 Each of the last three arguments
298 must be a value in the range \fB0\fP through \fB1000\fP.
299 .PP
300 When \fBinit_color\fR is used, all
301 occurrences of that color on the screen immediately change to the new
302 definition.
303 .SS color_content
304 .PP
305 The \fBcolor_content\fR routine gives programmers a way to find the intensity
306 of the red, green, and blue (RGB) components in a color.
307 It requires four arguments: the color number, and three addresses
308 of \fBshort\fRs for storing
309 the information about the amounts of red, green, and blue components in the
310 given color.
311 .bP
312 The first argument must be a legal color value, i.e.,
313 \fB0\fP through \fBCOLORS\-1\fP, inclusive.
314 .bP
315 The values that are stored at the addresses pointed to by the
316 last three arguments are in the range
317 \fB0\fP (no component) through \fB1000\fP (maximum amount of component), inclusive.
318 .SS pair_content
319 .PP
320 The \fBpair_content\fR routine allows programmers to find out what colors a
321 given color-pair consists of.
322 It requires three arguments: the color-pair
323 number, and two addresses of \fBshort\fRs for storing the foreground and the
324 background color numbers.
325 .bP
326 The first argument must be a legal color value,
327 i.e., in the range \fB1\fP through \fBCOLOR_PAIRS\-1\fR, inclusive.
328 .bP
329 The values that are stored at the addresses pointed
330 to by the second and third arguments are in the
331 range \fB0\fP through \fBCOLORS\fR, inclusive.
332 .SS PAIR_NUMBER
333 .PP
334 \fBPAIR_NUMBER(\fR\fIattrs\fR) extracts the color
335 value from its \fIattrs\fP parameter and returns it as a color pair number.
336 .SS COLOR_PAIR
337 Its inverse \fBCOLOR_PAIR(\fR\fIn\fR\fB)\fR converts a color pair number
338 to an attribute.
339 Attributes can hold color pairs in the range 0 to 255.
340 If you need a color pair larger than that, you must use functions
341 such as \fBattr_set\fP (which pass the color pair as a separate parameter)
342 rather than the legacy functions such as \fBattrset\fP.
343 .SH RETURN VALUE
344 The routines \fBcan_change_color\fR and \fBhas_colors\fR return \fBTRUE\fR
345 or \fBFALSE\fR.
346 .PP
347 All other routines return the integer \fBERR\fR upon failure and an \fBOK\fR
348 (SVr4 specifies only \*(lqan integer value other than \fBERR\fR\*(rq) upon successful
349 completion.
350 .PP
351 X/Open defines no error conditions.
352 This implementation will return \fBERR\fR on attempts to
353 use color values outside the range \fB0\fP to \fBCOLORS\fP\-1
354 (except for the default colors extension),
355 or use color pairs outside the range \fB0\fP to \fBCOLOR_PAIRS\-1\fP.
356 Color values used in \fBinit_color\fP must be in the range \fB0\fP to \fB1000\fP.
357 An error is returned from all functions
358 if the terminal has not been initialized.
359 An error is returned from secondary functions such as \fBinit_pair\fP
360 if \fBstart_color\fP was not called.
361 .RS 3
362 .TP 5
363 \fBinit_color\fP
364 returns an error if the terminal does not support
365 this feature, e.g., if the \fBinitialize_color\fP capability is absent
366 from the terminal description.
367 .TP 5
368 \fBstart_color\fP
369 returns an error if the color table cannot be allocated.
370 .RE
371 .SH NOTES
372 In the \fBncurses\fR implementation, there is a separate color activation flag,
373 color palette, color pairs table, and associated \fBCOLORS\fP and \fBCOLOR_PAIRS\fP counts
374 for each screen; the \fBstart_color\fR function only affects the current
375 screen.
376 The SVr4/XSI interface is not really designed with this in mind, and
377 historical implementations may use a single shared color palette.
378 .PP
379 Setting an implicit background color via a color pair affects only
380 character cells that a character write operation explicitly touches.
381 To change
382 the background color used when parts of a window are blanked by erasing or
383 scrolling operations, see \fBcurs_bkgd\fR(3X).
384 .PP
385 Several caveats apply on older x86 machines (e.g., i386, i486) with VGA-compatible graphics:
386 .bP
387 COLOR_YELLOW is actually brown.
388 To get yellow, use COLOR_YELLOW combined with the \fBA_BOLD\fR attribute.
389 .bP
390 The A_BLINK attribute should in theory cause the background to go bright.
391 This often fails to work, and even some cards for which it mostly works
392 (such as the
393 Paradise and compatibles) do the wrong thing when you try to set a bright
394 \*(lqyellow\*(rq background (you get a blinking yellow foreground instead).
395 .bP
396 Color RGB values are not settable.
397 .SH PORTABILITY
398 This implementation satisfies XSI Curses's minimum maximums
399 for \fBCOLORS\fR and \fBCOLOR_PAIRS\fR.
400 .PP
401 The \fBinit_pair\fP routine accepts negative values of foreground
402 and background color to support the \fBuse_default_colors\fR(3X) extension,
403 but only if that routine has been first invoked.
404 .PP
405 The assumption that \fBCOLOR_BLACK\fR is the default
406 background color for all terminals can be modified using the
407 \fBassume_default_colors\fR(3X) extension.
408 .PP
409 This implementation checks the pointers,
410 e.g., for the values returned by
411 \fBcolor_content\fP and \fBpair_content\fP,
412 and will treat those as optional parameters when null.
413 .PP
414 X/Open Curses does not specify a limit for the number of colors and
415 color pairs which a terminal can support.
416 However, in its use of \fBshort\fP for the parameters,
417 it carries over SVr4's implementation detail for the compiled
418 terminfo database, which uses signed 16-bit numbers.
419 This implementation provides extended versions of those functions
420 which use \fBshort\fP parameters,
421 allowing applications to use larger color- and pair-numbers.
422 .PP
423 The \fBreset_color_pairs\fP function is an extension of ncurses.
424 .SH SEE ALSO
425 \fBcurses\fR(3X),
426 \fBcurs_initscr\fR(3X),
427 \fBcurs_attr\fR(3X),
428 \fBcurs_variables\fR(3X),
429 \fBdefault_colors\fR(3X)