feat(post): add user ID with color specific to user address

This commit is contained in:
Tom (plebeius.eth)
2024-08-13 18:27:56 +02:00
parent 25722d74a3
commit 8dfe2098ce
4 changed files with 45 additions and 6 deletions
+18
View File
@@ -0,0 +1,18 @@
export function hashStringToColor(str: string): string {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
const r = (hash >> 24) & 0xff;
const g = (hash >> 16) & 0xff;
const b = (hash >> 8) & 0xff;
return `rgb(${r}, ${g}, ${b})`;
}
export function getTextColorForBackground(rgb: string): string {
const [r, g, b] = rgb.match(/\d+/g)?.map(Number) || [0, 0, 0];
const brightness = r * 0.299 + g * 0.587 + b * 0.114;
return brightness > 125 ? 'black' : 'white';
}