@@ -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
2935fun 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