From ed4309ce41557d39576108731631ac59aa6e3120 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?=
 <skjnldsv@protonmail.com>
Date: Mon, 7 May 2018 10:50:33 +0200
Subject: [PATCH] Unify colour algorithm output
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
---
 core/js/placeholder.js |  9 ++++++---
 lib/private/Avatar.php | 16 +++++++++++-----
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/core/js/placeholder.js b/core/js/placeholder.js
index a0dfe8491d4..81f0b12e61a 100644
--- a/core/js/placeholder.js
+++ b/core/js/placeholder.js
@@ -62,13 +62,16 @@
 (function ($) {
 
 	String.prototype.toRgb = function() {
-		var hash = this.toLowerCase().replace(/[^0-9a-f]+/g, '');
+		// Normalize hash		
+		var hash = this.toLowerCase();
 
 		// Already a md5 hash?
-		if( !hash.match(/^[0-9a-f]{32}$/g) ) {
+		if( hash.match(/^([0-9a-f]{4}-?){8}$/) === null ) {
 			hash = md5(hash);
 		}
 
+		hash = hash.replace(/[^0-9a-f]/g, '');
+
 		function Color(r,g,b) {
 			this.r = r;
 			this.g = g;
@@ -116,7 +119,7 @@
 			var result = Array();
 
 			// Splitting evenly the string
-			for (var i in hash) {
+			for (var i=0; i<hash.length; i++) {
 				// chars in md5 goes up to f, hex:16
 				result.push(parseInt(hash.charAt(i), 16) % 16);
 			}
diff --git a/lib/private/Avatar.php b/lib/private/Avatar.php
index 6858346f22b..0ea2b82f5ea 100644
--- a/lib/private/Avatar.php
+++ b/lib/private/Avatar.php
@@ -408,14 +408,20 @@ class Avatar implements IAvatar {
 	}
 
 	/**
-	 * @param string $text
+	 * @param string $hash
 	 * @return Color Object containting r g b int in the range [0, 255]
 	 */
-	public function avatarBackgroundColor(string $text) {
-		$hash = preg_replace('/[^0-9a-f]+/', '', $text);
+	public function avatarBackgroundColor(string $hash) {
+		// Normalize hash
+		$hash = strtolower($hash);
+		
+		// Already a md5 hash?
+		if( preg_match('/^([0-9a-f]{4}-?){8}$/', $hash, $matches) !== 1 ) {
+			$hash = md5($hash);
+		}
 
-		$hash = md5($hash);
-		$hashChars = str_split($hash);
+		// Remove unwanted char
+		$hash = preg_replace('/[^0-9a-f]+/', '', $hash);
 
 		$red = new Color(182, 70, 157);
 		$yellow = new Color(221, 203, 85);
-- 
GitLab