8000 Implement setDebounceDuration function by skydoves · Pull Request #67 · skydoves/colorpicker-compose · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement setDebounceDuration function #67

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

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions colorpicker-compose/api/android/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expan 8000 d Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions colorpicker-compose/api/desktop/colorpicker-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float, Float, Float> {
val cmax = maxOf(red, green, blue)
Expand All @@ -50,8 +50,8 @@ internal fun Color.toHSV(): Triple<Float, Float, Float> {
}

/** 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()
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -153,7 +156,7 @@ constructor(

@OptIn(FlowPreview::class)
public fun getColorFlow(debounceDuration: Long = 0): Flow<ColorEnvelope> =
_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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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) =
Expand Down
Loading
0