🛠️ Fix over-saturated colors in MaterialYou

* Desaturated MaterialYou theme colors, which in most cases are over-saturated.
This commit is contained in:
Acclorite 2024-09-05 18:23:52 +03:00
parent 26e5eb007f
commit ba78352008
2 changed files with 59 additions and 10 deletions

View file

@ -1,15 +1,12 @@
package ua.acclorite.book_story.presentation.ui package ua.acclorite.book_story.presentation.ui
import android.annotation.SuppressLint
import androidx.compose.material3.ColorScheme import androidx.compose.material3.ColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import androidx.compose.ui.platform.LocalContext
import ua.acclorite.book_story.presentation.ui.theme.aquaTheme import ua.acclorite.book_story.presentation.ui.theme.aquaTheme
import ua.acclorite.book_story.presentation.ui.theme.blackTheme import ua.acclorite.book_story.presentation.ui.theme.blackTheme
import ua.acclorite.book_story.presentation.ui.theme.blueTheme import ua.acclorite.book_story.presentation.ui.theme.blueTheme
import ua.acclorite.book_story.presentation.ui.theme.dynamicTheme
import ua.acclorite.book_story.presentation.ui.theme.greenTheme import ua.acclorite.book_story.presentation.ui.theme.greenTheme
import ua.acclorite.book_story.presentation.ui.theme.lavenderTheme import ua.acclorite.book_story.presentation.ui.theme.lavenderTheme
import ua.acclorite.book_story.presentation.ui.theme.marshTheme import ua.acclorite.book_story.presentation.ui.theme.marshTheme
@ -46,7 +43,6 @@ fun String.toTheme(): Theme {
* *
* @return a [ColorScheme]. * @return a [ColorScheme].
*/ */
@SuppressLint("NewApi")
@Composable @Composable
fun colorScheme( fun colorScheme(
theme: Theme, theme: Theme,
@ -57,11 +53,7 @@ fun colorScheme(
val colorScheme = when (theme) { val colorScheme = when (theme) {
Theme.DYNAMIC -> { Theme.DYNAMIC -> {
/* Dynamic Theme */ /* Dynamic Theme */
if (darkTheme) { dynamicTheme(isDark = darkTheme)
dynamicDarkColorScheme(LocalContext.current)
} else {
dynamicLightColorScheme(LocalContext.current)
}
} }
Theme.BLUE -> { Theme.BLUE -> {

View file

@ -0,0 +1,57 @@
package ua.acclorite.book_story.presentation.ui.theme
import android.annotation.SuppressLint
import androidx.compose.material3.ColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
@SuppressLint("NewApi")
@Composable
fun dynamicTheme(isDark: Boolean): ColorScheme {
return if (isDark) {
dynamicDarkColorScheme(LocalContext.current).run {
copy(
surface = surface.desaturateColor(
saturation = 0.73f,
lightness = 0.86f
),
surfaceContainerLowest = surfaceContainerLowest.desaturateColor(
saturation = 0.5f,
lightness = 0.7f
),
surfaceContainerLow = surfaceContainerLow.desaturateColor(
saturation = 0.9f,
lightness = 1f
),
surfaceContainer = surfaceContainer.desaturateColor(
saturation = 0.9f,
lightness = 1f
),
surfaceContainerHigh = surfaceContainerHigh.desaturateColor(
saturation = 0.9f,
lightness = 1f
)
)
}
} else {
dynamicLightColorScheme(LocalContext.current)
}
}
/**
* Desaturates MaterialYou dynamic colors.
* Sometimes they are too colorful, which results in ugly background color.
*/
private fun Color.desaturateColor(saturation: Float, lightness: Float): Color {
val hsl = FloatArray(3)
android.graphics.Color.colorToHSV(toArgb(), hsl)
hsl[1] = hsl[1] * saturation
hsl[2] = hsl[2] * lightness
return Color(android.graphics.Color.HSVToColor(hsl))
}