Skip to content

Commit

Permalink
Use mumurhash for identicon colors (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 authored Jul 24, 2024
1 parent d6ce325 commit 5cac34f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 53 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
3 changes: 2 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"cmdk": "0.2.0",
"djb2a": "^2.0.0",
"framer-motion": "^11.2.4",
"humanize-duration": "^3.32.0",
"lucide-react": "^0.378.0",
"murmurhash3js": "^3.0.1",
"react-dom": "^18.3.1",
"react-loader-spinner": "^6.1.6",
"react-router-dom": "^6.23.1",
Expand All @@ -85,6 +85,7 @@
"@storybook/react-vite": "8.1.1",
"@storybook/theming": "^8.1.11",
"@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
68 changes: 20 additions & 48 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 5cac34f

Please sign in to comment.