diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 14:15:39 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-27 23:51:07 +0100 |
commit | e675e3f8158c47cab4c0f0a2456d775210dfa12b (patch) | |
tree | f36be6082ee903067d42073a37885edd6ff77f35 /static | |
parent | 699c9f8d17cec8c40b7182a9a34849168448cc06 (diff) | |
download | simplechat-e675e3f8158c47cab4c0f0a2456d775210dfa12b.tar.gz simplechat-e675e3f8158c47cab4c0f0a2456d775210dfa12b.tar.bz2 simplechat-e675e3f8158c47cab4c0f0a2456d775210dfa12b.zip |
Chat buddies now get a random color assigned.
Diffstat (limited to 'static')
-rw-r--r-- | static/chat.js | 16 |
1 files 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($("<li/>").text(nick)); + var col = randomColor(); + buddyColors[nick] = col; + console.log(col); + $("#buddies").append($("<li/>").css("color", col).text(nick)); } } @@ -42,7 +54,7 @@ function chatlogWriter(event) { break; } - var elemNick = $("<span/>").addClass("nick").text(data.user); + var elemNick = $("<span/>").addClass("nick").prop("style", "color: " + buddyColors[data.user]).text(data.user); var elemText = $("<span/>").addClass("msg").text(msgtext); var logentry = $("<li/>").addClass(data.type).append(elemNick).append(elemText); $("#chatlog").append(logentry); |