From cdf26c8969fd2b6bd14ef4930843e9295d0b10b5 Mon Sep 17 00:00:00 2001 From: Michael Flisar Date: Wed, 18 Oct 2023 15:05:19 +0200 Subject: [PATCH] exposed the checkerboard drawable function from DialogColorUtil --- .../dialogs/color/DialogColor.kt | 16 +++++++--------- .../dialogs/color/DialogColorUtil.kt | 18 +++++++++++------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColor.kt b/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColor.kt index f7a092e..7986705 100644 --- a/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColor.kt +++ b/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColor.kt @@ -50,6 +50,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.michaelflisar.composedialogs.core.Dialog @@ -254,7 +255,7 @@ private fun Content( labelStyle: DialogColorLabelStyle ) { val context = LocalContext.current - val checkerBoardPixelSize = with(LocalDensity.current) { 4.dp.toPx().toInt() } + val density = LocalDensity.current val updateSelectedPresetColors = { selectedMainColor.value = DialogColorUtil.getNearestColorGroup(context, color.value) selectedSubColor.value = null @@ -282,10 +283,7 @@ private fun Content( //.size(previewSize) .clip(shape) ) { - DialogColorUtil.drawCheckerboard( - this, - checkerBoardPixelSize - ) + DialogColorUtil.drawCheckerboard(this, density) } Spacer( modifier = Modifier @@ -358,7 +356,7 @@ private fun Content( shape, space, size, - checkerBoardPixelSize + density ) { selectedMainColor.value = ColorDefinitions.COLORS[it] selectedPresetsLevel.value = 1 @@ -374,7 +372,7 @@ private fun Content( shape, space, size, - checkerBoardPixelSize + density ) { selectedSubColor.value = selectedMainColor.value.colors[it].getColor(context) @@ -481,7 +479,7 @@ private fun ColorGrid( shape: Shape, space: Dp, size: Dp, - checkerBoardPixelSize: Int, + density: Density, onClick: (index: Int) -> Unit ) { val rows = ceil(colors.size.toFloat() / gridSize.toFloat()).toInt() @@ -519,7 +517,7 @@ private fun ColorGrid( .fillMaxSize() .clip(shape) ) { - DialogColorUtil.drawCheckerboard(this, checkerBoardPixelSize) + DialogColorUtil.drawCheckerboard(this, density) } if (color == Color.Black && level == 0) { Canvas( diff --git a/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColorUtil.kt b/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColorUtil.kt index 4d46752..35898f3 100644 --- a/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColorUtil.kt +++ b/library/modules/color/src/main/java/com/michaelflisar/composedialogs/dialogs/color/DialogColorUtil.kt @@ -5,12 +5,16 @@ import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.DrawScope +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.Density +import androidx.compose.ui.unit.dp import com.michaelflisar.composedialogs.dialogs.color.classes.ColorDefinitions import com.michaelflisar.composedialogs.dialogs.color.classes.GroupedColor -internal object DialogColorUtil { +object DialogColorUtil { - fun drawCheckerboard(drawScope: DrawScope, pixelSize: Int) { + fun drawCheckerboard(drawScope: DrawScope, density: Density) { + val pixelSize = with(density) { 4.dp.toPx().toInt() } val color1 = Color(0xFFC2C2C2) val color2 = Color(0xFFF3F3F3) val sizePixel = Size(pixelSize.toFloat(), pixelSize.toFloat()) @@ -22,12 +26,12 @@ internal object DialogColorUtil { } } - fun drawBlackWhite(drawScope: DrawScope, alpha: Float) { + internal fun drawBlackWhite(drawScope: DrawScope, alpha: Float) { drawScope.drawRect(Color.Black.copy(alpha = alpha), size = Size(drawScope.size.width / 2, drawScope.size.height)) drawScope.drawRect(Color.White.copy(alpha = alpha), Offset(x = drawScope.size.width / 2, y = 0f), size = Size(drawScope.size.width / 2, drawScope.size.height)) } - fun getNearestColorGroup(context: Context, color: Color): GroupedColor { + internal fun getNearestColorGroup(context: Context, color: Color): GroupedColor { val solidColor = color.copy(alpha = 1f) var bestMatch = ColorDefinitions.COLORS_BW var minDiff: Double? = null @@ -45,7 +49,7 @@ internal object DialogColorUtil { return bestMatch } - fun getBestTextColor(background: Color): Color { + internal fun getBestTextColor(background: Color): Color { if (background.alpha <= 0.4f) { return Color.Black } @@ -56,11 +60,11 @@ internal object DialogColorUtil { } } - fun getDarknessFactor(color: Color): Double { + internal fun getDarknessFactor(color: Color): Double { return 1.0 - (0.299 * color.red + 0.587 * color.green + 0.114 * color.blue) } - fun calcColorDifference(c1: Color, c2: Color): Double { + internal fun calcColorDifference(c1: Color, c2: Color): Double { val r1 = c1.red val g1 = c1.green val b1 = c1.blue