Skip to content

Instantly share code, notes, and snippets.

@ntoskrnl
Created October 17, 2021 00:54
Show Gist options
  • Save ntoskrnl/14622d88cad3387d14786aa259e4653f to your computer and use it in GitHub Desktop.
Save ntoskrnl/14622d88cad3387d14786aa259e4653f to your computer and use it in GitHub Desktop.
Circular avatar placeholder with initials in Jetpack Compose
import androidx.annotation.ColorInt
import androidx.core.graphics.ColorUtils
import kotlin.math.absoluteValue
@ColorInt
fun String.toHslColor(saturation: Float = 0.5f, lightness: Float = 0.4f): Int {
val hue = fold(0) { acc, char -> char.code + acc * 37 } % 360
return ColorUtils.HSLToColor(floatArrayOf(hue.absoluteValue.toFloat(), saturation, lightness))
}
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun UserHead(
id: String,
firstName: String,
lastName: String,
modifier: Modifier = Modifier,
size: Dp = 40.dp,
textStyle: TextStyle = MaterialTheme.typography.subtitle1,
) {
Box(modifier.size(size), contentAlignment = Alignment.Center) {
val color = remember(id, firstName, lastName) {
val name = listOf(firstName, lastName)
.joinToString(separator = "")
.uppercase()
Color("$id / $name".toHslColor())
}
val initials = (firstName.take(1) + lastName.take(1)).uppercase()
Canvas(modifier = Modifier.fillMaxSize()) {
drawCircle(SolidColor(color))
}
Text(text = initials, style = textStyle, color = Color.White)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment