From 678f6e3c08534e2b859b11c502f5ba986bdd2436 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 23 Mar 2014 02:48:40 +0100 Subject: [PATCH] Unique color per nick --- client/css/style.css | 10 ++++++--- client/index.html | 12 +++++------ client/js/chat.js | 51 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/client/css/style.css b/client/css/style.css index a72a0b54..f67c9d68 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -1,6 +1,6 @@ html, body { - font: 12px Consolas, monospace; + font: 13px "Consolas", monospace; height: 100%; } a:focus { @@ -164,6 +164,7 @@ h2 { background: #fff; border-left: 1px solid #ddd; bottom: 30px; + line-height: 1.4em; overflow-y: auto; padding: 4px 0; position: absolute; @@ -173,20 +174,23 @@ h2 { } #chat .users .user { display: block; - color: #f00; padding: 0 8px; } #chat .messages { bottom: 30px; left: 0; overflow-y: auto; - padding: 4px 8px; + padding: 4px 0; position: absolute; right: 160px; top: 43px; word-wrap: break-word; z-index: 0; } +#chat .message { + line-height: 1.4em; + padding: 0 6px; +} #chat .message .time { color: #bbb; } diff --git a/client/index.html b/client/index.html index 27276bbf..218daa2a 100644 --- a/client/index.html +++ b/client/index.html @@ -90,7 +90,7 @@
{{#each users}} - + {{mode}}{{name}} {{/each}} @@ -99,8 +99,8 @@ {{#each messages}}
{{time}} - {{mode}}{{user}} - {{type}} {{{autoLink text}}} + {{mode}}{{user}} + {{type}} {{{link text}}}
{{/each}}
@@ -114,7 +114,7 @@ diff --git a/client/js/chat.js b/client/js/chat.js index 4b12553e..f409d867 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -215,9 +215,58 @@ $(function() { }; }); -Handlebars.registerHelper("autoLink", function(text) { +Handlebars.registerHelper("link", function(text) { var text = Handlebars.Utils.escapeExpression(text); return URI.withinString(text, function(url) { return "" + url + ""; }); }); + +Handlebars.registerHelper("color", function(text) { + return get_color(text); +}); + +// colornicks +// https://github.com/avidal + +function clean_nick(nick) { + // attempts to clean up a nickname + // by removing alternate characters from the end + // nc_ becomes nc, avidal` becomes avidal + + nick = nick.toLowerCase(); + + // typically ` and _ are used on the end alone + nick = nick.replace(/[`_]+$/, ''); + + // remove | from the end + nick = nick.replace(/|.*$/, ''); + + return nick; +} + +function hash(nick) { + var cleaned = clean_nick(nick); + var h = 0; + + for(var i = 0; i < cleaned.length; i++) { + h = cleaned.charCodeAt(i) + (h << 6) + (h << 16) - h; + } + + return h; +} + +function get_color(nick) { + var nickhash = hash(nick); + + // get a random value for the hue + var h = nickhash % 360; + + var l = 50; + var s = 100; + + // playing around with some numbers + h = 360 + (h % 40); + + return "hsl(" + h + "," + s + "%," + l + "%)"; +}