Skip to content

Commit 89eb53e

Browse files
update hue selector ring
1 parent d458e80 commit 89eb53e

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

app/src/main/java/com/smarttoolfactory/composecolorpicker/demo/ColorPickerDemo.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.graphics.Color
1414
import androidx.compose.ui.text.font.FontWeight
1515
import androidx.compose.ui.unit.dp
16+
import androidx.compose.ui.unit.sp
1617
import com.smarttoolfactory.colorpicker.argbToHex
1718
import com.smarttoolfactory.colorpicker.model.ColorHSL
1819
import com.smarttoolfactory.colorpicker.model.ColorModel
@@ -104,6 +105,7 @@ private fun ColorPickerHSL(initialColor: Color) {
104105
modifier = Modifier
105106
.width(350.dp)
106107
.aspectRatio(1f),
108+
hue = hue,
107109
selectionRadius = 8.dp
108110
) { hueChange ->
109111
hue = hueChange.toFloat()
@@ -156,7 +158,6 @@ private fun ColorPickerHSL(initialColor: Color) {
156158
),
157159
onColorChange = {
158160
(it as? ColorHSL)?.let { color ->
159-
println("COLOR: $color")
160161
hue = color.hue
161162
saturation = color.saturation
162163
lightness = color.lightness
@@ -170,7 +171,12 @@ private fun ColorPickerHSL(initialColor: Color) {
170171
outputColorModel = ColorModel.HSL
171172
)
172173

173-
Text(text =hexString)
174+
Text(
175+
text = hexString,
176+
fontWeight = FontWeight.Bold,
177+
fontSize = 18.sp,
178+
color = currentColor
179+
)
174180

175181
}
176182
}

app/src/main/java/com/smarttoolfactory/composecolorpicker/demo/ColorfulSliderDemo.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,6 @@ private fun HSLSliderDisplayExamples(
331331
saturation = saturation,
332332
lightness = lightness,
333333
onValueChange = {
334-
println("🔥 HUE $hue")
335334
hue = it
336335
}
337336
)

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/selector/HueSelectorRing.kt

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,56 @@ import com.smarttoolfactory.gesture.pointerMotionEvents
2323
/**
2424
* Hue selector Ring that selects hue based on rotation of circle selector.
2525
*
26+
* @param hue
27+
* @param outerRadiusRatio
28+
* @param innerRadiusRatio
29+
* @param borderStroke
30+
* @param borderStrokeWidth
31+
* @param backgroundColor
2632
* @param selectionRadius radius of selection circle that moves based on touch position
2733
*/
2834
@Composable
2935
fun HueSelectorRing(
3036
modifier: Modifier = Modifier,
31-
selectionRadius: Dp = (-1).dp,
37+
hue: Float,
38+
outerRadiusRatio: Float = .9f,
39+
innerRadiusRatio: Float = .6f,
40+
borderStroke: Color = Color.Black,
41+
borderStrokeWidth: Dp = 2.dp,
42+
backgroundColor: Color = Color.Black,
43+
selectionRadius: Dp = Dp.Unspecified,
3244
onChange: (Int) -> Unit
3345
) {
3446

3547
BoxWithConstraints(modifier) {
3648

49+
require(maxWidth == maxHeight)
50+
51+
// Angle from center is required to get Hue which is between 0-360
52+
var angle by remember { mutableStateOf(hue.toInt()) }
53+
angle = hue.toInt()
54+
3755
val density = LocalDensity.current.density
3856

3957
// Check if user touches between the valid area of circle
4058
var isTouched by remember { mutableStateOf(false) }
4159

42-
// Angle from center is required to get Hue which is between 0-360
43-
var angle by remember { mutableStateOf(0) }
60+
val width = constraints.maxWidth.toFloat()
61+
val radius = width / 2
4462

4563
// Center position of color picker
46-
var center by remember { mutableStateOf(Offset.Zero) }
64+
val center = Offset(radius, radius)
4765

48-
var radiusOuter by remember { mutableStateOf(0f) }
49-
var radiusInner by remember { mutableStateOf(0f) }
66+
val radiusInner: Float = (radius * innerRadiusRatio).coerceAtLeast(0f)
67+
val radiusOuter: Float = (radius * outerRadiusRatio).coerceAtLeast(radiusInner)
5068

5169
/**
5270
* Circle selector radius for setting [angle] which sets hue
5371
*/
5472
val selectorRadius =
55-
(if (selectionRadius > 0.dp) selectionRadius.value * density
56-
else radiusInner * 2 * .04f)
57-
.coerceAtMost(radiusOuter - radiusInner)
73+
if (selectionRadius == Dp.Unspecified)
74+
(radiusInner * 2 * .04f).coerceAtMost(radiusOuter - radiusInner)
75+
else selectionRadius.value * density
5876

5977
val colorPickerModifier = modifier
6078
.clipToBounds()
@@ -94,17 +112,6 @@ fun HueSelectorRing(
94112

95113
Canvas(modifier = colorPickerModifier) {
96114

97-
val canvasWidth = this.size.width
98-
val canvasHeight = this.size.height
99-
100-
val cX = canvasWidth / 2
101-
val cY = canvasHeight / 2
102-
val canvasRadius = canvasWidth.coerceAtMost(canvasHeight) / 2f
103-
center = Offset(cX, cY)
104-
105-
radiusOuter = canvasRadius * .9f
106-
radiusInner = canvasRadius * .65f
107-
108115
val strokeWidth = (radiusOuter - radiusInner)
109116

110117
drawCircle(
@@ -116,6 +123,9 @@ fun HueSelectorRing(
116123
)
117124
)
118125

126+
// This is background color from center to inner radius
127+
drawCircle(color = backgroundColor, radius = radiusInner)
128+
119129
// Stroke draws half in and half out of the current radius.
120130
// with 200 radius 20 stroke width starts from 190 and ends at 210
121131
drawCircle(Color.Black, radiusInner - 7f, style = Stroke(width = 14f))

0 commit comments

Comments
 (0)