|
| 1 | +'use client' |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | + |
| 5 | +const COLORS = ['#0A0310', '#49007E', '#FF005B', '#FF7D10', '#FFB238'] |
| 6 | + |
| 7 | +function hashStr(str: string): number { |
| 8 | + let hash = 0 |
| 9 | + for (let i = 0; i < str.length; i++) { |
| 10 | + hash = ((hash << 5) - hash + str.charCodeAt(i)) | 0 |
| 11 | + } |
| 12 | + return Math.abs(hash) |
| 13 | +} |
| 14 | + |
| 15 | +function getUnit(hash: number, range: number, index: number): number { |
| 16 | + const val = hash % (range * (index + 1)) |
| 17 | + return val % range |
| 18 | +} |
| 19 | + |
| 20 | +function getBoolean(hash: number, index: number): boolean { |
| 21 | + return getUnit(hash, 2, index) === 0 |
| 22 | +} |
| 23 | + |
| 24 | +function getRandomColor(hash: number, index: number, colors: string[]): string { |
| 25 | + return colors[getUnit(hash, colors.length, index)] |
| 26 | +} |
| 27 | + |
| 28 | +function getContrast(hex: string): string { |
| 29 | + const r = parseInt(hex.slice(1, 3), 16) |
| 30 | + const g = parseInt(hex.slice(3, 5), 16) |
| 31 | + const b = parseInt(hex.slice(5, 7), 16) |
| 32 | + return r * 0.299 + g * 0.587 + b * 0.114 > 128 ? '#000' : '#fff' |
| 33 | +} |
| 34 | + |
| 35 | +export interface BeamAvatarProps { |
| 36 | + name: string |
| 37 | + size?: number |
| 38 | + colors?: string[] |
| 39 | + className?: string |
| 40 | + square?: boolean |
| 41 | +} |
| 42 | + |
| 43 | +export function BeamAvatar({ |
| 44 | + name, |
| 45 | + size = 40, |
| 46 | + colors = COLORS, |
| 47 | + className, |
| 48 | + square = false, |
| 49 | +}: BeamAvatarProps) { |
| 50 | + const hash = hashStr(name) |
| 51 | + const wrapperColor = getRandomColor(hash, 0, colors) |
| 52 | + const faceColor = getContrast(wrapperColor) |
| 53 | + const isOpen = getBoolean(hash, 2) |
| 54 | + const mouthSpread = getUnit(hash, 3, 7) |
| 55 | + const eyeSpread = getUnit(hash, 5, 8) |
| 56 | + const faceRotate = getUnit(hash, 10, 9) |
| 57 | + const faceTranslateX = faceRotate > 6 ? faceRotate - 10 : faceRotate |
| 58 | + const faceTranslateY = getUnit(hash, 5, 10) > 3 ? getUnit(hash, 5, 10) - 5 : getUnit(hash, 5, 10) |
| 59 | + |
| 60 | + return ( |
| 61 | + <svg |
| 62 | + viewBox="0 0 36 36" |
| 63 | + fill="none" |
| 64 | + xmlns="http://www.w3.org/2000/svg" |
| 65 | + width={size} |
| 66 | + height={size} |
| 67 | + className={className} |
| 68 | + style={square ? undefined : { borderRadius: '50%' }} |
| 69 | + > |
| 70 | + <mask id={`beam-${hash}`} maskUnits="userSpaceOnUse" x={0} y={0} width={36} height={36}> |
| 71 | + <rect width={36} height={36} rx={square ? undefined : 72} fill="#fff" /> |
| 72 | + </mask> |
| 73 | + <g mask={`url(#beam-${hash})`}> |
| 74 | + <rect width={36} height={36} fill={wrapperColor} /> |
| 75 | + <rect |
| 76 | + x={0} |
| 77 | + y={0} |
| 78 | + width={36} |
| 79 | + height={36} |
| 80 | + transform={`translate(${faceTranslateX} ${faceTranslateY}) rotate(${faceRotate} 18 18)`} |
| 81 | + fill={getRandomColor(hash, 1, colors)} |
| 82 | + rx={6} |
| 83 | + /> |
| 84 | + <g |
| 85 | + transform={`translate(${faceTranslateX} ${faceTranslateY}) rotate(${faceRotate} 18 18)`} |
| 86 | + > |
| 87 | + {/* Mouth */} |
| 88 | + {isOpen ? ( |
| 89 | + <path |
| 90 | + d={`M15 ${19 + mouthSpread}c2 1 4 1 6 0`} |
| 91 | + stroke={faceColor} |
| 92 | + fill="none" |
| 93 | + strokeLinecap="round" |
| 94 | + /> |
| 95 | + ) : ( |
| 96 | + <path |
| 97 | + d={`M13 ${19 + mouthSpread}a1 .75 0 0 0 10 0`} |
| 98 | + fill={faceColor} |
| 99 | + /> |
| 100 | + )} |
| 101 | + {/* Eyes */} |
| 102 | + <rect |
| 103 | + x={14 - eyeSpread} |
| 104 | + y={14} |
| 105 | + width={1.5} |
| 106 | + height={2} |
| 107 | + rx={1} |
| 108 | + fill={faceColor} |
| 109 | + /> |
| 110 | + <rect |
| 111 | + x={20 + eyeSpread} |
| 112 | + y={14} |
| 113 | + width={1.5} |
| 114 | + height={2} |
| 115 | + rx={1} |
| 116 | + fill={faceColor} |
| 117 | + /> |
| 118 | + </g> |
| 119 | + </g> |
| 120 | + </svg> |
| 121 | + ) |
| 122 | +} |
0 commit comments