Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PainterLoader #70

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions kmpalette-core/api/android/kmpalette-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class com/kmpalette/DominantColorState$Companion {
public final class com/kmpalette/DominantColorStateKt {
public static final fun rememberDominantColorState-1Kfb2uI (JJILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
public static final fun rememberDominantColorState-K2djEUw (Lcom/kmpalette/loader/ImageBitmapLoader;JJILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
public static final fun rememberPainterDominantColorState-n5X53cU (JJLandroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
}

public final class com/kmpalette/PaletteKt {
Expand Down Expand Up @@ -88,6 +89,7 @@ public final class com/kmpalette/PaletteState$Companion {
}

public final class com/kmpalette/PaletteStateKt {
public static final fun rememberPainterPaletteState (Landroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
public static final fun rememberPaletteState (ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
public static final fun rememberPaletteState (Lcom/kmpalette/loader/ImageBitmapLoader;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
}
Expand Down Expand Up @@ -153,3 +155,10 @@ public final class com/kmpalette/SwatchTarget$VibrantLight : com/kmpalette/Swatc
public fun toString ()Ljava/lang/String;
}

public final class com/kmpalette/loader/PainterLoader : com/kmpalette/loader/ImageBitmapLoader {
public static final field $stable I
public fun <init> (Landroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;)V
public fun load (Landroidx/compose/ui/graphics/painter/Painter;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public synthetic fun load (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

9 changes: 9 additions & 0 deletions kmpalette-core/api/jvm/kmpalette-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class com/kmpalette/DominantColorState$Companion {
public final class com/kmpalette/DominantColorStateKt {
public static final fun rememberDominantColorState-1Kfb2uI (JJILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
public static final fun rememberDominantColorState-K2djEUw (Lcom/kmpalette/loader/ImageBitmapLoader;JJILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
public static final fun rememberPainterDominantColorState-n5X53cU (JJLandroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/DominantColorState;
}

public final class com/kmpalette/PaletteKt {
Expand Down Expand Up @@ -88,6 +89,7 @@ public final class com/kmpalette/PaletteState$Companion {
}

public final class com/kmpalette/PaletteStateKt {
public static final fun rememberPainterPaletteState (Landroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
public static final fun rememberPaletteState (ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
public static final fun rememberPaletteState (Lcom/kmpalette/loader/ImageBitmapLoader;ILkotlin/coroutines/CoroutineContext;Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lcom/kmpalette/PaletteState;
}
Expand Down Expand Up @@ -153,3 +155,10 @@ public final class com/kmpalette/SwatchTarget$VibrantLight : com/kmpalette/Swatc
public fun toString ()Ljava/lang/String;
}

public final class com/kmpalette/loader/PainterLoader : com/kmpalette/loader/ImageBitmapLoader {
public static final field $stable I
public fun <init> (Landroidx/compose/ui/unit/Density;Landroidx/compose/ui/unit/LayoutDirection;)V
public fun load (Landroidx/compose/ui/graphics/painter/Painter;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public synthetic fun load (Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.palette.graphics.Palette
import com.kmpalette.internal.LruCache
import com.kmpalette.loader.ImageBitmapLoader
import com.kmpalette.loader.PainterLoader
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -61,6 +67,41 @@ public fun rememberDominantColorState(
builder = builder,
)

/**
* Wrapper around [rememberDominantColorState] that uses [PainterLoader] to load the image.
*
* @see rememberDominantColorState
* @param[defaultColor] The default color, which will be used if [Palette.generate] fails.
* @param[defaultOnColor] The default color to use _on_ [defaultColor].
* @param[density] The [Density] used for drawing the [Painter] as [ImageBitmap].
* @param[layoutDirection] The [LayoutDirection] used for drawing the [Painter] as [ImageBitmap].
* @param[cacheSize] The size of the LruCache used to store recent results. Pass `0` to disable.
* @param[coroutineContext] The [CoroutineContext] used to launch the coroutine.
* @param[isSwatchValid] A lambda which allows filtering of the calculated [Palette.Swatch].
* @param[builder] A lambda which allows filtering of the calculated [Palette.Builder] used to generate
* the [Palette].
* @return A [DominantColorState] which can be used to generate a dominant color from a [Painter].
*/
@Composable
public fun rememberPainterDominantColorState(
defaultColor: Color = MaterialTheme.colorScheme.primary,
defaultOnColor: Color = MaterialTheme.colorScheme.onPrimary,
density: Density = LocalDensity.current,
layoutDirection: LayoutDirection = LocalLayoutDirection.current,
cacheSize: Int = 0,
coroutineContext: CoroutineContext = Dispatchers.Default,
isSwatchValid: (Palette.Swatch) -> Boolean = { true },
builder: Palette.Builder.() -> Unit = {},
): DominantColorState<Painter> = rememberDominantColorState(
loader = PainterLoader(density, layoutDirection),
defaultColor = defaultColor,
defaultOnColor = defaultOnColor,
cacheSize = cacheSize,
coroutineContext = coroutineContext,
isSwatchValid = isSwatchValid,
builder = builder
)

/**
* Create a [DominantColorState] which will be remembered across compositions. Then can be used
* to generate a dominant color from an [ImageBitmap].
Expand Down
32 changes: 32 additions & 0 deletions kmpalette-core/src/commonMain/kotlin/com/kmpalette/PaletteState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.palette.graphics.Palette
import com.kmpalette.internal.LruCache
import com.kmpalette.loader.ImageBitmapLoader
import com.kmpalette.loader.PainterLoader
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
Expand Down Expand Up @@ -60,6 +66,32 @@ public fun <T : Any> rememberPaletteState(
}
}

/**
* Wrapper around [rememberPaletteState] that uses [PainterLoader] to load the image.
*
* @see rememberPaletteState
* @param[density] The [Density] used for drawing the [Painter] as [ImageBitmap].
* @param[layoutDirection] The [LayoutDirection] used for drawing the [Painter] as [ImageBitmap].
* @param[cacheSize] The maximum number of [Palette]s to cache. If 0, no caching will be done.
* @param[coroutineContext] The [CoroutineContext] to use for generating [Palette]s.
* @param[builder] A lambda that will be applied to the [Palette.Builder] to customize the
* generation of the [Palette].
* @return A [PaletteState] that will be remembered across composition.
*/
@Composable
public fun rememberPainterPaletteState(
density: Density = LocalDensity.current,
layoutDirection: LayoutDirection = LocalLayoutDirection.current,
cacheSize: Int = DominantColorState.DEFAULT_CACHE_SIZE,
coroutineContext: CoroutineContext = Dispatchers.Default,
builder: Palette.Builder.() -> Unit = {},
): PaletteState<Painter> = rememberPaletteState(
loader = PainterLoader(density, layoutDirection),
cacheSize = cacheSize,
coroutineContext = coroutineContext,
builder = builder
)

/**
* A state object that generates a [Palette] from an [ImageBitmap] using [loader].
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.kmpalette.internal

import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.drawscope.CanvasDrawScope
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import kotlin.math.roundToInt

internal class PainterImage(
private val painter: Painter,
private val density: Density,
private val layoutDirection: LayoutDirection
) {
fun asBitmap(
width: Int = painter.intrinsicSize.width.roundToInt(),
height: Int = painter.intrinsicSize.height.roundToInt()
): ImageBitmap {
val bitmap = ImageBitmap(width, height)
val canvas = Canvas(bitmap)
val floatSize = Size(width.toFloat(), height.toFloat())

bitmap.prepareToDraw()

CanvasDrawScope().draw(
density = density,
layoutDirection = layoutDirection,
canvas = canvas,
size = floatSize
) {
with(painter) {
draw(floatSize)
}
DatL4g marked this conversation as resolved.
Show resolved Hide resolved
}

return bitmap
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package com.kmpalette.loader

import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import com.kmpalette.internal.PainterImage

/**
* A default loader that takes an [ImageBitmap] and returns it.
*/
internal val ImageBitmapLoader = object : ImageBitmapLoader<ImageBitmap> {

override suspend fun load(input: ImageBitmap): ImageBitmap = input
}

/**
* A default loader that takes an [Painter], draws it as an [ImageBitmap] and returns that.
*/
public class PainterLoader(
private val density: Density,
private val layoutDirection: LayoutDirection
) : ImageBitmapLoader<Painter> {
override suspend fun load(input: Painter): ImageBitmap {
return PainterImage(input, density, layoutDirection).asBitmap()
}
}