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

This commit is contained in:
Benoit Marty 2023-09-05 12:05:42 +02:00
parent b241b2c57a
commit 9bf3521f39
4 changed files with 120 additions and 1 deletions

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.designsystem.colors
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
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(
val background: Color,
val foreground: Color,
)
@Composable
fun AvatarColor(userId: String): AvatarColor {
val hash = userId.toHash()
val colors = if (ElementTheme.isLightTheme) {
avatarColorsLight[hash % avatarColorsLight.size]
} else {
avatarColorsDark[hash % avatarColorsDark.size]
}
return AvatarColor(
background = colors.first,
foreground = colors.second,
)
}
private fun String.toHash(): Int {
return toList().sumOf { it.code } % 8 + 1
}