@@ -7,6 +7,7 @@ import android.graphics.RectF
7
7
import android.widget.Toast
8
8
9
9
import androidx.compose.foundation.Image
10
+ import androidx.compose.foundation.gestures.detectDragGestures
10
11
import androidx.compose.foundation.gestures.detectTapGestures
11
12
import androidx.compose.foundation.layout.*
12
13
import androidx.compose.material.Text
@@ -16,6 +17,7 @@ import androidx.compose.ui.Modifier
16
17
import androidx.compose.ui.geometry.Offset
17
18
import androidx.compose.ui.geometry.Size
18
19
import androidx.compose.ui.graphics.Color
20
+ import androidx.compose.ui.input.pointer.PointerInputChange
19
21
import androidx.compose.ui.input.pointer.pointerInput
20
22
import androidx.compose.ui.layout.ContentScale
21
23
import androidx.compose.ui.layout.layoutId
@@ -48,30 +50,38 @@ fun ColorPickerComponent(
48
50
showHex : Boolean = true
49
51
) {
50
52
51
- val context = LocalContext .current
52
- val listener: OnColorChangedListener ? = onColorChanged
53
- val rowPadding = dimensionResource(R .dimen.compose_row_padding)
54
- val colPadding = dimensionResource(R .dimen.compose_col_padding)
53
+ /* Loading dimension resources from dimens.xml */
55
54
val hsvLabelMinWidth = dimensionResource(R .dimen.compose_hsv_label_min_width)
56
55
val hsvLabelPaddingEnd = dimensionResource(R .dimen.compose_hsv_label_padding_end)
57
56
val argbValueMinWidth = dimensionResource(R .dimen.compose_argb_value_min_width)
57
+ val rowPadding = dimensionResource(R .dimen.compose_row_padding)
58
+ val colPadding = dimensionResource(R .dimen.compose_col_padding)
58
59
59
- /* The initial value must be `initialColor` for all of them . */
60
+ /* All the values are being initialized by the `initialColor` . */
60
61
var currentColor: Int by remember { mutableStateOf(initialColor.hashCode()) }
61
62
var currentAlpha: Float by remember { mutableStateOf(initialColor.alpha) }
62
63
var currentHue: Float by remember { mutableStateOf(getHSV(initialColor)[0 ]) }
63
64
var currentSat: Float by remember { mutableStateOf(getHSV(initialColor)[1 ]) }
64
65
var currentVal: Float by remember { mutableStateOf(getHSV(initialColor)[2 ]) }
65
66
67
+ /* The measured position of the SatValPainter. */
66
68
var offsetSatVal by remember { mutableStateOf(Offset .Zero ) }
67
69
var sizeSatVal by remember { mutableStateOf(IntSize .Zero ) }
68
70
71
+ /* The measured position of the HuePainter. */
69
72
var offsetHue by remember { mutableStateOf(Offset .Zero ) }
70
73
var sizeHue by remember { mutableStateOf(IntSize .Zero ) }
71
74
75
+ /* The measured position of the AlphaPainter. */
72
76
var offsetAlpha by remember { mutableStateOf(Offset .Zero ) }
73
77
var sizeAlpha by remember { mutableStateOf(IntSize .Zero ) }
74
78
79
+ /* Callback listener */
80
+ val listener: OnColorChangedListener ? = onColorChanged
81
+
82
+ /* Composable LocalContext */
83
+ val context = LocalContext .current
84
+
75
85
Column (
76
86
horizontalAlignment = Alignment .CenterHorizontally ,
77
87
modifier = Modifier .fillMaxWidth()
@@ -242,14 +252,24 @@ fun ColorPickerComponent(
242
252
modifier = Modifier
243
253
.layoutId(SatVal )
244
254
.testTag(" sat_val" )
255
+ .pointerInput(Unit ) {
256
+ detectDragGestures(
257
+ onDrag = { inputChange: PointerInputChange , _: Offset ->
258
+ val rect: RectF = getLayoutBounds(offsetSatVal, sizeSatVal)
259
+ currentSat = pointToSat(rect, inputChange.position.x)
260
+ currentVal = pointToVal(rect, inputChange.position.y)
261
+ currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
262
+ }
263
+ )
264
+ }
245
265
.pointerInput(Unit ) {
246
266
detectTapGestures(
247
- onPress = { event ->
267
+ onPress = { offset : Offset ->
248
268
val rect: RectF = getLayoutBounds(offsetSatVal, sizeSatVal)
249
- currentSat = pointToSat(rect, event .x)
250
- currentVal = pointToVal(rect, event .y)
269
+ currentSat = pointToSat(rect, offset .x)
270
+ currentVal = pointToVal(rect, offset .y)
251
271
currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
252
- println (" Sat: $currentSat , Val: $currentVal (${event .x.toInt()} x ${event .y.toInt()} )" )
272
+ println (" Hue: $currentHue , Sat: $currentSat , Val: $currentVal (${offset .x.toInt()} x ${offset .y.toInt()} )" )
253
273
}
254
274
)
255
275
}
@@ -273,13 +293,22 @@ fun ColorPickerComponent(
273
293
modifier = Modifier
274
294
.layoutId(Hue )
275
295
.testTag(" hue" )
296
+ .pointerInput(Unit ) {
297
+ detectDragGestures(
298
+ onDrag = { inputChange: PointerInputChange , _: Offset ->
299
+ val rect: RectF = getLayoutBounds(offsetHue, sizeHue)
300
+ currentHue = pointToHue(rect, inputChange.position.y)
301
+ currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
302
+ }
303
+ )
304
+ }
276
305
.pointerInput(Unit ) {
277
306
detectTapGestures(
278
- onPress = { event ->
307
+ onPress = { offset : Offset ->
279
308
val rect: RectF = getLayoutBounds(offsetHue, sizeHue)
280
- currentHue = pointToHue(rect, event .y)
309
+ currentHue = pointToHue(rect, offset .y)
281
310
currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
282
- println (" Val: $currentHue (${event .y.toInt()} )" )
311
+ println (" Val: $currentHue (${offset .y.toInt()} )" )
283
312
}
284
313
)
285
314
}
@@ -309,13 +338,22 @@ fun ColorPickerComponent(
309
338
modifier = Modifier
310
339
.layoutId(Alpha )
311
340
.testTag(" alpha" )
341
+ .pointerInput(Unit ) {
342
+ detectDragGestures(
343
+ onDrag = { inputChange: PointerInputChange , _: Offset ->
344
+ val rect: RectF = getLayoutBounds(offsetAlpha, sizeAlpha)
345
+ currentAlpha = pointToAlpha(rect, inputChange.position.x)
346
+ currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
347
+ }
348
+ )
349
+ }
312
350
.pointerInput(Unit ) {
313
351
detectTapGestures(
314
- onPress = { event ->
352
+ onPress = { offset : Offset ->
315
353
val rect: RectF = getLayoutBounds(offsetAlpha, sizeAlpha)
316
- currentAlpha = pointToAlpha(rect, event .x)
354
+ currentAlpha = pointToAlpha(rect, offset .x)
317
355
currentColor = toIntColor(currentAlpha, currentHue, currentSat, currentVal)
318
- println (" Alpha: $currentAlpha (${event .x.toInt()} )" )
356
+ println (" Alpha: $currentAlpha (${offset .x.toInt()} )" )
319
357
}
320
358
)
321
359
}
@@ -367,7 +405,7 @@ fun ColorPickerComponent(
367
405
.padding(all = rowPadding)
368
406
.pointerInput(Unit ) {
369
407
detectTapGestures(
370
- onPress = {
408
+ onPress = { _ : Offset ->
371
409
/* reset to the previous color */
372
410
onButtonClick(context, OldColor , initialColor.hashCode(), listener)
373
411
currentColor = initialColor.hashCode()
@@ -405,7 +443,7 @@ fun ColorPickerComponent(
405
443
.padding(all = rowPadding)
406
444
.pointerInput(Unit ) {
407
445
detectTapGestures(
408
- onPress = {
446
+ onPress = { _ : Offset ->
409
447
onButtonClick(
410
448
context,
411
449
NewColor ,
0 commit comments