+
+ if (has_colors() && (start_color() == OK) && COLORS >= MAX_ANSI) {
+ static const struct {
+ int fg, bg;
+ } table[MAX_ANSI] = {
+#define DATA(fg,bg) { COLOR_##fg, COLOR_##bg }
+ DATA(RED, WHITE),
+ DATA(GREEN, WHITE),
+ DATA(YELLOW, BLACK),
+ DATA(BLUE, WHITE),
+ DATA(MAGENTA, WHITE),
+ DATA(MAGENTA, BLACK),
+ DATA(CYAN, WHITE),
+ DATA(CYAN, BLACK),
+#undef DATA
+ };
+ int n;
+ int pair = 1;
+
+ use_colors = TRUE;
+ /*
+ * Discounting color-pair 0 (no color), make the next 8 color pairs
+ * useful for leaving a visually distinct trail of characters on the
+ * screen.
+ */
+ for (n = 0; n < MAX_ANSI; ++n) {
+ init_pair((short) pair++, (short) table[n].fg, (short) table[n].bg);
+ }
+ /*
+ * After that, use color pairs for constructing a test-pattern, e.g.,
+ * imitating xterm's scripts.
+ */
+ if (fill_by == 0) {
+ if (COLORS <= 256) {
+ for (n = 0; n < COLORS; ++n)
+ init_pair((short) (n + MAX_ANSI), (short) n, (short) n);
+ }
+#if HAVE_TIGETSTR && USE_WIDEC_SUPPORT
+ else {
+ int r_max, g_max, b_max;
+
+ if (parse_rgb(&r_max, &g_max, &b_max) > 0) {
+ int rows = LINES - 1;
+ int cols = COLS - 1;
+ int b_delta = (b_max / rows);
+ int r_delta = (r_max / cols);
+ int g_delta = (g_max / cols);
+ int row = 0;
+
+ b = 0;
+ using_rgb = TRUE;
+ while (row++ < rows) {
+ int col = 0;
+ r = 0;
+ g = g_max;
+ while (col++ < cols) {
+ int color = (((r * (g_max + 1)) + g) * (b_max + 1)
+ + b + MAX_ANSI);
+#if USE_EXTENDED_COLOR
+ init_extended_pair(pair, color, color);
+#else
+ init_pair(pair, color, color);
+#endif
+ pair++;
+ r += r_delta;
+ g -= g_delta;
+ }
+ b += b_delta;
+ }
+ }
+ }
+#endif
+ }
+ if ((fill_by == 0) && !replaying) {
+ /*
+ * Originally (before wide-characters) ncurses supported 16 colors.
+ */
+ if (COLORS >= 16 && COLORS <= 256) {
+ mvprintw(2, 0, "System colors:\n");
+ for (n = 0; n < 16; ++n) {
+ pair = n + MAX_ANSI;
+ addch((chtype) (' ' | COLOR_PAIR(pair)));
+ addch((chtype) (' ' | COLOR_PAIR(pair)));
+ if (((n + 1) % 8) == 0)
+ addch('\n');
+ }
+ }
+ /*
+ * Even with ncurses, you need wide-character support to have more
+ * than 16 colors.
+ */
+#if USE_WIDEC_SUPPORT
+ if (COLORS == 88) {
+ cube = 4;
+ } else if (COLORS == 256) {
+ cube = 6;
+ }
+ if (cube != 0) {
+ cube0 = 16;
+ cube1 = cube0 + (cube * cube * cube);
+
+ addch('\n');
+ printw("Color cube, %dx%dx%d:\n", cube, cube, cube);
+ for (g = 0; g < cube; g++) {
+ for (r = 0; r < cube; r++) {
+ for (b = 0; b < cube; b++) {
+ pair = MAX_ANSI
+ + 16
+ + (r * cube * cube) + (g * cube) + b;
+ setcchar(&mycc, mywc, 0, (short) pair, NULL);
+ add_wch(&mycc);
+ add_wch(&mycc);
+ }
+ addch(' ');
+ }
+ addch('\n');
+ }
+ addch('\n');
+ printw("Grayscale ramp:\n");
+ for (n = cube1; n < COLORS; ++n) {
+ pair = n + MAX_ANSI;
+ setcchar(&mycc, mywc, 0, (short) pair, NULL);
+ add_wch(&mycc);
+ add_wch(&mycc);
+ }
+ } else if ((COLORS > 256) && using_rgb) {
+ int rows = LINES - 1;
+ int cols = COLS - 1;
+ int row = 0;
+
+ b = 0;
+ pair = MAX_ANSI;
+ while (row++ < rows) {
+ int col = 0;
+ while (col++ < cols) {
+ setcchar(&mycc, mywc, 0, (short) pair, &pair);
+ add_wch(&mycc);
+ ++pair;
+ }
+ addch('\n');
+ }
+ addch('\n');
+ }
+#endif
+ }
+ }
+
+ if (fill_by != 0) {
+ FILE *fp = fopen(fill_by, "r");
+ if (fp != 0) {
+ bool filled = FALSE;
+ move(1, 0);
+ while ((ch = fgetc(fp)) != EOF) {
+ if (addch(UChar(ch)) == ERR) {
+ filled = TRUE;
+ break;
+ }
+ }
+ fclose(fp);
+ if (!filled) {
+ while (addch(' ') != ERR) {
+ ;
+ }
+ }
+ move(0, 0);
+ } else {
+ stop_curses();
+ fprintf(stderr, "Cannot open \"%s\"\n", fill_by);
+ ExitProgram(EXIT_FAILURE);