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

Ability to use Bitmap instead of ImageBitmap #257

Open
sosauce opened this issue Jan 16, 2025 · 4 comments
Open

Ability to use Bitmap instead of ImageBitmap #257

sosauce opened this issue Jan 16, 2025 · 4 comments

Comments

@sosauce
Copy link

sosauce commented Jan 16, 2025

Hello !

So I've found out that using .asImageBitmap() to convert Bitmap to ImageBitmap kinda slow making the theme switch 3 seconds after image change compared to instant when using Google's Palette API which takes in a Bitmap.

Could it be possible to directly Bitmaps ?

Thanks in advance ^^

@jordond
Copy link
Owner

jordond commented Jan 26, 2025

Which function are you using? The androidx Palette API does things differently, and is probably faster than the implementation in MCU.

@sosauce
Copy link
Author

sosauce commented Jan 26, 2025

class ThemeProcessingViewModel: ViewModel() {


    var palette by mutableStateOf<Palette?>(null)
    var dominantColor by mutableStateOf(Color.Black)

    private fun createPaletteAsync(bitmap: Bitmap) {
        Palette.from(bitmap).generate { generatedPalette ->
             palette = generatedPalette
        }
    }


    fun calculateSeedColor(bitmap: ImageBitmap) {
        val suitableColors = bitmap.themeColors(fallback = Color.Black)
        dominantColor = suitableColors.first()
    }

    fun urlToBitmap(
        imageUri: Uri?,
        context: Context,
    ) {
        viewModelScope.launch(Dispatchers.IO) {
            val loader = ImageLoader(context)
            val request = ImageRequest.Builder(context)
                .data(imageUri)
                .allowHardware(false) // If set to true it will not work ???
                .build()
            val result = loader.execute(request)
            if (result is SuccessResult) {
                createPaletteAsync((result.image as BitmapImage).bitmap)
                // Note: When MaterialKolor supports Bitmap or is faster use it and remove Palette dependency
                // calculateSeedColor((result.image as BitmapImage).bitmap.asImageBitmap())
            } else if (result is ErrorResult) {
                cancel(result.throwable.localizedMessage ?: "CuteError", result.throwable)
            }
        }
    }
}

Here's my full ViewModel, as commented, creating a Palette from a bitmap is instantaneous compared to generating a seed color. I may have led to un-necessary confusion, as it may be the process of generating a seed color that is slow as opposed to converting to an ImageBitmap

@jordond
Copy link
Owner

jordond commented Jan 26, 2025

Yeah so ImageBitmap.themeColors() is relatively slow. It is a direct copy from the material-color-utilities.

For a better/faster approach you can use my KMP port of android.palette: [kmpalette](https://github.com/jordond/kmpalette]. But I will warn you, it's in the process of being updated as it's been awhile since I've given it some love. It currently doesn't have WASM support if you need that, but I'm working on implementing it.

@sosauce
Copy link
Author

sosauce commented Jan 26, 2025

Thanks ! I'll wait for an update, my endgoal is achieved and works correctly so it's not a rush ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants