Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marenovakovic/d8756aa42693195b48cc59c0a1a038d4 to your computer and use it in GitHub Desktop.
Save marenovakovic/d8756aa42693195b48cc59c0a1a038d4 to your computer and use it in GitHub Desktop.
import android.graphics.Bitmap
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.core.graphics.ColorUtils
import androidx.palette.graphics.Palette
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
@OptIn(ExperimentalCoroutinesApi::class)
suspend fun Bitmap.computeDominantTopSectionColor(): Pair<Color, Boolean> =
suspendCancellableCoroutine { continuation ->
Palette.from(this)
.setRegion(0, 0, this.width, 24.dp.value.toInt())
.maximumColorCount(3)
.generate { palette ->
palette ?: continuation.cancel()
val statusBarColorRgb = palette!!.dominantSwatch?.rgb
statusBarColorRgb ?: continuation.cancel()
val hsl = FloatArray(3)
ColorUtils.colorToHSL(statusBarColorRgb!!, hsl)
val isLight = hsl[2] >= 0.5
continuation.resume(Color(statusBarColorRgb) to isLight)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment