micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 1 | .\"*************************************************************************** |
| 2 | .\" Copyright 2018-2023,2024 Thomas E. Dickey * |
| 3 | .\" Copyright 2017 Free Software Foundation, Inc. * |
| 4 | .\" * |
| 5 | .\" Permission is hereby granted, free of charge, to any person obtaining a * |
| 6 | .\" copy of this software and associated documentation files (the * |
| 7 | .\" "Software"), to deal in the Software without restriction, including * |
| 8 | .\" without limitation the rights to use, copy, modify, merge, publish, * |
| 9 | .\" distribute, distribute with modifications, sublicense, and/or sell * |
| 10 | .\" copies of the Software, and to permit persons to whom the Software is * |
| 11 | .\" furnished to do so, subject to the following conditions: * |
| 12 | .\" * |
| 13 | .\" The above copyright notice and this permission notice shall be included * |
| 14 | .\" in all copies or substantial portions of the Software. * |
| 15 | .\" * |
| 16 | .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * |
| 17 | .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * |
| 18 | .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * |
| 19 | .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * |
| 20 | .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * |
| 21 | .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * |
| 22 | .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. * |
| 23 | .\" * |
| 24 | .\" Except as contained in this notice, the name(s) of the above copyright * |
| 25 | .\" holders shall not be used in advertising or otherwise to promote the * |
| 26 | .\" sale, use or other dealings in this Software without prior written * |
| 27 | .\" authorization. * |
| 28 | .\"*************************************************************************** |
| 29 | .\" |
| 30 | .\" Author: Thomas E. Dickey |
| 31 | .\" |
| 32 | .\" $Id: new_pair.3x,v 1.46 2024/03/16 15:35:01 tom Exp $ |
| 33 | .TH new_pair 3X 2024-03-16 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "Library calls" |
| 34 | .ie \n(.g \{\ |
| 35 | .ds `` \(lq |
| 36 | .ds '' \(rq |
| 37 | .\} |
| 38 | .el \{\ |
| 39 | .ie t .ds `` `` |
| 40 | .el .ds `` "" |
| 41 | .ie t .ds '' '' |
| 42 | .el .ds '' "" |
| 43 | .\} |
| 44 | . |
| 45 | .de bP |
| 46 | .ie n .IP \(bu 4 |
| 47 | .el .IP \(bu 2 |
| 48 | .. |
| 49 | .SH NAME |
| 50 | \fB\%alloc_pair\fP, |
| 51 | \fB\%find_pair\fP, |
| 52 | \fB\%free_pair\fP \- |
| 53 | dynamically allocate \fIcurses\fR color pairs |
| 54 | .SH SYNOPSIS |
| 55 | .nf |
| 56 | \fB#include <curses.h> |
| 57 | .PP |
| 58 | \fBint alloc_pair(int \fIfg\fP, int \fIbg\fP); |
| 59 | \fBint find_pair(int \fIfg\fP, int \fIbg\fP); |
| 60 | \fBint free_pair(int \fIpair\fP); |
| 61 | .fi |
| 62 | .SH DESCRIPTION |
| 63 | These functions are an extension to the \fIcurses\fP library. |
| 64 | They permit an application to dynamically allocate a color pair using |
| 65 | the foreground/background colors rather than assign a fixed color pair number, |
| 66 | and return an unused pair to the pool. |
| 67 | .PP |
| 68 | The number of colors may be related to the number of possible color |
| 69 | pairs for a given terminal, or it may not: |
| 70 | .bP |
| 71 | While almost all terminals allow setting the color \fIattributes\fP |
| 72 | independently, |
| 73 | it is unlikely that your terminal allows you to modify the attributes |
| 74 | of a given character cell without rewriting it. |
| 75 | That is, the foreground and background colors are applied as a pair. |
| 76 | .bP |
| 77 | Color pairs are the \fIcurses\fP library's way of managing a color palette |
| 78 | on a terminal. |
| 79 | If the library does not keep track of the \fIcombinations\fP of |
| 80 | colors which are displayed, it will be inefficient. |
| 81 | .IP \(bu 4 |
| 82 | For simple terminal emulators |
| 83 | with only a few dozen color combinations, |
| 84 | it is convenient to use the maximum number of combinations |
| 85 | as the limit on color pairs: |
| 86 | .PP |
| 87 | .RS 8 |
| 88 | .EX |
| 89 | \fBCOLORS\fI * \fBCOLORS\fR |
| 90 | .EE |
| 91 | .RE |
| 92 | .IP \(bu 4 |
| 93 | Terminals which support \fIdefault colors\fP distinct |
| 94 | from \*(``ANSI colors\*('' |
| 95 | add to the possible combinations, producing this total: |
| 96 | .PP |
| 97 | .RS 8 |
| 98 | .EX |
| 99 | \fI( \fBCOLORS\fI + 1 ) * ( \fBCOLORS\fI + 1 )\fR |
| 100 | .EE |
| 101 | .RE |
| 102 | .bP |
| 103 | An application might use up to a few dozen color pairs to |
| 104 | implement a predefined color scheme. |
| 105 | .IP |
| 106 | Beyond that lies in the realm of programs using the foreground |
| 107 | and background colors for \*(``ASCII art\*('' |
| 108 | (or some other non-textual application). |
| 109 | .IP |
| 110 | Also beyond those few dozen pairs, the required size for a table |
| 111 | to represent the combinations grows rapidly with an increasing number of colors. |
| 112 | .IP |
| 113 | These functions allow a developer to let the screen library |
| 114 | manage color pairs. |
| 115 | .SS alloc_pair |
| 116 | The \fBalloc_pair\fP function accepts parameters for |
| 117 | foreground and background color, and |
| 118 | checks if that color combination is already associated with a color pair. |
| 119 | .bP |
| 120 | If the combination already exists, |
| 121 | \fBalloc_pair\fP returns the existing pair. |
| 122 | .bP |
| 123 | If the combination does not exist, |
| 124 | \fBalloc_pair\fP allocates a new color pair and returns that. |
| 125 | .bP |
| 126 | If the table fills up, \fBalloc_pair\fP discards the least-recently |
| 127 | allocated entry using \fBfree_pair\fP and allocates a new color pair. |
| 128 | .PP |
| 129 | All of the color pairs are allocated from a table of possible color pairs. |
| 130 | The size of the table is determined by the terminfo \fBpairs\fP capability. |
| 131 | The table is shared with \fBinit_pair\fP; |
| 132 | in fact \fBalloc_pair\fP calls \fBinit_pair\fP after |
| 133 | updating the \fI\%ncurses\fP library's fast index |
| 134 | to the colors versus color pairs. |
| 135 | .SS find_pair |
| 136 | The \fBfind_pair\fP function accepts parameters for |
| 137 | foreground and background color, and |
| 138 | checks if that color combination is already associated with a color pair, |
| 139 | returning the pair number if it has been allocated. |
| 140 | Otherwise it returns \-1. |
| 141 | .SS free_pair |
| 142 | Marks the given color pair as unused, |
| 143 | i.e., like color pair 0. |
| 144 | .SH RETURN VALUE |
| 145 | The \fBalloc_pair\fP function returns a color pair number in the range |
| 146 | 1 through \fBCOLOR_PAIRS\fP\-1, unless it encounters an error updating |
| 147 | its fast index to the color pair values, preventing it from allocating |
| 148 | a color pair. |
| 149 | In that case, it returns \-1. |
| 150 | .PP |
| 151 | The \fBfind_pair\fP function returns a color pair number if the |
| 152 | given color combination has been associated with a color pair, |
| 153 | or \-1 if not. |
| 154 | .PP |
| 155 | Likewise, \fBfree_pair\fP returns \fBOK\fP unless it encounters an |
| 156 | error updating the fast index or if no such color pair is in use. |
| 157 | .SH PORTABILITY |
| 158 | These routines are specific to \fI\%ncurses\fP. |
| 159 | They were not supported on |
| 160 | Version 7, BSD or System V implementations. |
| 161 | It is recommended that |
| 162 | any code depending on them be conditioned using \fB\%NCURSES_VERSION\fP. |
| 163 | .SH AUTHORS |
| 164 | Thomas Dickey |
| 165 | .SH SEE ALSO |
| 166 | \fB\%curs_color\fP(3X) |