Skip to content

Commit

Permalink
Code quality updates
Browse files Browse the repository at this point in the history
  • Loading branch information
coderforlife committed Jun 28, 2024
1 parent c180107 commit 62ac435
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ internal fun Color.toHSV(): Triple<Float, Float, Float> {
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,15 +26,13 @@ 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
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Boolean> = mutableStateOf(true)

Expand All @@ -126,6 +140,7 @@ public class ColorPickerController @OptIn(DelicateCoroutinesApi::class) construc
internal var reviseTick = mutableIntStateOf(0)

private var _colorFlow = MutableStateFlow<ColorEnvelope?>(null)

@OptIn(FlowPreview::class)
public fun getColorFlow(debounceDuration: Long = 0): Flow<ColorEnvelope> =
_colorFlow.filterNotNull().debounce(debounceDuration)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -87,6 +88,6 @@ public fun HsvColorPicker(
}
}
},
draw = { drawHsvColorGradient(controller.canvasSize) }
draw = { drawHsvColorGradient(controller.canvasSize) },
)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ private fun scaleToHeight(imSize: IntSize, targetSize: IntSize): Pair<Float, Off
val scale = targetSize.height.toFloat() / imSize.height
val offset = Offset((targetSize.width - imSize.width * scale) * 0.5f, 0f)
return scale to offset
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.annotation.FloatRange
Expand Down Expand Up @@ -118,7 +133,7 @@ internal fun Slider(
canvas.drawImage(it)

// draw a linear gradient color shader.
val halfHeight = height*0.5f
val halfHeight = height * 0.5f
colorPaint.shader = LinearGradientShader(
colors = controller.getGradientColors(),
from = Offset(0f, halfHeight),
Expand All @@ -129,8 +144,14 @@ internal fun Slider(

// draw wheel bitmap on the canvas.
canvas.drawWheel(
controller.getValue(), width, height,
wheelImageBitmap, wheelRadiusPx, wheelColor, wheelAlpha, wheelPaint
controller.getValue(),
width,
height,
wheelImageBitmap,
wheelRadiusPx,
wheelColor,
wheelAlpha,
wheelPaint,
)
}

Expand Down

0 comments on commit 62ac435

Please sign in to comment.