diff --git a/app/src/main/java/ua/acclorite/book_story/presentation/ui/Color.kt b/app/src/main/java/ua/acclorite/book_story/presentation/ui/Color.kt index 0a0da6ee..6d479adc 100644 --- a/app/src/main/java/ua/acclorite/book_story/presentation/ui/Color.kt +++ b/app/src/main/java/ua/acclorite/book_story/presentation/ui/Color.kt @@ -1,15 +1,12 @@ package ua.acclorite.book_story.presentation.ui -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.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.blackTheme 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.lavenderTheme import ua.acclorite.book_story.presentation.ui.theme.marshTheme @@ -46,7 +43,6 @@ fun String.toTheme(): Theme { * * @return a [ColorScheme]. */ -@SuppressLint("NewApi") @Composable fun colorScheme( theme: Theme, @@ -57,11 +53,7 @@ fun colorScheme( val colorScheme = when (theme) { Theme.DYNAMIC -> { /* Dynamic Theme */ - if (darkTheme) { - dynamicDarkColorScheme(LocalContext.current) - } else { - dynamicLightColorScheme(LocalContext.current) - } + dynamicTheme(isDark = darkTheme) } Theme.BLUE -> { diff --git a/app/src/main/java/ua/acclorite/book_story/presentation/ui/theme/DynamicTheme.kt b/app/src/main/java/ua/acclorite/book_story/presentation/ui/theme/DynamicTheme.kt new file mode 100644 index 00000000..6ddd4625 --- /dev/null +++ b/app/src/main/java/ua/acclorite/book_story/presentation/ui/theme/DynamicTheme.kt @@ -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)) +} \ No newline at end of file