From 9b499e6520e2603d09a5e8d5bd125d2ea2232c7b Mon Sep 17 00:00:00 2001 From: Albert Zhang Date: Fri, 9 Oct 2020 12:48:02 -0400 Subject: [PATCH] Implement HSLUV based quasi-random hue color cycling for role colors --- app.js | 2 ++ lib/colorGenerator.js | 15 +++++++++++++++ lib/teams.js | 9 +-------- package-lock.json | 5 +++++ package.json | 1 + 5 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 lib/colorGenerator.js diff --git a/app.js b/app.js index 82b3ee8..ea1ce71 100644 --- a/app.js +++ b/app.js @@ -11,6 +11,7 @@ import { registerMany, register } from "./lib/discordjs-ext/register.js"; import serverTemplate from "./serverTemplate.js"; import createPartialMessage from "./lib/discordjs-ext/createPartialMessage.js"; +import colorGenerator from "./lib/colorGenerator.js"; const client = new Client(); @@ -29,6 +30,7 @@ client.on("ready", async function onReady() { Promise.all( client.guilds.cache.map(guild => { console.log(`${guild.name} ${guild.owner.user.tag}`); + guild.colorGenerator = colorGenerator(); return guild.roles.fetch(); }) ).then(() => { diff --git a/lib/colorGenerator.js b/lib/colorGenerator.js new file mode 100644 index 0000000..9f8fa89 --- /dev/null +++ b/lib/colorGenerator.js @@ -0,0 +1,15 @@ +import hsluv from "hsluv"; + +// Golden Ratio +// See rationale here: http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/ +const phi = 1.6180339887498948482; +const step = 1.0 / phi; + +export default function* colorGenerator(seed) { + let hue = seed || Math.random(); + + while (true) { + yield hsluv.hsluvToHex([hue * 360, 100, 65]); + hue = (hue + step) % 1; + } +} diff --git a/lib/teams.js b/lib/teams.js index 76fd2ac..009124d 100644 --- a/lib/teams.js +++ b/lib/teams.js @@ -2,14 +2,7 @@ import { createRoom, addToRoom, deleteRoom } from "./rooms.js"; // eslint-disable-next-line import/prefer-default-export export async function createTeam(guild, name) { - let color; - do { - color = [ - Math.floor(Math.random() * 256), - Math.floor(Math.random() * 256), - Math.floor(Math.random() * 256), - ]; - } while (color[0] + color[1] + color[2] < 128 || color[0] + color[1] + color[2] > 576); + const color = guild.colorGenerator.next().value; const teamRole = await guild.roles.create({ data: { diff --git a/package-lock.json b/package-lock.json index fa63a0c..cdd4c8b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -806,6 +806,11 @@ "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", "dev": true }, + "hsluv": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/hsluv/-/hsluv-0.1.0.tgz", + "integrity": "sha512-ERcanKLAszD2XN3Vh5r5Szkrv9q0oSTudmP0rkiKAGM/3NMc9FLmMZBB7TSqTaXJfSDBOreYTfjezCOYbRKqlw==" + }, "ignore": { "version": "4.0.6", "resolved": "https://registry.npmjs.org/ignore/-/ignore-4.0.6.tgz", diff --git a/package.json b/package.json index 02687a7..ab68037 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "dependencies": { "discord-command-parser": "^1.4.3", "discord.js": "^12.3.1", + "hsluv": "^0.1.0", "parse-url": "^5.0.1" }, "devDependencies": {