From e675e3f8158c47cab4c0f0a2456d775210dfa12b Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Thu, 27 Mar 2014 14:15:39 +0100 Subject: Chat buddies now get a random color assigned. --- static/chat.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/static/chat.js b/static/chat.js index d9c89fe..0b5d1c9 100644 --- a/static/chat.js +++ b/static/chat.js @@ -1,9 +1,18 @@ +function randomColor() { + var h = Math.floor(Math.random() * 360); + var s = Math.floor((Math.random() * 0.5 + 0.5)*100); + var l = Math.floor((Math.random() * 0.4 + 0.3)*100); + return "hsl("+h+", "+s+"%, "+l+"%)"; +} + function askTryAgain(reason, ws_url) { if(confirm("Could not join chat (Reason: "+ reason +"). Try again?")) { window.setTimeout(RunChat, 1, ws_url); // We use a timeout, so we don't accidentally fill up the call stack. } } +var buddyColors = {}; + function addBuddy(nick) { var found = false; $("#buddies li").each(function(index) { @@ -12,7 +21,10 @@ function addBuddy(nick) { } }); if(!found) { - $("#buddies").append($("
  • ").text(nick)); + var col = randomColor(); + buddyColors[nick] = col; + console.log(col); + $("#buddies").append($("
  • ").css("color", col).text(nick)); } } @@ -42,7 +54,7 @@ function chatlogWriter(event) { break; } - var elemNick = $("").addClass("nick").text(data.user); + var elemNick = $("").addClass("nick").prop("style", "color: " + buddyColors[data.user]).text(data.user); var elemText = $("").addClass("msg").text(msgtext); var logentry = $("
  • ").addClass(data.type).append(elemNick).append(elemText); $("#chatlog").append(logentry); -- cgit v1.2.3-70-g09d2