Compute avatar color of users and apply foreground color to the sender displayname.

This commit is contained in:
Benoit Marty 2023-09-05 12:12:59 +02:00
parent 9db45a997d
commit a79e3d41d6
3 changed files with 50 additions and 10 deletions

View file

@ -22,26 +22,26 @@ import io.element.android.libraries.theme.ElementTheme
import io.element.android.libraries.theme.colors.avatarColorsDark
import io.element.android.libraries.theme.colors.avatarColorsLight
data class AvatarColor(
data class AvatarColors(
val background: Color,
val foreground: Color,
)
@Composable
fun AvatarColor(userId: String): AvatarColor {
fun AvatarColors(userId: String): AvatarColors {
val hash = userId.toHash()
val colors = if (ElementTheme.isLightTheme) {
avatarColorsLight[hash % avatarColorsLight.size]
avatarColorsLight[hash]
} else {
avatarColorsDark[hash % avatarColorsDark.size]
avatarColorsDark[hash]
}
return AvatarColor(
return AvatarColors(
background = colors.first,
foreground = colors.second,
)
}
private fun String.toHash(): Int {
return toList().sumOf { it.code } % 8 + 1
internal fun String.toHash(): Int {
return toList().sumOf { it.code } % 8
}