alt_color_spec follow-up

This commit is contained in:
nhmall
2024-04-13 12:00:37 -04:00
parent 936096d5e3
commit c30dbb2caa

View File

@@ -5,8 +5,6 @@
#include "hack.h" #include "hack.h"
#include <ctype.h> #include <ctype.h>
staticfn int32 alt_color_spec(const char *cp);
struct color_names { struct color_names {
const char *name; const char *name;
int color; int color;
@@ -221,6 +219,10 @@ static struct nethack_color colortable[] = {
{ rgb_color, 154, 138, "white", "#FFFFFF", 255, 255, 255 }, { rgb_color, 154, 138, "white", "#FFFFFF", 255, 255, 255 },
}; };
#ifdef CHANGE_COLOR
staticfn int32 alt_color_spec(const char *cp);
#endif
int32 int32
colortable_to_int32(struct nethack_color *cte) colortable_to_int32(struct nethack_color *cte)
{ {
@@ -1069,10 +1071,9 @@ alternative_palette(char *op)
coloridx = match_str2clr(c_colorid, TRUE); coloridx = match_str2clr(c_colorid, TRUE);
if (c_colorval && coloridx >= 0 && coloridx < CLR_MAX) { if (c_colorval && coloridx >= 0 && coloridx < CLR_MAX) {
if (*c_colorval == '#' || *c_colorval == '\\') { rgb = rgbstr_to_int32(c_colorval);
if (rgb == -1) {
rgb = alt_color_spec(c_colorval); rgb = alt_color_spec(c_colorval);
} else {
rgb = rgbstr_to_int32(c_colorval);
} }
if (rgb != -1) { if (rgb != -1) {
ga.altpalette[coloridx] = (uint32) rgb | NH_ALTPALETTE; ga.altpalette[coloridx] = (uint32) rgb | NH_ALTPALETTE;
@@ -1098,39 +1099,51 @@ change_palette(void)
} }
staticfn int32 staticfn int32
alt_color_spec(const char *cp) alt_color_spec(const char *str)
{ {
static NEARDATA const char oct[] = "01234567", dec[] = "0123456789"; static NEARDATA const char oct[] = "01234567", dec[] = "0123456789";
/* hexdd[] is defined in decl.c */ /* hexdd[] is defined in decl.c */
const char *dp; const char *dp, *cp = str;
int32 cval = -1; int32 cval = -1;
int dcount; int dcount;
boolean hexescape = FALSE, octescape = FALSE;
cval = dcount = 0; /* for decimal, octal, hexadecimal cases */ dcount = 0; /* for decimal, octal, hexadecimal cases */
while (*cp) { hexescape =
if (((*cp != '\\') && (*cp != '#')) || !cp[1]) { (*cp == '\\' && cp[1] && (cp[1] == 'x' || cp[1] == 'X') && cp[2]);
if (!hexescape) {
octescape =
(*cp == '\\' && cp[1] && (cp[1] == 'o' || cp[1] == 'O') && cp[2]);
}
if (hexescape || octescape) {
cval = 0;
cp += 2;
} else if (*cp == '#' && cp[1]) {
hexescape = TRUE;
cval = 0;
cp += 1;
} else if (cp[1]) {
cval = 0;
} else if (!cp[1]) {
if (strchr(dec, *cp) != 0) {
/* simple val, or nothing left for \ to escape */ /* simple val, or nothing left for \ to escape */
cval = *cp++; cval = (*cp - '0');
} else if (*cp != '#' && strchr(dec, cp[1])) { }
++cp; /* move past backslash to first digit */ cp++;
}
while (*cp) {
if (!hexescape && !octescape && strchr(dec, *cp)) {
do { do {
cval = (cval * 10) + (*cp - '0'); cval = (cval * 10) + (*cp - '0');
} while (*++cp && strchr(dec, *cp) && ++dcount < 3); } while (*++cp && strchr(dec, *cp) && ++dcount < 8);
} else if (*cp != '#' && (cp[1] == 'o' || cp[1] == 'O') && cp[2] } else if (octescape && strchr(oct, *cp)) {
&& strchr(oct, cp[2])) {
cp += 2; /* move past backslash and 'O' */
do { do {
cval = (cval * 8) + (*cp - '0'); cval = (cval * 8) + (*cp - '0');
} while (*++cp && strchr(oct, *cp) && ++dcount < 8); } while (*++cp && strchr(oct, *cp) && ++dcount < 8);
} else if (((cp[1] == 'x' || cp[1] == 'X') && cp[2] } else if (hexescape && (dp = strchr(hexdd, *cp)) != 0) {
&& (dp = strchr(hexdd, cp[2])) != 0)
|| (cp[0] == '#' && cp[1]
&& (dp = strchr(hexdd, cp[1])) != 0)) {
if (*cp == '\\')
cp += 2; /* move past backslash and 'X' */
else if (*cp == '#')
cp += 1; /* move past '#' */
do { do {
cval = (cval * 16) + ((int) (dp - hexdd) / 2); cval = (cval * 16) + ((int) (dp - hexdd) / 2);
} while (*++cp && (dp = strchr(hexdd, *cp)) != 0 && ++dcount < 6); } while (*++cp && (dp = strchr(hexdd, *cp)) != 0 && ++dcount < 6);