Currently, Q3 defines a color escape codes as a '^' followed by anything except '\0':
#define Q_IsColorString(p) ( p && *(p) == Q_COLOR_ESCAPE && *((p)+1) && *((p)+1) != Q_COLOR_ESCAPE )
I see no reason why ^a, ^b, etc. should be recongized as color escapes (It's not even consistent throughout platforms). Also, in a message ending with ^, after which a newline should have been outputted (like any server message, such as a chat), ^\n is a color code, and not a ^ and a new line (bug!). I think ^1 ^2 ^3 ^4 ^5 ^6 ^7 ^8 ^9 and ^0 should be the only color escape codes.
We also shouldn't check wether p is a valid pointer, because we usually guarantee it, check it and whatsoever, and if we do check p, and Q_IsColorString doesn't result in crash, then the code after it will.
So my recommended method is:
#define Q_IsColorString(p) ( *(p) == Q_COLOR_ESCAPE && isdigit( *((p)+1) ) )
Or even better, only use ^A to ^Z !!!
Allow new colors: all RGB color combinations of 0, 128 and 255, such as RGB(0,255,128), RGB(128,128,255), etc., which makes a total of 27 colors. The english alphabet has only 26 characters, leaving out the one annoying color: black. All other colors can be easily read on black background.
#define Q_IsColorString(p) ( *(p) == Q_COLOR_ESCAPE && isalpha( *((p)+1) ) )
void Index2Color( char index, vec4_t color ) {
int num, tmp;
int col[3];
color[3] = 1.0f;
if( !isalpha( index ) ) {
color[0] = 1.0f;
color[1] = 1.0f;
color[2] = 1.0f;
return;
}
num = ( tolower( index ) - 'a' ) % 26 + 1;
tmp = num / 3;
col[2] = num - tmp * 3;
num = tmp;
tmp = num / 3;
col[1] = num - tmp * 3;
num = tmp;
tmp = num / 3;
col[0] = num - tmp * 3;
color[0] = col[0] / 2.0f;
color[1] = col[1] / 2.0f;
color[2] = col[2] / 2.0f;
}
Invalid already? Slow down a bit! I will contact the OSP/CPMA developers and ask them about this. They may release a quick 1.3c version, or not.
But either way, using
#define Q_IsColorString(p) ( *(p) == Q_COLOR_ESCAPE && ( isdigit( *((p)+1) ) || isalpha( *((p)+1) ) ) // ^[0-9a-zA-Z]
will not cause any trouble, and will standardize a bit, while fixing stuff like the the bug where ^ characters are eating newlines.
How's that?
Or even better, only use ^A to ^Z !!! Allow new colors: all RGB color combinations of 0, 128 and 255, such as RGB(0,255,128), RGB(128,128,255), etc., which makes a total of 27 colors. The english alphabet has only 26 characters, leaving out the one annoying color: black. All other colors can be easily read on black background. #define Q_IsColorString(p) ( *(p) == Q_COLOR_ESCAPE && isalpha( *((p)+1) ) ) void Index2Color( char index, vec4_t color ) { int num, tmp; int col[3]; color[3] = 1.0f; if( !isalpha( index ) ) { color[0] = 1.0f; color[1] = 1.0f; color[2] = 1.0f; return; } num = ( tolower( index ) - 'a' ) % 26 + 1; tmp = num / 3; col[2] = num - tmp * 3; num = tmp; tmp = num / 3; col[1] = num - tmp * 3; num = tmp; tmp = num / 3; col[0] = num - tmp * 3; color[0] = col[0] / 2.0f; color[1] = col[1] / 2.0f; color[2] = col[2] / 2.0f; }Or fill up g_color_table[] with 26 colors and use ( tolower( index ) - 'a' ) % 26 as an index ;)Invalid already? Slow down a bit! I will contact the OSP/CPMA developers and ask them about this. They may release a quick 1.3c version, or not. But either way, using #define Q_IsColorString(p) ( *(p) == Q_COLOR_ESCAPE && ( isdigit( *((p)+1) ) || isalpha( *((p)+1) ) ) // ^[0-9a-zA-Z] will not cause any trouble, and will standardize a bit, while fixing stuff like the the bug where ^ characters are eating newlines. How's that?