diff --git a/colorpicker-compose/api/android/colorpicker-compose.api b/colorpicker-compose/api/android/colorpicker-compose.api index 6158c20..a760ff9 100644 --- a/colorpicker-compose/api/android/colorpicker-compose.api +++ b/colorpicker-compose/api/android/colorpicker-compose.api @@ -50,6 +50,8 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController public final fun selectCenter (Z)V public final fun setAlpha (FZ)V public final fun setBrightness (FZ)V + public final fun setDebounceDuration (J)V + public static synthetic fun setDebounceDuration$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)V public final fun setEnabled (Z)V public final fun setPaletteImageBitmap (Landroidx/compose/ui/graphics/ImageBitmap;)V public final fun setWheelAlpha (F)V diff --git a/colorpicker-compose/api/desktop/colorpicker-compose.api b/colorpicker-compose/api/desktop/colorpicker-compose.api index 6158c20..a760ff9 100644 --- a/colorpicker-compose/api/desktop/colorpicker-compose.api +++ b/colorpicker-compose/api/desktop/colorpicker-compose.api @@ -50,6 +50,8 @@ public final class com/github/skydoves/colorpicker/compose/ColorPickerController public final fun selectCenter (Z)V public final fun setAlpha (FZ)V public final fun setBrightness (FZ)V + public final fun setDebounceDuration (J)V + public static synthetic fun setDebounceDuration$default (Lcom/github/skydoves/colorpicker/compose/ColorPickerController;JILjava/lang/Object;)V public final fun setEnabled (Z)V public final fun setPaletteImageBitmap (Landroidx/compose/ui/graphics/ImageBitmap;)V public final fun setWheelAlpha (F)V diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/CanvasExtensions.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/CanvasExtensions.kt index dcdd32c..3296dec 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/CanvasExtensions.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/CanvasExtensions.kt @@ -21,19 +21,19 @@ import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.Paint import androidx.compose.ui.unit.IntSize -internal inline fun Canvas.drawImage(image: ImageBitmap, topLeft: Offset = Offset.Zero) { +internal fun Canvas.drawImage(image: ImageBitmap, topLeft: Offset = Offset.Zero) { drawImage(image, topLeft, emptyPaint) } -internal inline fun Canvas.drawImageCenterAt(image: ImageBitmap, center: Offset = Offset.Zero) { +internal fun Canvas.drawImageCenterAt(image: ImageBitmap, center: Offset = Offset.Zero) { drawImage(image, center - image.size.center) } -internal inline fun Canvas.drawRect(size: IntSize, paint: Paint) { +internal fun Canvas.drawRect(size: IntSize, paint: Paint) { drawRect(0f, 0f, size.width.toFloat(), size.height.toFloat(), paint) } -internal inline fun Canvas.drawRoundRect(size: IntSize, radius: Float, paint: Paint) { +internal fun Canvas.drawRoundRect(size: IntSize, radius: Float, paint: Paint) { drawRoundRect(0f, 0f, size.width.toFloat(), size.height.toFloat(), radius, radius, paint) } diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorExtensions.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorExtensions.kt index 890bd2b..e61a80e 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorExtensions.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorExtensions.kt @@ -27,7 +27,7 @@ internal val Color.hexCode: String return a.hex + r.hex + g.hex + b.hex } -private inline val Int.hex get() = this.toString(16).padStart(2, '0') +private val Int.hex get() = this.toString(16).padStart(2, '0') internal fun Color.toHSV(): Triple { val cmax = maxOf(red, green, blue) @@ -50,8 +50,8 @@ internal fun Color.toHSV(): Triple { } /** Converts an HS(V) color to a coordinate on the hue/saturation circle. */ -internal inline fun hsvToCoord(h: Float, s: Float, center: Offset) = +internal fun hsvToCoord(h: Float, s: Float, center: Offset) = Offset.fromAngle(hueToAngle(h), s * center.minCoordinate) + center -internal inline fun angleToHue(angle: Float) = (-angle.toDegrees() + 360f) % 360f -internal inline fun hueToAngle(hue: Float) = -hue.toRadians() +internal fun angleToHue(angle: Float) = (-angle.toDegrees() + 360f) % 360f +internal fun hueToAngle(hue: Float) = -hue.toRadians() diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPicker.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPicker.kt index 74d2d9b..2ade58d 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPicker.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPicker.kt @@ -66,7 +66,7 @@ internal fun ColorPicker( DisposableEffect(key1 = controller) { controller.coroutineScope.launch(Dispatchers.Main) { - controller.getColorFlow().collect { onColorChanged(it) } // TODO: debounce parameter + controller.getColorFlow().collect { onColorChanged(it) } } onDispose { controller.releaseBitmap() } } diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPickerController.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPickerController.kt index 2af934d..bf2a6fd 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPickerController.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ColorPickerController.kt @@ -102,6 +102,9 @@ constructor( /** An [ImageBitmap] to be drawn on the canvas as a wheel. */ public var wheelBitmap: ImageBitmap? = null + /** A debounce duration for observing color changes. */ + private var debounceDuration: Long? = null + /** Radius to draw default wheel. */ public var wheelRadius: Dp = 12.dp set(value) { @@ -153,7 +156,7 @@ constructor( @OptIn(FlowPreview::class) public fun getColorFlow(debounceDuration: Long = 0): Flow = - _colorFlow.filterNotNull().debounce(debounceDuration) + _colorFlow.filterNotNull().debounce(this.debounceDuration ?: debounceDuration) // Function that takes a coordinate and obtains a color // Also returns an adjusted coordinate if appropriate @@ -262,6 +265,11 @@ constructor( } } + /** Sets the debounce duration that allows you to observe color changes with a given duration. */ + public fun setDebounceDuration(duration: Long = 0) { + this.debounceDuration = duration + } + /** Notify color changes to the color picker and other subcomponents. */ private fun notifyColorChanged(fromUser: Boolean) { val color = _selectedColor.value diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/GeometryExtensions.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/GeometryExtensions.kt index 424d563..06cff36 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/GeometryExtensions.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/GeometryExtensions.kt @@ -29,26 +29,27 @@ import kotlin.math.sqrt internal const val PI_F = PI.toFloat() internal const val TO_DEGREES = 180f / PI_F internal const val TO_RADIANS = PI_F / 180f -internal inline fun Float.toRadians() = this * TO_RADIANS -internal inline fun Float.toDegrees() = this * TO_DEGREES +internal fun Float.toRadians() = this * TO_RADIANS +internal fun Float.toDegrees() = this * TO_DEGREES // These exist for Size but not IntSize -internal inline val IntSize.center get() = Offset(width * 0.5f, height * 0.5f) -internal inline operator fun IntSize.times(scale: Float) = +internal val IntSize.center get() = Offset(width * 0.5f, height * 0.5f) +internal operator fun IntSize.times(scale: Float) = IntSize((width * scale).roundToInt(), (height * scale).roundToInt()) // New extensions -internal inline val IntSize.radius get() = min(width, height) * 0.5f +internal val IntSize.radius get() = min(width, height) * 0.5f -internal inline val Offset.minCoordinate get() = min(x, y) // exists for Size but not Offset -internal inline fun Offset.distanceTo(other: Offset) = sqrt( +internal val Offset.minCoordinate get() = min(x, y) // exists for Size but not Offset +internal fun Offset.distanceTo(other: Offset) = sqrt( (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y), ) -internal inline fun Offset.midpoint(other: Offset) = (this + other) * 0.5f -internal inline fun Offset.length() = sqrt(x * x + y * y) -internal inline fun Offset.angle() = atan2(y, x) -internal inline fun Offset.roundToInt() = IntOffset(x.roundToInt(), y.roundToInt()) +internal fun Offset.midpoint(other: Offset) = (this + other) * 0.5f +internal fun Offset.length() = sqrt(x * x + y * y) +internal fun Offset.angle() = atan2(y, x) -internal inline fun Offset.Companion.fromAngle(radians: Float, magnitude: Float) = +internal fun Offset.roundToInt() = IntOffset(x.roundToInt(), y.roundToInt()) + +internal fun Offset.Companion.fromAngle(radians: Float, magnitude: Float) = Offset(cos(radians) * magnitude, sin(radians) * magnitude) diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageBitmapExtensions.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageBitmapExtensions.kt index ac2efa7..5c30db4 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageBitmapExtensions.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageBitmapExtensions.kt @@ -30,12 +30,12 @@ internal fun ImageBitmap.getPixel(x: Int, y: Int): Color { return Color(buffer[0]) } -internal inline fun ImageBitmap.getPixel(point: IntOffset): Color = +internal fun ImageBitmap.getPixel(point: IntOffset): Color = getPixel(point.x, point.y) -internal inline val ImageBitmap.size get() = IntSize(width, height) +internal val ImageBitmap.size get() = IntSize(width, height) -internal inline fun ImageBitmap.Companion.fromDrawing(size: IntSize, draw: Canvas.() -> Unit) = +internal fun ImageBitmap.Companion.fromDrawing(size: IntSize, draw: Canvas.() -> Unit) = ImageBitmap(size.width, size.height, ImageBitmapConfig.Argb8888).also { Canvas(it).draw() } internal fun ImageBitmap.Companion.fromPaint(paint: Paint, size: IntSize) =