1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: color_content.c,v 1.17 2022/12/04 00:40:11 tom Exp $
33 #include <test.priv.h>
35 #if USE_EXTENDED_COLOR
36 typedef int my_color_t;
38 typedef NCURSES_COLOR_T my_color_t;
55 #if USE_EXTENDED_COLOR
59 static MYCOLOR *expected;
62 static struct timeval initial_time;
63 static struct timeval finish_time;
67 failed(const char *msg)
72 ExitProgram(EXIT_FAILURE);
75 #if USE_EXTENDED_COLOR
77 InitColor(int pair, int r, int g, int b)
81 rc = init_extended_color(pair, r, g, b);
83 rc = init_color((NCURSES_PAIRS_T) pair,
92 ColorContent(int color, int *rp, int *gp, int *bp)
96 rc = extended_color_content(color, rp, gp, bp);
98 NCURSES_COLOR_T r, g, b;
99 if ((rc = color_content((NCURSES_COLOR_T) color, &r, &g, &b)) == OK) {
108 #define InitColor(color,r,g,b) init_color((NCURSES_COLOR_T)color,(NCURSES_COLOR_T)r,(NCURSES_COLOR_T)g,(NCURSES_COLOR_T)b)
109 #define ColorContent(color,rp,gp,bp) color_content((NCURSES_COLOR_T)color,rp,gp,bp)
115 return (my_color_t) (rand() % 1000);
121 setlocale(LC_ALL, "");
125 scrollok(stdscr, TRUE);
128 if (!can_change_color() && !p_opt)
129 failed("this terminal cannot initialize colors");
136 failed("color limit must be greater than zero");
140 size_t need = (size_t) ((l_opt > COLORS) ? l_opt : COLORS) + 1;
142 expected = typeCalloc(MYCOLOR, need);
148 for (r = 0; r < 1000; ++r) {
149 for (g = 0; g < 1000; ++g) {
150 for (b = 0; b < 1000; ++b) {
152 InitColor(color, r, g, b);
153 expected[color].r = (my_color_t) r;
154 expected[color].g = (my_color_t) g;
155 expected[color].b = (my_color_t) b;
164 for (color = f_opt; color < l_opt; ++color) {
165 expected[color].r = random_color();
166 expected[color].g = random_color();
167 expected[color].b = random_color();
176 failed("This demo requires a color terminal");
178 #if HAVE_GETTIMEOFDAY
179 gettimeofday(&initial_time, 0);
188 for (color = f_opt; color < l_opt; ++color) {
192 if (ColorContent(color, &r, &g, &b) == OK) {
194 if (r != expected[color].r)
196 if (g != expected[color].g)
198 if (b != expected[color].b)
204 addch(success ? '.' : '?');
216 #if HAVE_GETTIMEOFDAY
218 seconds(struct timeval *mark)
220 double result = (double) mark->tv_sec;
221 result += ((double) mark->tv_usec / 1e6);
229 static const char *msg[] =
231 "Usage: color_content [options]"
235 ," -f COLOR first color value to test (default: 0)"
236 ," -i interactive, showing test-progress"
237 ," -l COLOR last color value to test (default: max_colors-1)"
238 ," -n do not initialize color pairs"
239 ," -p print data for color content instead of testing"
240 ," -r COUNT repeat for given count"
241 ," -s initialize pairs sequentially rather than random"
242 #if USE_EXTENDED_COLOR
243 ," -x use extended color pairs/values"
247 for (n = 0; n < SIZEOF(msg); n++)
248 fprintf(stderr, "%s\n", msg[n]);
249 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
256 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
260 while ((ch = getopt(argc, argv, OPTS_COMMON "f:il:npr:sx")) != -1) {
263 if ((f_opt = atoi(optarg)) <= 0)
270 if ((l_opt = atoi(optarg)) <= 0)
280 if ((r_opt = atoi(optarg)) <= 0)
286 #if USE_EXTENDED_COLOR
293 ExitProgram(EXIT_SUCCESS);
295 usage(ch == OPTS_USAGE);
308 for (i = 0; i < COLORS; ++i) {
310 if (ColorContent(i, &r, &g, &b) == OK) {
311 printf("%d: %d %d %d\n", i, r, g, b);
313 printf("%d: ? ?\n", i);
319 for (repeat = 0; repeat < r_opt; ++repeat) {
331 #if HAVE_GETTIMEOFDAY
332 gettimeofday(&finish_time, 0);
333 printw("%.03f seconds",
334 seconds(&finish_time)
335 - seconds(&initial_time));
341 ExitProgram(EXIT_SUCCESS);