diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-02-17 00:17:13 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-02-17 00:17:13 +0100 |
commit | 143eaaf45bf65a970bc2183d5721b08f77fbcbb4 (patch) | |
tree | fba7d5c97c6ea87cf61767d12bb0b9ab8e1542bb | |
parent | 12fcafcb346462adf99a8bd98895d0d4405bc257 (diff) |
Matrix: use same user id colors as element
-rw-r--r-- | src/plugins/Matrix.cpp | 33 |
1 files 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) : |