feat(xtext): render \x04 hex color codes in chat text

This commit is contained in:
BareMetal
2026-10-11 23:54:08 -06:00
parent d614e25792
commit 39daca9d22
6 changed files with 245 additions and 2 deletions
+44
View File
@@ -301,6 +301,42 @@ strip_color (const char *text, int len, int flags)
return new_str;
}
static int
hexcolor_component (const char *s, int avail, guint32 *rgb)
{
int i;
guint32 value = 0;
if (avail < 6)
return FALSE;
for (i = 0; i < 6; i++)
{
if (!g_ascii_isxdigit (s[i]))
return FALSE;
value = (value << 4) | g_ascii_xdigit_value (s[i]);
}
if (rgb)
*rgb = value;
return TRUE;
}
// Parses the parameters of a \004 (hex color) code: RRGGBB[,RRGGBB].
int
hexcolor_parse (const char *s, int avail, guint32 *fg, guint32 *bg, int *has_bg)
{
if (has_bg)
*has_bg = FALSE;
if (!hexcolor_component (s, avail, fg))
return 0;
if (avail >= 7 && s[6] == ',' && hexcolor_component (s + 7, avail - 7, bg))
{
if (has_bg)
*has_bg = TRUE;
return 13;
}
return 6;
}
/* CL: strip_color2 strips src and writes the output at dst; pass the same pointer
in both arguments to strip in place. */
int
@@ -330,6 +366,14 @@ strip_color2 (const char *src, int len, char *dst, int flags)
if (!(flags & STRIP_COLOR)) goto pass_char;
rcol = 2;
break;
case '\004': /*ATTR_HEXCOLOR: */
if (!(flags & STRIP_COLOR)) goto pass_char;
{
int skip = hexcolor_parse (src + 1, len, NULL, NULL, NULL);
src += skip;
len -= skip;
}
break;
case HIDDEN_CHAR: /* CL: invisible text (for event formats only) */ /* this takes care of the topic */
if (!(flags & STRIP_HIDDEN)) goto pass_char;
break;