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