From 243f6410d296a54ead72ca9a14f648e50e0f2b9a Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Fri, 15 Mar 2024 20:21:31 +0200 Subject: [PATCH] Curses: simplify color handling The map frame (background) colors were all over the place; the code should be much cleaner now, and still work exactly the same as before. I tested this with terminals with 8, 16, and 256 COLORS. --- win/curses/cursinit.c | 84 +++++++++++++++---------------------------- win/curses/cursmisc.c | 25 ++++++++----- win/curses/curswins.c | 19 ++-------- 3 files changed, 47 insertions(+), 81 deletions(-) diff --git a/win/curses/cursinit.c b/win/curses/cursinit.c index 09b2d5b62..9b2380bb4 100644 --- a/win/curses/cursinit.c +++ b/win/curses/cursinit.c @@ -301,66 +301,40 @@ curses_create_main_windows(void) void curses_init_nhcolors(void) { - if (has_colors()) { - use_default_colors(); - init_pair(1, COLOR_BLACK, -1); - init_pair(2, COLOR_RED, -1); - init_pair(3, COLOR_GREEN, -1); - init_pair(4, COLOR_YELLOW, -1); - init_pair(5, COLOR_BLUE, -1); - init_pair(6, COLOR_MAGENTA, -1); - init_pair(7, COLOR_CYAN, -1); - init_pair(8, COLOR_WHITE, -1); + /* COLOR_foo + 8 means COLOR | A_BOLD when COLORS < 16 */ + /* otherwise assume the terminal has least 16 different colors */ + /* these map to the NetHack CLR_ defines */ + static const int fg_clr[16] = { + COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, + COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE, + -1, COLOR_RED + 8, COLOR_GREEN + 8, COLOR_YELLOW + 8, + COLOR_BLUE + 8, COLOR_MAGENTA + 8, COLOR_CYAN + 8, COLOR_WHITE + 8 + }; + static const int bg_clr[8] = { + -1, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, + COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE + }; + int bg, nhclr; + int maxc = (COLORS >= 16) ? 16 : 8; - { - int i; - boolean hicolor = FALSE; + if (!has_colors()) + return; - static const int clr_remap[16] = { - COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, - COLOR_BLUE, - COLOR_MAGENTA, COLOR_CYAN, -1, COLOR_WHITE, - COLOR_RED + 8, COLOR_GREEN + 8, COLOR_YELLOW + 8, - COLOR_BLUE + 8, - COLOR_MAGENTA + 8, COLOR_CYAN + 8, COLOR_WHITE + 8 - }; + use_default_colors(); - for (i = 0; i < (COLORS >= 16 ? 16 : 8); i++) { - init_pair(17 + (i * 2) + 0, clr_remap[i], COLOR_RED); - init_pair(17 + (i * 2) + 1, clr_remap[i], COLOR_BLUE); - } - - if (COLORS >= 16) - hicolor = TRUE; - - /* Work around the crazy definitions above for more background - colors... */ - for (i = 0; i < (COLORS >= 16 ? 16 : 8); i++) { - init_pair((hicolor ? 49 : 9) + i, clr_remap[i], COLOR_GREEN); - init_pair((hicolor ? 65 : 33) + i, clr_remap[i], COLOR_YELLOW); - init_pair((hicolor ? 81 : 41) + i, clr_remap[i], COLOR_MAGENTA); - init_pair((hicolor ? 97 : 49) + i, clr_remap[i], COLOR_CYAN); - init_pair((hicolor ? 113 : 57) + i, clr_remap[i], COLOR_WHITE); - } - } - - - if (COLORS >= 16) { -# ifdef USE_DARKGRAY - if (iflags.wc2_darkgray) { - init_pair(1, COLOR_BLACK + 8, -1); - } -# endif - init_pair(9, -1, -1); - init_pair(10, COLOR_RED + 8, -1); - init_pair(11, COLOR_GREEN + 8, -1); - init_pair(12, COLOR_YELLOW + 8, -1); - init_pair(13, COLOR_BLUE + 8, -1); - init_pair(14, COLOR_MAGENTA + 8, -1); - init_pair(15, COLOR_CYAN + 8, -1); - init_pair(16, COLOR_WHITE + 8, -1); + for (nhclr = CLR_BLACK; nhclr < maxc; nhclr++) { + for (bg = 0; bg < 8; bg++) { + init_pair((maxc * bg) + nhclr + 1, fg_clr[nhclr], bg_clr[bg]); } } + +#ifdef USE_DARKGRAY + if (COLORS >= 16) { + if (iflags.wc2_darkgray) { + init_pair(CLR_BLACK + 1, COLOR_BLACK + 8, -1); + } + } +#endif } #if 0 /* curses_choose_character + curses_character_dialog no longer used */ diff --git a/win/curses/cursmisc.c b/win/curses/cursmisc.c index 2e3f3bd42..40585e448 100644 --- a/win/curses/cursmisc.c +++ b/win/curses/cursmisc.c @@ -78,6 +78,7 @@ curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff) color = NONE; int curses_color; + boolean use_bold = FALSE; /* if color is disabled, just show attribute */ if ((win == mapwin) ? !iflags.wc_color @@ -93,7 +94,7 @@ curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff) return; } - if (color == 0) { /* make black fg visible */ + if (color == CLR_BLACK) { /* make black fg visible */ # ifdef USE_DARKGRAY if (iflags.wc2_darkgray) { if (COLORS > 16) { @@ -105,17 +106,23 @@ curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff) # endif/* USE_DARKGRAY */ color = CLR_BLUE; } - curses_color = color + 1; + if (COLORS < 16) { - if (curses_color > 8 && curses_color < 17) - curses_color -= 8; - else if (curses_color > (17 + 16)) - curses_color -= 16; + /* convert NetHack's 16 colors to 8 colors + BOLD */ + int fg = color % 16; + int bg = color / 16; + + if (fg > 7) + use_bold = TRUE; + + curses_color = (8 * (bg % 8)) + (fg % 8) + 1; + } else { + curses_color = color + 1; } + if (onoff == ON) { /* Turn on color/attributes */ if (color != NONE) { - if ((((color > 7) && (color < 17)) || - (color > 17 + 17)) && (COLORS < 16)) { + if (use_bold) { wattron(win, A_BOLD); } wattron(win, COLOR_PAIR(curses_color)); @@ -127,7 +134,7 @@ curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff) } else { /* Turn off color/attributes */ if (color != NONE) { - if ((color > 7) && (COLORS < 16)) { + if (use_bold) { wattroff(win, A_BOLD); } # ifdef USE_DARKGRAY diff --git a/win/curses/curswins.c b/win/curses/curswins.c index 8e4d03b39..08dfdbde1 100644 --- a/win/curses/curswins.c +++ b/win/curses/curswins.c @@ -607,23 +607,8 @@ coordinates without a refresh. Currently only used for the map. */ static int get_framecolor(int nhcolor, int framecolor) { - boolean hicolor = (COLORS >= 16), adj_framecolor = framecolor; - static int framecolors[16][8] = { - { 0, 16, 8, 32, 17, 40, 48, 0 }, { 1, 18, 9, 33, 19, 41, 49, 1 }, - { 2, 20, 10, 34, 21, 42, 50, 2 }, { 3, 22, 11, 35, 23, 43, 51, 3 }, - { 4, 24, 12, 36, 25, 44, 52, 4 }, { 5, 26, 13, 37, 27, 45, 53, 5 }, - { 6, 28, 14, 38, 29, 46, 54, 6 }, { 0, 30, 15, 72, 31, 88, 56, 0 }, - { 0, 30, 15, 39, 31, 47, 55, 0 }, { 1, 18, 9, 73, 19, 89, 57, 121 }, - { 2, 20, 10, 74, 21, 90, 58, 122 }, { 2, 22, 11, 75, 23, 91, 59, 123 }, - { 4, 24, 12, 76, 25, 44, 60, 124 }, { 5, 26, 13, 77, 27, 93, 61, 125 }, - { 6, 28, 14, 78, 29, 94, 62, 126 }, { 0, 30, 15, 79, 31, 95, 63, 127 }, - }; - - if (framecolor < 16 && framecolor >= 8) - adj_framecolor = framecolor - 8; - return ((nhcolor < (hicolor ? 16 : 8) && adj_framecolor < 8) - ? framecolors[nhcolor][adj_framecolor] - : nhcolor); + /* curses_toggle_color_attr() adds the +1 and takes care of COLORS < 16 */ + return (16 * (framecolor % 8)) + (nhcolor % 16); } static void