]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_color.3x
ncurses 6.1 - patch 20180728
[ncurses.git] / man / curs_color.3x
1 .\"***************************************************************************
2 .\" Copyright (c) 1998-2017,2018 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.55 2018/07/28 22:15:59 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
236 the terminal database's \fBmax_pairs\fP capability,
237 (see \fBterminfo\fR(\*n)).
238 .bP
239 legal color pair values are in the range \fB1\fP to \fBCOLOR_PAIRS\-1\fP,
240 inclusive.
241 .bP
242 color pair \fB0\fP is special; it denotes \*(``no color\*(''.
243 .IP
244 Color pair \fB0\fP is assumed to be white on black,
245 but is actually whatever the terminal implements before color is initialized.
246 It cannot be modified by the application.
247 .SS has_colors
248 .PP
249 The \fBhas_colors\fR routine requires no arguments.
250 It returns \fBTRUE\fR if
251 the terminal can manipulate colors; otherwise, it returns \fBFALSE\fR.
252 This routine facilitates writing terminal-independent programs.
253 For example, a programmer can use it to decide
254 whether to use color or some other video attribute.
255 .SS can_change_color
256 .PP
257 The \fBcan_change_color\fR routine requires no arguments.
258 It returns \fBTRUE\fR if the terminal supports colors
259 and can change their definitions;
260 other, it returns \fBFALSE\fR.
261 This routine facilitates writing terminal-independent programs.
262 .SS init_pair
263 .PP
264 The \fBinit_pair\fR routine changes the definition of a color-pair.
265 It takes three arguments:
266 the number of the color-pair to be changed, the foreground
267 color number, and the background color number.
268 For portable applications:
269 .bP
270 The first argument must be a legal color pair value.
271 If default colors are used (see \fBuse_default_colors\fP)
272 the upper limit is adjusted to allow for extra pairs which use
273 a default color in foreground and/or background.
274 .bP
275 The second and third arguments must be legal color values.
276 .PP
277 If the color-pair was previously initialized,
278 the screen is refreshed and all occurrences of that color-pair
279 are changed to the new definition.
280 .PP
281 As an extension, ncurses allows you to set color pair \fB0\fP via
282 the \fBassume_default_colors\fR(3X) routine, or to specify the use of
283 default colors (color number \fB\-1\fR) if you first invoke the
284 \fBuse_default_colors\fR(3X) routine.
285 .PP
286 The extension \fBreset_color_pairs\fP tells ncurses to discard all
287 of the color-pair information which was set with \fBinit_pair\fP.
288 It also touches the current- and standard-screens, allowing an application to
289 switch color palettes rapidly.
290 .SS init_color
291 .PP
292 The \fBinit_color\fR routine changes the definition of a color.
293 It takes four arguments:
294 the number of the color to be changed followed by three RGB values
295 (for the amounts of red, green, and blue components).
296 .bP
297 The first argument must be a legal color value;
298 default colors are not allowed here.
299 (See the section \fBColors\fR for the default color index.)
300 .bP
301 Each of the last three arguments
302 must be a value in the range \fB0\fP through \fB1000\fP.
303 .PP
304 When \fBinit_color\fR is used, all
305 occurrences of that color on the screen immediately change to the new
306 definition.
307 .SS color_content
308 .PP
309 The \fBcolor_content\fR routine gives programmers a way to find the intensity
310 of the red, green, and blue (RGB) components in a color.
311 It requires four arguments: the color number, and three addresses
312 of \fBshort\fRs for storing
313 the information about the amounts of red, green, and blue components in the
314 given color.
315 .bP
316 The first argument must be a legal color value, i.e.,
317 \fB0\fP through \fBCOLORS\-1\fP, inclusive.
318 .bP
319 The values that are stored at the addresses pointed to by the
320 last three arguments are in the range
321 \fB0\fP (no component) through \fB1000\fP
322 (maximum amount of component), inclusive.
323 .SS pair_content
324 .PP
325 The \fBpair_content\fR routine allows programmers to find out what colors a
326 given color-pair consists of.
327 It requires three arguments: the color-pair
328 number, and two addresses of \fBshort\fRs for storing the foreground and the
329 background color numbers.
330 .bP
331 The first argument must be a legal color value,
332 i.e., in the range \fB1\fP through \fBCOLOR_PAIRS\-1\fR, inclusive.
333 .bP
334 The values that are stored at the addresses pointed
335 to by the second and third arguments are in the
336 range \fB0\fP through \fBCOLORS\fR, inclusive.
337 .SS PAIR_NUMBER
338 .PP
339 \fBPAIR_NUMBER(\fR\fIattrs\fR) extracts the color
340 value from its \fIattrs\fP parameter and returns it as a color pair number.
341 .SS COLOR_PAIR
342 Its inverse \fBCOLOR_PAIR(\fR\fIn\fR\fB)\fR converts a color pair number
343 to an attribute.
344 Attributes can hold color pairs in the range 0 to 255.
345 If you need a color pair larger than that, you must use functions
346 such as \fBattr_set\fP (which pass the color pair as a separate parameter)
347 rather than the legacy functions such as \fBattrset\fP.
348 .SH RETURN VALUE
349 The routines \fBcan_change_color\fR and \fBhas_colors\fR return \fBTRUE\fR
350 or \fBFALSE\fR.
351 .PP
352 All other routines return the integer \fBERR\fR upon failure and an \fBOK\fR
353 (SVr4 specifies only \*(``an integer value
354 other than \fBERR\fR\*('') upon successful completion.
355 .PP
356 X/Open defines no error conditions.
357 This implementation will return \fBERR\fR on attempts to
358 use color values outside the range \fB0\fP to \fBCOLORS\fP\-1
359 (except for the default colors extension),
360 or use color pairs outside the range \fB0\fP to \fBCOLOR_PAIRS\-1\fP.
361 Color values used in \fBinit_color\fP must be
362 in the range \fB0\fP to \fB1000\fP.
363 An error is returned from all functions
364 if the terminal has not been initialized.
365 An error is returned from secondary functions such as \fBinit_pair\fP
366 if \fBstart_color\fP was not called.
367 .RS 3
368 .TP 5
369 \fBinit_color\fP
370 returns an error if the terminal does not support
371 this feature, e.g., if the \fBinitialize_color\fP capability is absent
372 from the terminal description.
373 .TP 5
374 \fBstart_color\fP
375 returns an error if the color table cannot be allocated.
376 .RE
377 .SH NOTES
378 In the \fBncurses\fR implementation, there is a separate color activation flag,
379 color palette, color pairs table,
380 and associated \fBCOLORS\fP and \fBCOLOR_PAIRS\fP counts
381 for each screen; the \fBstart_color\fR function only affects the current
382 screen.
383 The SVr4/XSI interface is not really designed with this in mind, and
384 historical implementations may use a single shared color palette.
385 .PP
386 Setting an implicit background color via a color pair affects only
387 character cells that a character write operation explicitly touches.
388 To change
389 the background color used when parts of a window are blanked by erasing or
390 scrolling operations, see \fBcurs_bkgd\fR(3X).
391 .PP
392 Several caveats apply on older x86 machines
393 (e.g., i386, i486) with VGA-compatible graphics:
394 .bP
395 COLOR_YELLOW is actually brown.
396 To get yellow, use COLOR_YELLOW combined with the \fBA_BOLD\fR attribute.
397 .bP
398 The A_BLINK attribute should in theory cause the background to go bright.
399 This often fails to work, and even some cards for which it mostly works
400 (such as the
401 Paradise and compatibles) do the wrong thing when you try to set a bright
402 \*(``yellow\*('' background (you get a blinking yellow foreground instead).
403 .bP
404 Color RGB values are not settable.
405 .SH PORTABILITY
406 This implementation satisfies XSI Curses's minimum maximums
407 for \fBCOLORS\fR and \fBCOLOR_PAIRS\fR.
408 .PP
409 The \fBinit_pair\fP routine accepts negative values of foreground
410 and background color to support the \fBuse_default_colors\fR(3X) extension,
411 but only if that routine has been first invoked.
412 .PP
413 The assumption that \fBCOLOR_BLACK\fR is the default
414 background color for all terminals can be modified using the
415 \fBassume_default_colors\fR(3X) extension.
416 .PP
417 This implementation checks the pointers,
418 e.g., for the values returned by
419 \fBcolor_content\fP and \fBpair_content\fP,
420 and will treat those as optional parameters when null.
421 .PP
422 X/Open Curses does not specify a limit for the number of colors and
423 color pairs which a terminal can support.
424 However, in its use of \fBshort\fP for the parameters,
425 it carries over SVr4's implementation detail for the compiled
426 terminfo database, which uses signed 16-bit numbers.
427 This implementation provides extended versions of those functions
428 which use \fBshort\fP parameters,
429 allowing applications to use larger color- and pair-numbers.
430 .PP
431 The \fBreset_color_pairs\fP function is an extension of ncurses.
432 .SH SEE ALSO
433 \fBcurses\fR(3X),
434 \fBcurs_initscr\fR(3X),
435 \fBcurs_attr\fR(3X),
436 \fBcurs_variables\fR(3X),
437 \fBdefault_colors\fR(3X)