1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 * $Id: extended_color.c,v 1.15 2020/02/02 23:34:34 tom Exp $
33 #include <test.priv.h>
35 #if USE_EXTENDED_COLOR
37 #define SHOW(n) ((n) == ERR ? "ERR" : "OK")
40 static bool opt_s = FALSE;
41 #define if_opt_s(a,b) (opt_s ? (a) : (b))
43 #define if_opt_s(a,b) (b)
47 failed(const char *name)
49 printw("...%s failed", name);
52 ExitProgram(EXIT_FAILURE);
56 do_pair_content(SCREEN *sp, int pair)
61 i = if_opt_s(extended_pair_content_sp(sp, pair, &f, &b),
62 extended_pair_content(0, &f, &b));
64 failed("pair_content");
65 printw("pair %d contains (%d,%d)\n", pair, f, b);
70 do_init_pair(SCREEN *sp, int pair, int fg, int bg)
75 i = if_opt_s(init_extended_pair_sp(sp, pair, fg, bg),
76 init_extended_pair(pair, fg, bg));
82 do_init_color(SCREEN *sp, int color, int adjust)
88 i = if_opt_s(extended_color_content_sp(sp, color, &r, &g, &b),
89 extended_color_content(color, &r, &g, &b));
91 failed("color_content");
93 r = (adjust + 1000 + r) % 1000;
94 g = (adjust + 1000 + g) % 1000;
95 b = (adjust + 1000 + b) % 1000;
97 i = if_opt_s(init_extended_color_sp(sp, color, r, g, b),
98 init_extended_color(color, r, g, b));
100 failed("init_color");
104 do_color_set(const char *expected, int pair)
106 int i = color_set((short) pair, (void *) &pair);
107 printw("%s (%s)\n", expected, SHOW(i));
114 show_1_rgb(SCREEN *sp, const char *name, int color, int y, int x)
120 i = if_opt_s(extended_color_content_sp(sp, color, &r, &g, &b),
121 extended_color_content(color, &r, &g, &b));
124 printw("%-8s %3d/%3d/%3d", name, r, g, b);
126 printw("%-8s %s", name, SHOW(i));
135 show_1_rgb(sp, "RED", COLOR_RED, y + 1, x);
136 show_1_rgb(sp, "GREEN", COLOR_GREEN, y + 2, x);
137 show_1_rgb(sp, "BLUE", COLOR_BLUE, y + 3, x);
144 static const char *tbl[] =
146 "Usage: extended_color",
153 for (n = 0; n < SIZEOF(tbl); ++n) {
154 fprintf(stderr, "%s\n", tbl[n]);
156 ExitProgram(EXIT_FAILURE);
160 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
165 while ((i = getopt(argc, argv, "s")) != -1) {
179 sp = newterm(NULL, stdout, stdin);
185 fprintf(stderr, "This demo requires a color terminal\n");
186 ExitProgram(EXIT_FAILURE);
191 do_pair_content(sp, 0);
193 printw("Initializing pair 1 to red/black\n");
194 do_init_pair(sp, 1, COLOR_RED, COLOR_BLACK);
195 do_color_set("RED/BLACK", 1);
197 printw("Initializing pair 2 to white/blue\n");
198 do_init_pair(sp, 2, COLOR_WHITE, COLOR_BLUE);
199 do_color_set("WHITE/BLUE", 2);
201 printw("Initializing pair 3 to green/black\n");
202 do_init_pair(sp, 3, COLOR_GREEN, COLOR_BLACK);
203 do_color_set("GREEN/BLACK", 3);
205 printw("Resetting colors to pair 0\n");
206 do_color_set("Default Colors", 0);
208 printw("Resetting colors to pair 1\n");
209 do_color_set("RED/BLACK", 1);
211 printw("Drawing soft-key tabs with pair 2\n");
212 slk_attrset(A_BOLD); /* reverse-video is hard to see */
213 (void) if_opt_s(extended_slk_color_sp(sp, 2),
214 extended_slk_color(2));
215 for (i = 1; i <= 8; ++i) {
217 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "(SLK-%d)", i);
223 i = if_opt_s(can_change_color_sp(sp),
226 do_color_set("Default Colors", 0);
227 printw("Press any key to stop...\n");
228 nodelay(stdscr, TRUE);
229 while (getch() == ERR) {
231 do_init_color(sp, COLOR_RED, 1);
232 do_init_color(sp, COLOR_BLUE, -1);
236 nodelay(stdscr, FALSE);
242 ExitProgram(EXIT_SUCCESS);
249 printf("This program requires the ncurses extended color/pair functions\n");
250 ExitProgram(EXIT_FAILURE);