From 143eaaf45bf65a970bc2183d5721b08f77fbcbb4 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 17 Feb 2021 00:17:13 +0100 Subject: Matrix: use same user id colors as element --- src/plugins/Matrix.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index 8865fd9..c725fc8 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -78,18 +78,29 @@ namespace QuickMedia { } } + static int color_hash_code(const std::string &str) { + int hash = 0; + if(str.empty()) + return hash; + for (char chr : str) { + hash = ((hash << 5) - hash) + (unsigned char)chr; + } + return std::abs(hash); + } + static sf::Color user_id_to_color(const std::string &user_id) { - uint32_t color = 2166136261; - for(unsigned char c : user_id) { - color = (color * 16777619) ^ c; - } - sf::Uint8 *col = (sf::Uint8*)&color; - sf::Color result(col[0], col[1], col[2]); - result.r = std::min(255, 80 + (int)result.r); - result.g = std::min(255, 80 + (int)result.g); - result.b = std::min(255, 80 + (int)result.b); - result.a = 255; - return result; + const int num_colors = 8; + const sf::Color colors[num_colors] = { + sf::Color(54, 139, 214), + sf::Color(172, 59, 168), + sf::Color(3, 179, 129), + sf::Color(230, 79, 122), + sf::Color(255, 129, 45), + sf::Color(45, 194, 197), + sf::Color(92, 86, 245), + sf::Color(116, 209, 44) + }; + return colors[color_hash_code(user_id) % num_colors]; } UserInfo::UserInfo(RoomData *room, std::string user_id) : -- cgit v1.2.3