This commit is contained in:
vfsfitvnm 2022-10-16 14:36:37 +02:00
parent 1e6a6dc6de
commit 8bd4e0e715
2 changed files with 27 additions and 29 deletions

View file

@ -4,45 +4,43 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.saveable.Saver import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.graphics.ColorUtils
data class Appearance( data class Appearance(
val colorPalette: ColorPalette, val colorPalette: ColorPalette,
val typography: Typography = typographyOf(colorPalette.text), val typography: Typography,
val thumbnailShape: Shape val thumbnailShape: Shape
) { ) {
companion object : Saver<Appearance, List<Any>> { companion object : Saver<Appearance, List<Any>> {
override fun restore(value: List<Any>) = Appearance( override fun restore(value: List<Any>): Appearance {
colorPalette = ColorPalette( val colorPalette = when (val accent = value[0] as Int) {
background0 = Color((value[0] as Long).toULong()), 0 -> DefaultDarkColorPalette
background1 = Color((value[1] as Long).toULong()), 1 -> DefaultLightColorPalette
background2 = Color((value[2] as Long).toULong()), 2 -> PureBlackColorPalette
accent = Color((value[3] as Long).toULong()), else -> dynamicColorPaletteOf(
onAccent = Color((value[4] as Long).toULong()), FloatArray(3).apply { ColorUtils.colorToHSL(accent, this) },
red = Color((value[5] as Long).toULong()), value[1] as Boolean
blue = Color((value[6] as Long).toULong()),
text = Color((value[7] as Long).toULong()),
textSecondary = Color((value[8] as Long).toULong()),
textDisabled = Color((value[9] as Long).toULong()),
isDark = value[10] as Boolean
),
thumbnailShape = RoundedCornerShape((value[11] as Int).dp)
) )
}
return Appearance(
colorPalette = colorPalette,
typography = typographyOf(colorPalette.text),
thumbnailShape = RoundedCornerShape((value[2] as Int).dp)
)
}
override fun SaverScope.save(value: Appearance) = override fun SaverScope.save(value: Appearance) =
listOf( listOf(
value.colorPalette.background0.value.toLong(), when {
value.colorPalette.background1.value.toLong(), value.colorPalette === DefaultDarkColorPalette -> 0
value.colorPalette.background2.value.toLong(), value.colorPalette === DefaultLightColorPalette -> 1
value.colorPalette.accent.value.toLong(), value.colorPalette === PureBlackColorPalette -> 2
value.colorPalette.onAccent.value.toLong(), else -> value.colorPalette.accent.toArgb()
value.colorPalette.red.value.toLong(), },
value.colorPalette.blue.value.toLong(),
value.colorPalette.text.value.toLong(),
value.colorPalette.textSecondary.value.toLong(),
value.colorPalette.textDisabled.value.toLong(),
value.colorPalette.isDark, value.colorPalette.isDark,
when (value.thumbnailShape) { when (value.thumbnailShape) {
RoundedCornerShape(2.dp) -> 2 RoundedCornerShape(2.dp) -> 2

View file

@ -100,7 +100,7 @@ fun dynamicColorPaletteOf(bitmap: Bitmap, isDark: Boolean): ColorPalette? {
} }
} }
private fun dynamicColorPaletteOf(hsl: FloatArray, isDark: Boolean): ColorPalette { fun dynamicColorPaletteOf(hsl: FloatArray, isDark: Boolean): ColorPalette {
return colorPaletteOf(ColorPaletteName.Dynamic, if (isDark) ColorPaletteMode.Dark else ColorPaletteMode.Light, false).copy( return colorPaletteOf(ColorPaletteName.Dynamic, if (isDark) ColorPaletteMode.Dark else ColorPaletteMode.Light, false).copy(
background0 = Color.hsl(hsl[0], hsl[1].coerceAtMost(0.1f), if (isDark) 0.10f else 0.925f), background0 = Color.hsl(hsl[0], hsl[1].coerceAtMost(0.1f), if (isDark) 0.10f else 0.925f),
background1 = Color.hsl(hsl[0], hsl[1].coerceAtMost(0.3f), if (isDark) 0.15f else 0.90f), background1 = Color.hsl(hsl[0], hsl[1].coerceAtMost(0.3f), if (isDark) 0.15f else 0.90f),