From e64d3c620fdef0c15259cf955466cb040d02e958 Mon Sep 17 00:00:00 2001 From: brunosimon Date: Tue, 5 Sep 2017 00:19:22 +0200 Subject: [PATCH] :lipstick: Chat > Improve colors --- lib/chat.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/lib/chat.js b/lib/chat.js index 831a881..babe7cf 100644 --- a/lib/chat.js +++ b/lib/chat.js @@ -153,14 +153,68 @@ class Chat getRandomColor() { - const r = (Math.round(Math.random() * 127) + 127).toString(16) - const g = (Math.round(Math.random() * 127) + 127).toString(16) - const b = (Math.round(Math.random() * 127) + 127).toString(16) - const color = '#' + r + g + b + const rgb = this.hslToRgb(Math.random(), 0.5, 0.5) + + rgb[0] = Math.round(rgb[0] * 0.7 + 255 * 0.3).toString(16) + rgb[1] = Math.round(rgb[1] * 0.7 + 255 * 0.3).toString(16) + rgb[2] = Math.round(rgb[2] * 0.7 + 255 * 0.3).toString(16) + + const color = `#${rgb[0]}${rgb[1]}${rgb[2]}` return color } + hslToRgb(h, s, l) + { + let r + let g + let b + + if(s === 0) + { + r = g = b = l + } + else + { + const hue2rgb = function hue2rgb(_p, _q, _t) + { + let t = _t + + if(t < 0) + { + t += 1 + } + if(t > 1) + { + t -= 1 + } + if(t < 1 / 6) + { + return _p + (_q - _p) * 6 * t + } + if(t < 1 / 2) + { + return _q + } + if(t < 2 / 3) + { + return _p + (_q - _p) * (2 / 3 - t) * 6 + } + + return _p + } + + const q = l < 0.5 ? l * (1 + s) : l + s - l * s + const p = 2 * l - q + + r = hue2rgb(p, q, h + 1 / 3) + g = hue2rgb(p, q, h) + b = hue2rgb(p, q, h - 1 / 3) + } + + return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)] + } + getRandomName() { // Refill robot names if no more name available