mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 08:45:27 +02:00
Simplified dynamic theme generation
This commit is contained in:
parent
962471e776
commit
9b6798b191
@ -4,47 +4,9 @@ import android.app.WallpaperManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.luminance
|
||||
|
||||
|
||||
/**
|
||||
* Shifts colors to make them brighter
|
||||
*/
|
||||
@Stable
|
||||
private fun Color.shiftTo255(ratio: Float): Color {
|
||||
return this
|
||||
.copy(
|
||||
alpha,
|
||||
red + (1.0f - red) * ratio,
|
||||
green + (1.0f - green) * ratio,
|
||||
blue + (1.0f - blue) * ratio,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Shifts colors to make them darker
|
||||
*/
|
||||
@Stable
|
||||
private fun Color.shiftTo0(ratio: Float): Color {
|
||||
return this
|
||||
.copy(
|
||||
alpha,
|
||||
red * (1.0f - ratio),
|
||||
green * (1.0f - ratio),
|
||||
blue * (1.0f - ratio),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides which colors fits the best for the color that is used as a background
|
||||
*/
|
||||
@Stable
|
||||
private fun Color.getAppropriateTextColor(): Color {
|
||||
return if (luminance() > 0.5) Color.Black else Color.White
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return dynamic light theme. Determines which API to use based on Android version
|
||||
*/
|
||||
@ -59,49 +21,12 @@ fun dynamicLightTheme(context: Context): ColorScheme {
|
||||
.getWallpaperColors(WallpaperManager.FLAG_SYSTEM)
|
||||
?: return LightThemeColors
|
||||
|
||||
val primary = Color(
|
||||
red = wallpaperColors.primaryColor.red(),
|
||||
green = wallpaperColors.primaryColor.green(),
|
||||
blue = wallpaperColors.primaryColor.blue()
|
||||
)
|
||||
// Secondary color can be null. For such cases we just generate it from primary
|
||||
val secondary: Color =
|
||||
if (wallpaperColors.secondaryColor == null) {
|
||||
primary.shiftTo255(0.5f)
|
||||
} else {
|
||||
Color(
|
||||
red = wallpaperColors.secondaryColor!!.red(),
|
||||
green = wallpaperColors.secondaryColor!!.green(),
|
||||
blue = wallpaperColors.secondaryColor!!.blue()
|
||||
)
|
||||
}.shiftTo255(0.5f)
|
||||
val background = primary.shiftTo255(0.9f)
|
||||
val onBackground = background.getAppropriateTextColor()
|
||||
|
||||
return lightColorScheme(
|
||||
// Settings screen group text, units screen units group text
|
||||
primary = primary.shiftTo0(0.7f),
|
||||
// Text color on Third party Licenses screen
|
||||
onPrimary = primary.shiftTo255(0.3f),
|
||||
// Selected unit, group, keyboard buttons
|
||||
secondaryContainer = secondary,
|
||||
onSecondaryContainer = secondary.getAppropriateTextColor(),
|
||||
// Background color for all screens
|
||||
background = background,
|
||||
// Main screen input/output text color
|
||||
onBackground = onBackground,
|
||||
// Collapsable top bar background
|
||||
surface = background,
|
||||
// Main screen Unitto text, disabled buttons
|
||||
// Settings screen title and back icon, dialog window title, not selected radio button color
|
||||
// Third party licenses screen title and back icon
|
||||
onSurface = onBackground,
|
||||
surfaceVariant = primary.shiftTo255(0.5f),
|
||||
// Main Screen settings icon, Not selected chips text, short unit names
|
||||
// Settings items secondary text
|
||||
onSurfaceVariant = primary.shiftTo0(0.80f),
|
||||
// Chips outline and cards outline on Third party licenses screen
|
||||
outline = primary.shiftTo0(0.8f),
|
||||
Color(
|
||||
wallpaperColors.primaryColor.red(),
|
||||
wallpaperColors.primaryColor.green(),
|
||||
wallpaperColors.primaryColor.blue()
|
||||
)
|
||||
)
|
||||
}
|
||||
// Just in case
|
||||
@ -123,49 +48,12 @@ fun dynamicDarkTheme(context: Context): ColorScheme {
|
||||
.getWallpaperColors(WallpaperManager.FLAG_SYSTEM)
|
||||
?: return DarkThemeColors
|
||||
|
||||
val primary = Color(
|
||||
red = wallpaperColors.primaryColor.red(),
|
||||
green = wallpaperColors.primaryColor.green(),
|
||||
blue = wallpaperColors.primaryColor.blue()
|
||||
)
|
||||
// Secondary color can be null. For such cases we just generate it from primary
|
||||
val secondary: Color =
|
||||
if (wallpaperColors.secondaryColor == null) {
|
||||
primary.shiftTo0(0.5f)
|
||||
} else {
|
||||
Color(
|
||||
red = wallpaperColors.secondaryColor!!.red(),
|
||||
green = wallpaperColors.secondaryColor!!.green(),
|
||||
blue = wallpaperColors.secondaryColor!!.blue()
|
||||
)
|
||||
}.shiftTo0(0.5f)
|
||||
val background = primary.shiftTo0(0.9f)
|
||||
val onBackground = background.getAppropriateTextColor()
|
||||
|
||||
return darkColorScheme(
|
||||
// Settings screen group text, units screen units group text
|
||||
primary = primary.shiftTo255(0.7f),
|
||||
// Text color on Third party Licenses screen
|
||||
onPrimary = primary.shiftTo0(0.3f),
|
||||
// Selected unit, group, keyboard buttons
|
||||
secondaryContainer = secondary,
|
||||
onSecondaryContainer = secondary.getAppropriateTextColor(),
|
||||
// Background color for all screens
|
||||
background = background,
|
||||
// Main screen input/output text color
|
||||
onBackground = onBackground,
|
||||
// Collapsable top bar background
|
||||
surface = background,
|
||||
// Main screen Unitto text, disabled buttons
|
||||
// Settings screen title and back icon, dialog window title, not selected radio button color
|
||||
// Third party licenses screen title and back icon
|
||||
onSurface = onBackground,
|
||||
surfaceVariant = primary.shiftTo0(0.5f),
|
||||
// Main Screen settings icon, Not selected chips text, short unit names
|
||||
// Settings items secondary text
|
||||
onSurfaceVariant = primary.shiftTo255(0.80f),
|
||||
// Chips outline and cards outline on Third party licenses screen
|
||||
outline = primary.shiftTo255(0.8f),
|
||||
Color(
|
||||
wallpaperColors.primaryColor.red(),
|
||||
wallpaperColors.primaryColor.green(),
|
||||
wallpaperColors.primaryColor.blue()
|
||||
)
|
||||
)
|
||||
}
|
||||
// Just in case
|
||||
|
Loading…
x
Reference in New Issue
Block a user