Create curses colour pairs for 256 colours

Keep only 8 background colours but if curses supports 256 colours and
256*8 colours pairs, create colours pairs for 256 foregrounds rather
than just 16.
This commit is contained in:
Doktor L
2026-06-19 16:36:27 +02:00
committed by Patric Mueller
parent 7036f902d2
commit 19c87585fa
4 changed files with 22 additions and 7 deletions
+7 -5
View File
@@ -7,6 +7,7 @@
#include "hack.h" #include "hack.h"
#include "wincurs.h" #include "wincurs.h"
#include "cursinit.h" #include "cursinit.h"
#include "cursmisc.h"
/* Initialization and startup functions for curses interface */ /* Initialization and startup functions for curses interface */
@@ -334,18 +335,18 @@ curses_init_nhcolors(void)
/* COLOR_foo + 8 means COLOR | A_BOLD when COLORS < 16 */ /* COLOR_foo + 8 means COLOR | A_BOLD when COLORS < 16 */
/* otherwise assume the terminal has least 16 different colors */ /* otherwise assume the terminal has least 16 different colors */
/* these map to the NetHack CLR_ defines */ /* these map to the NetHack CLR_ defines */
static const int fg_clr[16] = { static const int basic_fg_clr[16] = {
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE,
-1, COLOR_RED + 8, COLOR_GREEN + 8, COLOR_YELLOW + 8, -1, COLOR_RED + 8, COLOR_GREEN + 8, COLOR_YELLOW + 8,
COLOR_BLUE + 8, COLOR_MAGENTA + 8, COLOR_CYAN + 8, COLOR_WHITE + 8 COLOR_BLUE + 8, COLOR_MAGENTA + 8, COLOR_CYAN + 8, COLOR_WHITE + 8
}; };
static const int bg_clr[8] = { static const int bg_clr[CURSES_NUM_BACKGROUND_COLORS] = {
-1, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, -1, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
}; };
int bg, nhclr; int bg, nhclr;
int maxc = (COLORS >= 16) ? 16 : 8; int maxc = curses_has_256color() ? 256 : (COLORS >= 16) ? 16 : 8;
if (!has_colors()) if (!has_colors())
return; return;
@@ -353,8 +354,9 @@ curses_init_nhcolors(void)
use_default_colors(); use_default_colors();
for (nhclr = CLR_BLACK; nhclr < maxc; nhclr++) { for (nhclr = CLR_BLACK; nhclr < maxc; nhclr++) {
for (bg = 0; bg < 8; bg++) { for (bg = 0; bg < CURSES_NUM_BACKGROUND_COLORS; bg++) {
init_pair((maxc * bg) + nhclr + 1, fg_clr[nhclr], bg_clr[bg]); int fg_color = (nhclr < 16) ? basic_fg_clr[nhclr] : nhclr;
init_pair((maxc * bg) + nhclr + 1, fg_color, bg_clr[bg]);
} }
} }
+7 -1
View File
@@ -91,6 +91,12 @@ curses_read_char(void)
return ch; return ch;
} }
boolean
curses_has_256color(void)
{
return (COLORS >= 256) && (COLOR_PAIRS >= 256 * CURSES_NUM_BACKGROUND_COLORS);
}
/* Turn on or off the specified color and / or attribute */ /* Turn on or off the specified color and / or attribute */
void void
@@ -147,7 +153,7 @@ curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff)
if (use_bold) { if (use_bold) {
wattron(win, A_BOLD); wattron(win, A_BOLD);
} }
wattron(win, COLOR_PAIR(curses_color)); wcolor_set(win, curses_color, &curses_color);
} }
if (attr != NONE) { if (attr != NONE) {
+3
View File
@@ -8,8 +8,11 @@
/* Global declarations */ /* Global declarations */
#define CURSES_NUM_BACKGROUND_COLORS 8
int curses_getch(void); int curses_getch(void);
int curses_read_char(void); int curses_read_char(void);
boolean curses_has_256color(void);
void curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff); void curses_toggle_color_attr(WINDOW *win, int color, int attr, int onoff);
void curses_menu_color_attr(WINDOW *win, int color, int attr, int onoff); void curses_menu_color_attr(WINDOW *win, int color, int attr, int onoff);
void curses_bail(const char *mesg); void curses_bail(const char *mesg);
+5 -1
View File
@@ -769,7 +769,11 @@ static int
get_framecolor(int nhcolor, int framecolor) get_framecolor(int nhcolor, int framecolor)
{ {
/* curses_toggle_color_attr() adds the +1 and takes care of COLORS < 16 */ /* curses_toggle_color_attr() adds the +1 and takes care of COLORS < 16 */
return (16 * (framecolor % 8)) + (nhcolor % 16); if (curses_has_256color()) {
return (256 * (framecolor % 8)) + (nhcolor % 256);
} else
return (16 * (framecolor % 8)) + (nhcolor % 16);
} }
static void static void