-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathnameAvatar.js
43 lines (38 loc) · 1.21 KB
/
nameAvatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function getRandomColor() {
// Function to generate a random color
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getContrastColor(backgroundColor) {
// Function to get a contrasting text color based on the background color
const hex = backgroundColor.slice(1);
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
// Use black or white text based on brightness
return brightness > 128 ? '#000000' : '#ffffff';
}
const NameAvatar = (props) => {
const initials = props.name.split(' ').map(word => word[0]).join('').toUpperCase();
const backgroundColor = getRandomColor();
const textColor = getContrastColor(backgroundColor);
return (
<div
className={`name-initial-avatar ${props.className}`}
style={{
width: props?.width,
height: props?.height,
fontSize: props.fontSize || 50,
color: textColor,
backgroundColor: backgroundColor
}}
>
{initials}
</div>)
};
export default NameAvatar;