Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HSLUV based quasi-random hue color cycling for role colors #3

Merged
merged 1 commit into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(() => {
Expand Down
15 changes: 15 additions & 0 deletions lib/colorGenerator.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 1 addition & 8 deletions lib/teams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down