]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/color_content.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / color_content.c
index 77b04f17f5426508c8eebcc6433660cf90a7a7dc..d08dffb09fb5c267d44b37d9102d5da8ebca3334 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2018 Free Software Foundation, Inc.                        *
+ * Copyright 2018-2022,2023 Thomas E. Dickey                                *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: color_content.c,v 1.2 2018/12/30 00:52:58 tom Exp $
+ * $Id: color_content.c,v 1.19 2023/02/25 16:48:02 tom Exp $
  */
 
 #define NEED_TIME_H
 #include <test.priv.h>
 
+#if USE_EXTENDED_COLOR
+typedef int my_color_t;
+#else
+typedef NCURSES_COLOR_T my_color_t;
+#endif
+
 typedef struct {
-    NCURSES_COLOR_T r;
-    NCURSES_COLOR_T g;
-    NCURSES_COLOR_T b;
+    my_color_t r;
+    my_color_t g;
+    my_color_t b;
 } MYCOLOR;
 
+static int f_opt;
 static int i_opt;
 static int l_opt;
 static int n_opt;
+static int p_opt;
 static int r_opt;
 static int s_opt;
 
+#if USE_EXTENDED_COLOR
+static int x_opt;
+#endif
+
 static MYCOLOR *expected;
 
-#if HAVE_GETTIMEOFDAY
-static struct timeval initial_time;
-static struct timeval finish_time;
-#endif
+static TimeType initial_time;
+static TimeType finish_time;
 
 static void
 failed(const char *msg)
@@ -60,46 +70,87 @@ failed(const char *msg)
     ExitProgram(EXIT_FAILURE);
 }
 
-static NCURSES_COLOR_T
+#if USE_EXTENDED_COLOR
+static int
+InitColor(int pair, int r, int g, int b)
+{
+    int rc;
+    if (x_opt) {
+       rc = init_extended_color(pair, r, g, b);
+    } else {
+       rc = init_color((NCURSES_PAIRS_T) pair,
+                       (NCURSES_COLOR_T) r,
+                       (NCURSES_COLOR_T) g,
+                       (NCURSES_COLOR_T) b);
+    }
+    return rc;
+}
+
+static int
+ColorContent(int color, int *rp, int *gp, int *bp)
+{
+    int rc;
+    if (x_opt) {
+       rc = extended_color_content(color, rp, gp, bp);
+    } else {
+       NCURSES_COLOR_T r, g, b;
+       if ((rc = color_content((NCURSES_COLOR_T) color, &r, &g, &b)) == OK) {
+           *rp = r;
+           *gp = g;
+           *bp = b;
+       }
+    }
+    return rc;
+}
+#else
+#define InitColor(color,r,g,b)       init_color((NCURSES_COLOR_T)color,(NCURSES_COLOR_T)r,(NCURSES_COLOR_T)g,(NCURSES_COLOR_T)b)
+#define ColorContent(color,rp,gp,bp) color_content((NCURSES_COLOR_T)color,rp,gp,bp)
+#endif
+
+static my_color_t
 random_color(void)
 {
-    return (NCURSES_COLOR_T) (rand() % 1000);
+    return (my_color_t) (rand() % 1000);
 }
 
 static void
 setup_test(void)
 {
+    setlocale(LC_ALL, "");
     initscr();
     cbreak();
     noecho();
     scrollok(stdscr, TRUE);
     if (has_colors()) {
        start_color();
-       if (!can_change_color())
+       if (!can_change_color() && !p_opt)
            failed("this terminal cannot initialize colors");
 
+       if (!f_opt)
+           f_opt = 0;
        if (!l_opt)
            l_opt = COLORS;
        if (l_opt <= 0)
            failed("color limit must be greater than zero");
 
        if (!n_opt) {
-           NCURSES_PAIRS_T color;
+           int color;
+           size_t need = (size_t) ((l_opt > COLORS) ? l_opt : COLORS) + 1;
 
-           expected = typeCalloc(MYCOLOR, l_opt);
+           expected = typeCalloc(MYCOLOR, need);
            if (s_opt) {
-               NCURSES_COLOR_T r;
-               NCURSES_COLOR_T g;
-               NCURSES_COLOR_T b;
-               color = 0;
+               int r;
+               int g;
+               int b;
+               color = f_opt;
                for (r = 0; r < 1000; ++r) {
                    for (g = 0; g < 1000; ++g) {
                        for (b = 0; b < 1000; ++b) {
                            if (color < l_opt) {
-                               init_color(color, r, g, b);
-                               expected[color].r = r;
-                               expected[color].g = g;
-                               expected[color].b = b;
+                               InitColor(color, r, g, b);
+                               expected[color].r = (my_color_t) r;
+                               expected[color].g = (my_color_t) g;
+                               expected[color].b = (my_color_t) b;
                                ++color;
                            } else {
                                break;
@@ -108,33 +159,33 @@ setup_test(void)
                    }
                }
            } else {
-               for (color = 1; color < l_opt; ++color) {
+               for (color = f_opt; color < l_opt; ++color) {
                    expected[color].r = random_color();
                    expected[color].g = random_color();
                    expected[color].b = random_color();
-                   init_color(color, expected[color].r, expected[color].g,
-                              expected[color].b);
+                   InitColor(color,
+                             expected[color].r,
+                             expected[color].g,
+                             expected[color].b);
                }
            }
        }
     } else {
        failed("This demo requires a color terminal");
     }
-#if HAVE_GETTIMEOFDAY
-    gettimeofday(&initial_time, 0);
-#endif
+    GetClockTime(&initial_time);
 }
 
 static void
 run_test(void)
 {
-    NCURSES_PAIRS_T color;
+    int color;
     bool success = TRUE;
-    for (color = 0; color < l_opt; ++color) {
-       NCURSES_COLOR_T r;
-       NCURSES_COLOR_T g;
-       NCURSES_COLOR_T b;
-       if (color_content(color, &r, &g, &b) == OK) {
+    for (color = f_opt; color < l_opt; ++color) {
+       my_color_t r;
+       my_color_t g;
+       my_color_t b;
+       if (ColorContent(color, &r, &g, &b) == OK) {
            if (expected != 0) {
                if (r != expected[color].r)
                    success = FALSE;
@@ -158,91 +209,116 @@ finish_test(void)
     endwin();
 }
 
-#if HAVE_GETTIMEOFDAY
-static double
-seconds(struct timeval *mark)
-{
-    double result = (double) mark->tv_sec;
-    result += ((double) mark->tv_usec / 1e6);
-    return result;
-}
-#endif
-
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: pair_content [options]"
+       "Usage: color_content [options]"
        ,""
+       ,USAGE_COMMON
        ,"Options:"
+       ," -f COLOR first color value to test (default: 0)"
        ," -i       interactive, showing test-progress"
-       ," -l NUM   test NUM color pairs, rather than terminal description"
+       ," -l COLOR last color value to test (default: max_colors-1)"
        ," -n       do not initialize color pairs"
+       ," -p       print data for color content instead of testing"
        ," -r COUNT repeat for given count"
        ," -s       initialize pairs sequentially rather than random"
+#if USE_EXTENDED_COLOR
+       ," -x       use extended color pairs/values"
+#endif
     };
     size_t n;
     for (n = 0; n < SIZEOF(msg); n++)
        fprintf(stderr, "%s\n", msg[n]);
-    ExitProgram(EXIT_FAILURE);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
-main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
+main(int argc, char *argv[])
 {
-    int i;
-    int repeat;
+    int ch;
 
-    while ((i = getopt(argc, argv, "il:nr:s")) != -1) {
-       switch (i) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "f:il:npr:sx")) != -1) {
+       switch (ch) {
+       case 'f':
+           if ((f_opt = atoi(optarg)) <= 0)
+               usage(FALSE);
+           break;
        case 'i':
            i_opt = 1;
            break;
        case 'l':
            if ((l_opt = atoi(optarg)) <= 0)
-               usage();
+               usage(FALSE);
            break;
        case 'n':
            n_opt = 1;
            break;
+       case 'p':
+           p_opt = 1;
+           break;
        case 'r':
            if ((r_opt = atoi(optarg)) <= 0)
-               usage();
+               usage(FALSE);
            break;
        case 's':
            s_opt = 1;
            break;
+#if USE_EXTENDED_COLOR
+       case 'x':
+           x_opt = 1;
+           break;
+#endif
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
     if (optind < argc)
-       usage();
+       usage(FALSE);
     if (r_opt <= 0)
        r_opt = 1;
 
     setup_test();
+    if (p_opt) {
+       int i;
+       endwin();
+       for (i = 0; i < COLORS; ++i) {
+           my_color_t r, g, b;
+           if (ColorContent(i, &r, &g, &b) == OK) {
+               printf("%d: %d %d %d\n", i, r, g, b);
+           } else {
+               printf("%d: ? ?\n", i);
+           }
+       }
+    } else {
+       int repeat;
 
-    for (repeat = 0; repeat < r_opt; ++repeat) {
-       run_test();
-       if (i_opt) {
-           addch('.');
-           refresh();
+       for (repeat = 0; repeat < r_opt; ++repeat) {
+           run_test();
+           if (i_opt) {
+               addch('.');
+               refresh();
+           }
        }
-    }
 
-    if (i_opt) {
-       addch('\n');
+       if (i_opt) {
+           addch('\n');
+       }
+       printw("DONE: ");
+       GetClockTime(&finish_time);
+       printw("%.03f seconds", ElapsedSeconds(&finish_time, &initial_time));
+       finish_test();
     }
-    printw("DONE: ");
-#if HAVE_GETTIMEOFDAY
-    gettimeofday(&finish_time, 0);
-    printw("%.03f seconds",
-          seconds(&finish_time)
-          - seconds(&initial_time));
-#endif
-    finish_test();
 
+    free(expected);
     ExitProgram(EXIT_SUCCESS);
 }