Skip to content

Commit

Permalink
feat(avatar): add custom avatar color by player name
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebkel committed Mar 24, 2024
1 parent 6de9ab9 commit f732e72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
29 changes: 12 additions & 17 deletions packages/unoenty/src/components/Avatar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import React from "react"
import React, { useCallback } from "react"
import { Container, Typography } from "@material-ui/core"
import { fade } from "@material-ui/core/styles/colorManipulator"

import { stringToColor } from "@/utils/color"
import Device from "@/utils/device"

import theme from "@/styles/theme"

import useStyles from "@/components/Avatar/styles"

type AvatarProps = {
Expand All @@ -17,15 +20,15 @@ const Avatar: React.FC<AvatarProps> = (props) => {

const classes = useStyles()

const getNameMainLetter = () => {
const getNameMainLetter = useCallback(() => {
const firstLetter = name[0]

const upperCasedFirstLetter = firstLetter.toUpperCase()

return upperCasedFirstLetter
}
}, [name])

const getFontSize = () => {
const getFontSize = useCallback(() => {
let fontSize: number | undefined

if (size === "large") {
Expand All @@ -37,20 +40,11 @@ const Avatar: React.FC<AvatarProps> = (props) => {
}

return fontSize
}

const getAvatarColor = () => {
const colors = [
"#E0CE2D",
"#35EA88",
"#35EAC9",
"#9E3EFF",
]
}, [size])

return colors[name.length % colors.length]
}
const getAvatarColor = useCallback(() => stringToColor(name), [name])

const getAvatarSize = () => {
const getAvatarSize = useCallback(() => {
let avatarSize: number | undefined

if (size === "large") {
Expand All @@ -62,7 +56,7 @@ const Avatar: React.FC<AvatarProps> = (props) => {
}

return avatarSize
}
}, [size])

return (
<Container
Expand All @@ -88,6 +82,7 @@ const Avatar: React.FC<AvatarProps> = (props) => {
className={classes.avatarTypography}
style={{
fontSize: getFontSize(),
color: theme.palette.getContrastText(getAvatarColor())
}}
>
{getNameMainLetter()}
Expand Down
2 changes: 1 addition & 1 deletion packages/unoenty/src/components/Divider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"

import theme from "../../styles/theme"
import theme from "@/styles/theme"

type DividerProps = {
size: number
Expand Down
19 changes: 19 additions & 0 deletions packages/unoenty/src/utils/color.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const stringToColor = (string) => {
let hash = 0
let i

/* eslint-disable no-bitwise */
for (i = 0; i < string.length; i += 1) {
hash = string.charCodeAt(i) + ((hash << 5) - hash)
}

let color = '#';

for (i = 0; i < 3; i += 1) {
const value = (hash >> (i * 8)) & 0xff;
color += `00${value.toString(16)}`.substr(-2)
}
/* eslint-enable no-bitwise */

return color
}

0 comments on commit f732e72

Please sign in to comment.