diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/AlphaTile.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/AlphaTile.kt index b8912b9..91cc799 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/AlphaTile.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/AlphaTile.kt @@ -62,8 +62,9 @@ public fun AlphaTile( modifier .fillMaxSize() .onSizeChanged { size -> - if (size.width != 0 && size.height != 0) + if (size.width != 0 && size.height != 0) { background = ImageBitmap.fromPaint(paint, size) + } }, ) { drawIntoCanvas { canvas -> diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt index 8846505..0f4bb0c 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/BrightnessSlider.kt @@ -24,7 +24,6 @@ import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.Paint import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp -import kotlin.math.max /** * BrightnessSlider allows you to adjust the brightness value of the selected color from color pickers. 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 2dc97a3..dcdd32c 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 @@ -1,3 +1,18 @@ +/* + * Designed and developed by 2022 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.github.skydoves.colorpicker.compose import androidx.compose.ui.geometry.Offset 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 1e55941..890bd2b 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 @@ -34,10 +34,15 @@ internal fun Color.toHSV(): Triple { val cmin = minOf(red, green, blue) val diff = cmax - cmin - val h = if (diff == 0f) 0.0f - else if (cmax == red) (60 * ((green - blue) / diff) + 360f) % 360f - else if (cmax == green) (60 * ((blue - red) / diff) + 120f) % 360f - else /*if (cmax == blue)*/ (60 * ((red - green) / diff) + 240f) % 360f + val h = if (diff == 0f) { + 0.0f + } else if (cmax == red) { + (60 * ((green - blue) / diff) + 360f) % 360f + } else if (cmax == green) { + (60 * ((blue - red) / diff) + 120f) % 360f + } else { + (60 * ((red - green) / diff) + 240f) % 360f // if (cmax == blue) + } val s = if (cmax == 0f) 0f else diff / cmax val v = cmax 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 4008648..0bd9a78 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 @@ -18,7 +18,6 @@ package com.github.skydoves.colorpicker.compose import androidx.compose.foundation.Canvas import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.gestures.detectTapGestures -import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.getValue @@ -27,7 +26,6 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.geometry.Rect import androidx.compose.ui.geometry.Size import androidx.compose.ui.graphics.Canvas import androidx.compose.ui.graphics.ImageBitmap @@ -35,7 +33,6 @@ import androidx.compose.ui.graphics.Paint import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.drawIntoCanvas import androidx.compose.ui.input.pointer.pointerInput -import androidx.compose.ui.layout.onPlaced import androidx.compose.ui.layout.onSizeChanged import androidx.compose.ui.unit.IntSize import androidx.compose.ui.unit.toSize 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 6fb478d..3b3f779 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 @@ -56,7 +56,9 @@ public fun rememberColorPickerController(): ColorPickerController { * with the [rememberColorPickerController] extension. */ @Stable -public class ColorPickerController @OptIn(DelicateCoroutinesApi::class) constructor( +public class ColorPickerController +@OptIn(DelicateCoroutinesApi::class) +constructor( internal val coroutineScope: CoroutineScope = GlobalScope, ) { internal var canvasSize: Size = Size.Zero @@ -94,21 +96,33 @@ public class ColorPickerController @OptIn(DelicateCoroutinesApi::class) construc /** Radius to draw default wheel. */ public var wheelRadius: Dp = 12.dp - set(value) { field = value; reviseTick.intValue++ } + set(value) { + field = value + reviseTick.intValue++ + } /** Paint to draw default wheel. */ public var wheelPaint: Paint = Paint().apply { color = Color.White } - set(value) { field = value; reviseTick.intValue++ } + set(value) { + field = value + reviseTick.intValue++ + } /** Color of the default wheel. */ public var wheelColor: Color get() = wheelPaint.color - set(value) { wheelPaint.color = value; reviseTick.intValue++ } + set(value) { + wheelPaint.color = value + reviseTick.intValue++ + } /** Color of the default wheel. */ public var wheelAlpha: Float get() = wheelPaint.alpha - set(value) { wheelPaint.alpha = value; reviseTick.intValue++ } + set(value) { + wheelPaint.alpha = value + reviseTick.intValue++ + } private val _enabled: MutableState = mutableStateOf(true) @@ -126,6 +140,7 @@ public class ColorPickerController @OptIn(DelicateCoroutinesApi::class) construc internal var reviseTick = mutableIntStateOf(0) private var _colorFlow = MutableStateFlow(null) + @OptIn(FlowPreview::class) public fun getColorFlow(debounceDuration: Long = 0): Flow = _colorFlow.filterNotNull().debounce(debounceDuration) 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 663c263..424d563 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 @@ -1,7 +1,21 @@ +/* + * Designed and developed by 2022 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.github.skydoves.colorpicker.compose import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.geometry.Size import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.IntSize import kotlin.math.PI @@ -28,7 +42,7 @@ internal inline 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( - (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y) + (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) diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorGradient.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorGradient.kt index b4aef11..6627fae 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorGradient.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorGradient.kt @@ -24,7 +24,6 @@ import androidx.compose.ui.graphics.Paint import androidx.compose.ui.graphics.RadialGradient import androidx.compose.ui.graphics.TileMode - /** * Draws hsv color gradient with hue and saturation on a canvas. */ @@ -48,13 +47,13 @@ private val hsvSweepGradient = Brush.sweepGradient( 0.666f to Color.Green, 0.833f to Color.Yellow, 0.999f to Color.Red, - //center = center, + // center = center, ) private val saturationGradient = Brush.radialGradient( 0f to Color(0xFFFFFFFF), 1f to Color(0x00FFFFFF), - //center = center, - //radius = radius, + // center = center, + // radius = radius, tileMode = TileMode.Clamp, ) as RadialGradient diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt index 5c41d16..8c1191e 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/HsvColorPicker.kt @@ -78,7 +78,8 @@ public fun HsvColorPicker( val angle = vector.angle() val hue = angleToHue(angle) // hue: 0 to 360 val sat = min(vector.length() / radius, 1f) // saturation: 0 to 1 - val newPoint = Offset.fromAngle(angle, sat * radius) + center // in case saturation was out of bounds + // adjust point in case saturation was out of bounds + val newPoint = Offset.fromAngle(angle, sat * radius) + center try { Color.hsv(hue, sat, 1f) to newPoint } catch (e: IllegalArgumentException) { @@ -87,6 +88,6 @@ public fun HsvColorPicker( } } }, - draw = { drawHsvColorGradient(controller.canvasSize) } + draw = { drawHsvColorGradient(controller.canvasSize) }, ) } 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 be3c689..ac2efa7 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 @@ -1,3 +1,18 @@ +/* + * Designed and developed by 2022 skydoves (Jaewoong Eum) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.github.skydoves.colorpicker.compose import androidx.compose.ui.graphics.Canvas diff --git a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt index eb2d780..ac9c175 100644 --- a/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt +++ b/colorpicker-compose/src/commonMain/kotlin/com/github/skydoves/colorpicker/compose/ImageColorPicker.kt @@ -133,4 +133,4 @@ private fun scaleToHeight(imSize: IntSize, targetSize: IntSize): Pair