From 1cc8656368e229e9a63e8fdc8417451f2d955e49 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Mon, 10 Aug 2015 07:51:03 +0200 Subject: degesch: precompute the filtered color cube --- degesch.c | 56 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/degesch.c b/degesch.c index 889d989..35dcce0 100644 --- a/degesch.c +++ b/degesch.c @@ -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)) -- cgit v1.2.3-70-g09d2