Skip to content

Commit

Permalink
Replace identicon hash algo (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 authored Jul 24, 2024
1 parent b1094ba commit fb50cd1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/ui/components/ui/identicon/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Inspired by: https://github.com/vercel/avatar

import djb2a from 'djb2a';
import color from 'tinycolor2';
import Murmur from 'murmurhash3js';

// Deterministically getting a gradient from a string for use as an identicon
export const generateGradient = (str: string) => {
// Get first color
const hash = djb2a(str);
const hash = Murmur.x86.hash32(str);
const c = color({ h: hash % 360, s: 0.95, l: 0.5 });

const tetrad = c.tetrad(); // 4 colors spaced around the color wheel, the first being the input
Expand All @@ -22,11 +22,10 @@ export const generateGradient = (str: string) => {

export const generateSolidColor = (str: string) => {
// Get color
const hash = djb2a(str);
const hash = Murmur.x86.hash32(str);
const c = color({ h: hash % 360, s: 0.95, l: 0.5 })
.saturate(0)
.darken(20);

return {
bg: c.toHexString(),
// get readable text color
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"framer-motion": "^11.2.4",
"humanize-duration": "^3.32.0",
"lucide-react": "^0.378.0",
"murmurhash3js": "^3.0.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-loader-spinner": "^6.1.6",
Expand All @@ -83,6 +84,7 @@
"@testing-library/dom": "^10.1.0",
"@testing-library/react": "^15.0.7",
"@types/humanize-duration": "^3.27.4",
"@types/murmurhash3js": "^3.0.7",
"@types/react": "^18.3.2",
"@types/react-dom": "^18.3.0",
"@types/tinycolor2": "^1.4.6",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit fb50cd1

Please sign in to comment.