Skip to content

Commit

Permalink
Support Amoled pure dark mode (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong-tran committed Jun 10, 2024
1 parent 62a4b7d commit 39a8f59
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Contrast
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.LightMode
import androidx.compose.material3.Button
Expand Down Expand Up @@ -52,6 +53,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.graphics.toArgb
Expand Down Expand Up @@ -108,11 +110,13 @@ fun colorSchemePairs() = listOf(
@Composable
internal fun App() {
val isDarkTheme = isSystemInDarkTheme()
val isAmoled by rememberSaveable { mutableStateOf(false) }
var seedColor by rememberSaveable { mutableStateOf(SampleColors[0].toArgb()) }
var style by rememberSaveable { (mutableStateOf(PaletteStyle.TonalSpot.name)) }
val state = rememberDynamicMaterialThemeState(
seedColor = Color(seedColor),
isDark = isDarkTheme,
isAmoled = isAmoled,
style = PaletteStyle.valueOf(style),
)

Expand All @@ -123,15 +127,32 @@ internal fun App() {
.padding(8.dp)
.verticalScroll(rememberScrollState()),
) {
Box(
modifier = Modifier.align(Alignment.End)
Row(
modifier = Modifier
.align(Alignment.End)
) {
IconButton(
onClick = { state.isDark = !state.isDark },
modifier = Modifier.align(Alignment.TopEnd),
) {
val icon = if (state.isDark) Icons.Filled.LightMode else Icons.Filled.DarkMode
Icon(icon, contentDescription = null)
if (state.isDark) {
Box {
IconButton(onClick = { state.isAmoled = !state.isAmoled }) {
val mirror = if (state.isAmoled) {
Modifier.scale(scaleX = -1f, scaleY = 1f)
} else {
Modifier
}
Icon(
Icons.Filled.Contrast,
contentDescription = null,
modifier = mirror,
)
}
}
}

Box {
IconButton(onClick = { state.isDark = !state.isDark }) {
val icon = if (state.isDark) Icons.Filled.LightMode else Icons.Filled.DarkMode
Icon(icon, contentDescription = null)
}
}
}

Expand Down
14 changes: 8 additions & 6 deletions material-kolor/api/android/material-kolor.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public final class com/materialkolor/Contrast : java/lang/Enum {
}

public final class com/materialkolor/DynamicColorSchemeKt {
public static final fun dynamicColorScheme-KTwxG1Y (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-KTwxG1Y$default (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
public static final fun dynamicColorScheme-euL9pac (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-euL9pac$default (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-Pd0R-II (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
}

public final class com/materialkolor/DynamicMaterialThemeKt {
public static final fun AnimatedDynamicMaterialTheme-AnklqjM (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun AnimatedDynamicMaterialTheme-PSU77Us (JZZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme (Lcom/materialkolor/DynamicMaterialThemeState;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-PSU77Us (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-ehQzFsw (JZZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V
}

public final class com/materialkolor/DynamicMaterialThemeState {
Expand All @@ -28,8 +28,10 @@ public final class com/materialkolor/DynamicMaterialThemeState {
public final fun getModifyColorScheme ()Lkotlin/jvm/functions/Function2;
public final fun getSeedColor-0d7_KjU ()J
public final fun getStyle ()Lcom/materialkolor/PaletteStyle;
public final fun isAmoled ()Z
public final fun isDark ()Z
public final fun isExtendedFidelity ()Z
public final fun setAmoled (Z)V
public final fun setContrastLevel (D)V
public final fun setDark (Z)V
public final fun setExtendedFidelity (Z)V
Expand All @@ -38,7 +40,7 @@ public final class com/materialkolor/DynamicMaterialThemeState {
}

public final class com/materialkolor/DynamicMaterialThemeStateKt {
public static final fun rememberDynamicMaterialThemeState-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
public static final fun rememberDynamicMaterialThemeState-Pd0R-II (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
}

public final class com/materialkolor/LocalDynamicMaterialThemeSeedKt {
Expand Down
14 changes: 8 additions & 6 deletions material-kolor/api/jvm/material-kolor.api
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public final class com/materialkolor/Contrast : java/lang/Enum {
}

public final class com/materialkolor/DynamicColorSchemeKt {
public static final fun dynamicColorScheme-KTwxG1Y (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-KTwxG1Y$default (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
public static final fun dynamicColorScheme-euL9pac (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;)Landroidx/compose/material3/ColorScheme;
public static synthetic fun dynamicColorScheme-euL9pac$default (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Landroidx/compose/material3/ColorScheme;
public static final fun rememberDynamicColorScheme-Pd0R-II (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Landroidx/compose/material3/ColorScheme;
}

public final class com/materialkolor/DynamicMaterialThemeKt {
public static final fun AnimatedDynamicMaterialTheme-AnklqjM (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun AnimatedDynamicMaterialTheme-PSU77Us (JZZLcom/materialkolor/PaletteStyle;DLandroidx/compose/animation/core/AnimationSpec;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme (Lcom/materialkolor/DynamicMaterialThemeState;Landroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-PSU77Us (JZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun DynamicMaterialTheme-ehQzFsw (JZZLcom/materialkolor/PaletteStyle;DLandroidx/compose/material3/Shapes;Landroidx/compose/material3/Typography;ZZLandroidx/compose/animation/core/AnimationSpec;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;III)V
}

public final class com/materialkolor/DynamicMaterialThemeState {
Expand All @@ -28,8 +28,10 @@ public final class com/materialkolor/DynamicMaterialThemeState {
public final fun getModifyColorScheme ()Lkotlin/jvm/functions/Function2;
public final fun getSeedColor-0d7_KjU ()J
public final fun getStyle ()Lcom/materialkolor/PaletteStyle;
public final fun isAmoled ()Z
public final fun isDark ()Z
public final fun isExtendedFidelity ()Z
public final fun setAmoled (Z)V
public final fun setContrastLevel (D)V
public final fun setDark (Z)V
public final fun setExtendedFidelity (Z)V
Expand All @@ -38,7 +40,7 @@ public final class com/materialkolor/DynamicMaterialThemeState {
}

public final class com/materialkolor/DynamicMaterialThemeStateKt {
public static final fun rememberDynamicMaterialThemeState-ZPw9REg (JZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
public static final fun rememberDynamicMaterialThemeState-Pd0R-II (JZZLcom/materialkolor/PaletteStyle;DZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)Lcom/materialkolor/DynamicMaterialThemeState;
}

public final class com/materialkolor/LocalDynamicMaterialThemeSeedKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.materialkolor.ktx.toDynamicScheme
*
* @param[seedColor] The color to base the scheme on.
* @param[isDark] Whether the scheme should be dark or light.
* @param[isAmoled] Whether the dark scheme is used with Amoled screen (Pure dark).
* @param[style] The style of the scheme.
* @param[contrastLevel] The contrast level of the scheme.
* @param[isExtendedFidelity] Whether to use the extended fidelity color set. See [MaterialDynamicColors].
Expand All @@ -21,13 +22,15 @@ import com.materialkolor.ktx.toDynamicScheme
public fun rememberDynamicColorScheme(
seedColor: Color,
isDark: Boolean,
isAmoled: Boolean = false,
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = Contrast.Default.value,
isExtendedFidelity: Boolean = false,
modifyColorScheme: ((ColorScheme) -> ColorScheme)? = null,
): ColorScheme = remember(
seedColor,
isDark,
isAmoled,
style,
contrastLevel,
isExtendedFidelity,
Expand All @@ -36,6 +39,7 @@ public fun rememberDynamicColorScheme(
dynamicColorScheme(
seedColor = seedColor,
isDark = isDark,
isAmoled = isAmoled,
style = style,
contrastLevel = contrastLevel,
isExtendedFidelity = isExtendedFidelity,
Expand All @@ -48,13 +52,15 @@ public fun rememberDynamicColorScheme(
*
* @param[seedColor] The color to base the scheme on.
* @param[isDark] Whether the scheme should be dark or light.
* @param[isAmoled] Whether the dark scheme is used with Amoled screen (Pure dark).
* @param[style] The style of the scheme.
* @param[contrastLevel] The contrast level of the scheme.
* @param[isExtendedFidelity] Whether to use the extended fidelity color set. See [MaterialDynamicColors].
*/
public fun dynamicColorScheme(
seedColor: Color,
isDark: Boolean,
isAmoled: Boolean,
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = Contrast.Default.value,
isExtendedFidelity: Boolean = false,
Expand All @@ -64,20 +70,20 @@ public fun dynamicColorScheme(
val colors = MaterialDynamicColors(isExtendedFidelity)

return ColorScheme(
background = colors.background().getColor(scheme),
background = if (isDark && isAmoled) Color.Black else colors.background().getColor(scheme),
error = colors.error().getColor(scheme),
errorContainer = colors.errorContainer().getColor(scheme),
inverseOnSurface = colors.inverseOnSurface().getColor(scheme),
inversePrimary = colors.inversePrimary().getColor(scheme),
inverseSurface = colors.inverseSurface().getColor(scheme),
onBackground = colors.onBackground().getColor(scheme),
onBackground = if (isDark && isAmoled) Color.White else colors.onBackground().getColor(scheme),
onError = colors.onError().getColor(scheme),
onErrorContainer = colors.onErrorContainer().getColor(scheme),
onPrimary = colors.onPrimary().getColor(scheme),
onPrimaryContainer = colors.onPrimaryContainer().getColor(scheme),
onSecondary = colors.onSecondary().getColor(scheme),
onSecondaryContainer = colors.onSecondaryContainer().getColor(scheme),
onSurface = colors.onSurface().getColor(scheme),
onSurface = if (isDark && isAmoled) Color.White else colors.onSurface().getColor(scheme),
onSurfaceVariant = colors.onSurfaceVariant().getColor(scheme),
onTertiary = colors.onTertiary().getColor(scheme),
onTertiaryContainer = colors.onTertiaryContainer().getColor(scheme),
Expand All @@ -88,7 +94,7 @@ public fun dynamicColorScheme(
scrim = colors.scrim().getColor(scheme),
secondary = colors.secondary().getColor(scheme),
secondaryContainer = colors.secondaryContainer().getColor(scheme),
surface = colors.surface().getColor(scheme),
surface = if (isDark && isAmoled) Color.Black else colors.surface().getColor(scheme),
surfaceTint = colors.surfaceTint().getColor(scheme),
surfaceBright = colors.surfaceBright().getColor(scheme),
surfaceDim = colors.surfaceDim().getColor(scheme),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.materialkolor.dynamiccolor.MaterialDynamicColors
* @see PaletteStyle
* @param[seedColor] The seed color to use for generating the color scheme.
* @param[useDarkTheme] Whether to use a dark theme or not.
* @param[withAmoled] Whether the dark scheme is used with Amoled screen (Pure dark).
* @param[style] The style of the color scheme.
* @param[contrastLevel] The contrast level of the color scheme.
* @param[isExtendedFidelity] Whether to use the extended fidelity color set. See [MaterialDynamicColors].
Expand All @@ -33,6 +34,7 @@ import com.materialkolor.dynamiccolor.MaterialDynamicColors
public fun DynamicMaterialTheme(
seedColor: Color,
useDarkTheme: Boolean = isSystemInDarkTheme(),
withAmoled: Boolean = false,
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = Contrast.Default.value,
shapes: Shapes = MaterialTheme.shapes,
Expand All @@ -45,6 +47,7 @@ public fun DynamicMaterialTheme(
val state = rememberDynamicMaterialThemeState(
seedColor = seedColor,
isDark = useDarkTheme,
isAmoled = withAmoled,
style = style,
contrastLevel = contrastLevel,
extendedFidelity = isExtendedFidelity,
Expand Down Expand Up @@ -144,6 +147,7 @@ public fun DynamicMaterialTheme(
* @see PaletteStyle
* @param[seedColor] The seed color to use for generating the color scheme.
* @param[useDarkTheme] Whether to use a dark theme or not.
* @param[withAmoled] Whether the dark scheme is used with Amoled screen (Pure dark).
* @param[style] The style of the color scheme.
* @param[contrastLevel] The contrast level of the color scheme.
* @param[animationSpec] The animation spec to use for animating the color scheme.
Expand All @@ -152,12 +156,14 @@ public fun DynamicMaterialTheme(
*/
@Deprecated(
level = DeprecationLevel.WARNING,
message = "Use DynamicMaterialTheme with animate = true instead."
message = "Use DynamicMaterialTheme with animate = true instead.",
replaceWith = ReplaceWith("DynamicMaterialTheme(animate = true)"),
)
@Composable
public fun AnimatedDynamicMaterialTheme(
seedColor: Color,
useDarkTheme: Boolean = isSystemInDarkTheme(),
withAmoled: Boolean = false,
style: PaletteStyle = PaletteStyle.TonalSpot,
contrastLevel: Double = Contrast.Default.value,
animationSpec: AnimationSpec<Color> = spring(stiffness = Spring.StiffnessLow),
Expand All @@ -169,6 +175,7 @@ public fun AnimatedDynamicMaterialTheme(
DynamicMaterialTheme(
seedColor = seedColor,
useDarkTheme = useDarkTheme,
withAmoled = withAmoled,
style = style,
contrastLevel = contrastLevel,
shapes = shapes,
Expand Down
Loading

0 comments on commit 39a8f59

Please sign in to comment.