centralise unformat_24bit_color_alg

This commit is contained in:
Ailin Nemui 2026-01-02 17:34:30 +01:00
commit 6d7ca54553
3 changed files with 38 additions and 47 deletions

View file

@ -112,17 +112,15 @@ void format_ext_color(GString *out, int bg, int color)
if (bg && color < 0x10) if (bg && color < 0x10)
g_string_append_c(out, FORMAT_COLOR_NOCHANGE); g_string_append_c(out, FORMAT_COLOR_NOCHANGE);
if (color < 0x10) if (color < 0x10)
g_string_append_c(out, color+'0'); g_string_append_c(out, color + '0');
else { else {
if (color < 0x60) if (color >= 0xb0)
g_string_append_c(out, bg ? FORMAT_COLOR_EXT1_BG g_string_append_c(out, bg ? FORMAT_COLOR_EXT3_BG : FORMAT_COLOR_EXT3);
: FORMAT_COLOR_EXT1); else if (color >= 0x60)
else if (color < 0xb0)
g_string_append_c(out, bg ? FORMAT_COLOR_EXT2_BG g_string_append_c(out, bg ? FORMAT_COLOR_EXT2_BG
: FORMAT_COLOR_EXT2); : FORMAT_COLOR_EXT2);
else else
g_string_append_c(out, bg ? FORMAT_COLOR_EXT3_BG g_string_append_c(out, bg ? FORMAT_COLOR_EXT1_BG : FORMAT_COLOR_EXT1);
: FORMAT_COLOR_EXT3);
g_string_append_c(out, FORMAT_COLOR_NOCHANGE + ((color-0x10)%0x50)); g_string_append_c(out, FORMAT_COLOR_NOCHANGE + ((color-0x10)%0x50));
} }
@ -142,14 +140,13 @@ static void format_ext_color_unexpand(GString *out, gboolean bg, int base, char
g_string_append_c(out, ext_color_al[value % 36]); g_string_append_c(out, ext_color_al[value % 36]);
} }
void unformat_24bit_color(char **ptr, int off, int *fgcolor, int *bgcolor, int *flags) int unformat_24bit_color_alg(const char **ptr, int off, int *is_bg, unsigned int *color)
{ {
unsigned int color;
unsigned char rgbx[4]; unsigned char rgbx[4];
unsigned int i; unsigned int i;
for (i = 0; i < 4; ++i) { for (i = 0; i < 4; ++i) {
if ((*ptr)[i + off] == '\0') if ((*ptr)[i + off] == '\0')
return; return FALSE;
rgbx[i] = (*ptr)[i + off]; rgbx[i] = (*ptr)[i + off];
} }
rgbx[3] -= 0x20; rgbx[3] -= 0x20;
@ -158,12 +155,23 @@ void unformat_24bit_color(char **ptr, int off, int *fgcolor, int *bgcolor, int *
if (rgbx[3] & (0x10 << i)) if (rgbx[3] & (0x10 << i))
rgbx[i] -= 0x20; rgbx[i] -= 0x20;
} }
color = rgbx[0] << 16 | rgbx[1] << 8 | rgbx[2]; *color = rgbx[0] << 16 | rgbx[1] << 8 | rgbx[2];
if (rgbx[3] & 0x1) { *is_bg = rgbx[3] & 0x1;
return TRUE;
}
static void unformat_24bit_color(const char **ptr, int off, int *fgcolor, int *bgcolor, int *flags)
{
unsigned int color;
int is_bg;
if (!unformat_24bit_color_alg(ptr, off, &is_bg, &color))
return;
if (is_bg) {
*bgcolor = color; *bgcolor = color;
*flags |= GUI_PRINT_FLAG_COLOR_24_BG; *flags |= GUI_PRINT_FLAG_COLOR_24_BG;
} } else {
else {
*fgcolor = color; *fgcolor = color;
*flags |= GUI_PRINT_FLAG_COLOR_24_FG; *flags |= GUI_PRINT_FLAG_COLOR_24_FG;
} }
@ -172,22 +180,13 @@ void unformat_24bit_color(char **ptr, int off, int *fgcolor, int *bgcolor, int *
static void format_24bit_color_unexpand(GString *out, int off, const char **ptr) static void format_24bit_color_unexpand(GString *out, int off, const char **ptr)
{ {
unsigned int color; unsigned int color;
unsigned char rgbx[4]; int is_bg;
unsigned int i;
for (i = 0; i < 4; ++i) { if (!unformat_24bit_color_alg(ptr, off, &is_bg, &color))
if ((*ptr)[i + off] == '\0') return;
return;
rgbx[i] = (*ptr)[i + off];
}
rgbx[3] -= 0x20;
*ptr += 4;
g_string_append_c(out, '%'); g_string_append_c(out, '%');
for (i = 0; i < 3; ++i) { g_string_append_c(out, is_bg ? 'z' : 'Z');
if (rgbx[3] & (0x10 << i))
rgbx[i] -= 0x20;
}
color = rgbx[0] << 16 | rgbx[1] << 8 | rgbx[2];
g_string_append_c(out, rgbx[3] & 0x1 ? 'z' : 'Z');
g_string_append_printf(out, "%06X", color); g_string_append_printf(out, "%06X", color);
} }
@ -1518,7 +1517,8 @@ void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC
flags &= ~GUI_PRINT_FLAG_COLOR_24_BG; flags &= ~GUI_PRINT_FLAG_COLOR_24_BG;
break; break;
case FORMAT_COLOR_24: case FORMAT_COLOR_24:
unformat_24bit_color(&ptr, 1, &fgcolor, &bgcolor, &flags); unformat_24bit_color((const char **) &ptr, 1, &fgcolor, &bgcolor,
&flags);
break; break;
default: default:
if (*ptr != FORMAT_COLOR_NOCHANGE) { if (*ptr != FORMAT_COLOR_NOCHANGE) {

View file

@ -173,6 +173,7 @@ void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC
int format_expand_styles(GString *out, const char **format, int *flags); int format_expand_styles(GString *out, const char **format, int *flags);
void format_ext_color(GString *out, int bg, int color); void format_ext_color(GString *out, int bg, int color);
void format_24bit_color(GString *out, int bg, unsigned int color); void format_24bit_color(GString *out, int bg, unsigned int color);
int unformat_24bit_color_alg(const char **ptr, int off, int *is_bg, unsigned int *color);
void format_gui_flags(GString *out, int *last_fg, int *last_bg, int *last_flags, int fg, int bg, void format_gui_flags(GString *out, int *last_fg, int *last_bg, int *last_flags, int fg, int bg,
int flags); int flags);

View file

@ -121,25 +121,15 @@ static void textbuffer_cache_unref(TEXT_BUFFER_CACHE_REC *cache)
static void unformat_24bit_line_color(const unsigned char **ptr, int off, int *flags, unsigned int *fg, unsigned int *bg) static void unformat_24bit_line_color(const unsigned char **ptr, int off, int *flags, unsigned int *fg, unsigned int *bg)
{ {
unsigned int color; unsigned int color;
unsigned char rgbx[4]; int is_bg;
unsigned int i;
for (i = 0; i < 4; ++i) { if (!unformat_24bit_color_alg((const char **) ptr, off, &is_bg, &color))
if ((*ptr)[i + off] == '\0') return;
return;
rgbx[i] = (*ptr)[i + off]; if (is_bg) {
}
rgbx[3] -= 0x20;
*ptr += 4;
for (i = 0; i < 3; ++i) {
if (rgbx[3] & (0x10 << i))
rgbx[i] -= 0x20;
}
color = rgbx[0] << 16 | rgbx[1] << 8 | rgbx[2];
if (rgbx[3] & 0x1) {
*flags = (*flags & FGATTR) | ATTR_BGCOLOR24; *flags = (*flags & FGATTR) | ATTR_BGCOLOR24;
*bg = color; *bg = color;
} } else {
else {
*flags = (*flags & BGATTR) | ATTR_FGCOLOR24; *flags = (*flags & BGATTR) | ATTR_FGCOLOR24;
*fg = color; *fg = color;
} }