mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-31 12:14:46 -04:00
Generate avatar color based on placeholder
This commit is contained in:
parent
2652e558d2
commit
eb92eed532
@ -27,6 +27,22 @@ import { YoshikiStyle } from "yoshiki/dist/type";
|
|||||||
import { P } from "@expo/html-elements";
|
import { P } from "@expo/html-elements";
|
||||||
import { forwardRef } from "react";
|
import { forwardRef } from "react";
|
||||||
|
|
||||||
|
const stringToColor = (string: string) => {
|
||||||
|
let hash = 0;
|
||||||
|
let i;
|
||||||
|
|
||||||
|
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)}`.slice(-2);
|
||||||
|
}
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
export const Avatar = forwardRef<
|
export const Avatar = forwardRef<
|
||||||
View,
|
View,
|
||||||
{
|
{
|
||||||
@ -58,7 +74,7 @@ export const Avatar = forwardRef<
|
|||||||
placeholder &&
|
placeholder &&
|
||||||
!src &&
|
!src &&
|
||||||
!isLoading && {
|
!isLoading && {
|
||||||
bg: theme.accent,
|
bg: stringToColor(placeholder),
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user