degesch: precompute the filtered color cube

This commit is contained in:
Přemysl Eric Janouch 2015-08-10 07:51:03 +02:00
parent 4c81112840
commit 1cc8656368
1 changed files with 31 additions and 25 deletions

View File

@ -1363,6 +1363,9 @@ struct app_context
struct input input; ///< User interface
int *nick_palette; ///< A 256-color palette for nicknames
size_t nick_palette_len; ///< Number of entries in nick_palette
bool awaiting_mirc_escape; ///< Awaiting a mIRC attribute escape
char char_buf[MB_LEN_MAX + 1]; ///< Buffered multibyte char
size_t char_buf_len; ///< How much of an MB char is buffered
@ -1371,6 +1374,28 @@ struct app_context
}
*g_ctx;
static int *
filter_color_cube_for_acceptable_nick_colors (size_t *len)
{
// This is a pure function and we don't use threads, static storage is fine
static int table[6 * 6 * 6];
size_t len_counter = 0;
for (int x = 0; x < 6 * 6 * 6; x++)
{
int r = x / 36;
int g = (x / 6) % 6;
int b = (x % 6);
// Use the luma value of colours within the cube to filter colours that
// look okay-ish on terminals with both black and white backgrounds
double luma = 0.2126 * r / 6. + 0.7152 * g / 6. + 0.0722 * b / 6.;
if (luma >= .3 && luma <= .5)
table[len_counter++] = 16 + x;
}
*len = len_counter;
return table;
}
static void
app_context_init (struct app_context *self)
{
@ -1407,6 +1432,9 @@ app_context_init (struct app_context *self)
free (encoding);
input_init (&self->input);
self->nick_palette =
filter_color_cube_for_acceptable_nick_colors (&self->nick_palette_len);
}
static void
@ -2467,27 +2495,6 @@ formatter_parse_mirc (struct formatter *self, const char *s)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static int *
filter_color_cube_for_acceptable_nick_colors (size_t *len)
{
static int table[6 * 6 * 6];
size_t len_counter = 0;
for (int x = 0; x < 6 * 6 * 6; x++)
{
int r = x / 36;
int g = (x / 6) % 6;
int b = (x % 6);
// Use the luma value of colours within the cube to filter colours that
// look okay-ish on terminals with both black and white backgrounds
double luma = 0.2126 * r / 6. + 0.7152 * g / 6. + 0.0722 * b / 6.;
if (luma >= .3 && luma <= .5)
table[len_counter++] = 16 + x;
}
*len = len_counter;
return table;
}
static void
formatter_parse_nick (struct formatter *self, char *s)
{
@ -2499,10 +2506,9 @@ formatter_parse_nick (struct formatter *self, char *s)
if (color == COLOR_BLACK)
color = -1;
size_t len;
// TODO: precompute this table
int *colors = filter_color_cube_for_acceptable_nick_colors (&len);
color |= colors[siphash_wrapper (nick, strlen (nick)) % len] << 16;
// Use a color from the 256-color cube if available
color |= self->ctx->nick_palette[siphash_wrapper (nick,
strlen (nick)) % self->ctx->nick_palette_len] << 16;
// We always use the default color for ourselves
if (self->s && irc_is_this_us (self->s, nick))