Skip to content

Commit

Permalink
exposed the checkerboard drawable function from DialogColorUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
MFlisarWork committed Oct 18, 2023
1 parent 71343e0 commit cdf26c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -282,10 +283,7 @@ private fun Content(
//.size(previewSize)
.clip(shape)
) {
DialogColorUtil.drawCheckerboard(
this,
checkerBoardPixelSize
)
DialogColorUtil.drawCheckerboard(this, density)
}
Spacer(
modifier = Modifier
Expand Down Expand Up @@ -358,7 +356,7 @@ private fun Content(
shape,
space,
size,
checkerBoardPixelSize
density
) {
selectedMainColor.value = ColorDefinitions.COLORS[it]
selectedPresetsLevel.value = 1
Expand All @@ -374,7 +372,7 @@ private fun Content(
shape,
space,
size,
checkerBoardPixelSize
density
) {
selectedSubColor.value =
selectedMainColor.value.colors[it].getColor(context)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit cdf26c8

Please sign in to comment.