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

Fix gradient generation to reduce hue collisions for longer usernames #53

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions utils/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ export function djb2(str: string) {
}

export function generateGradient(username: string) {
const c1 = color({ h: djb2(username) % 360, s: 0.95, l: 0.5 });
const second = c1.triad()[1].toHexString();
const hue = djb2(username) % 360;

let hash2 = 7;
for (let i = 0; i < username.length; i++) {
hash2 = hash2 * 31 + username.charCodeAt(i);
}
const hue2 = Math.abs(hash2) % 360;

const c1 = color({ h: hue, s: 0.95, l: 0.5 });
const c2 = color({ h: hue2, s: 0.95, l: 0.5 });
AntoineKM marked this conversation as resolved.
Show resolved Hide resolved

return {
fromColor: c1.toHexString(),
toColor: second,
toColor: c2.toHexString(),
};
}