1
1
package io.syslogic.colorpicker.compose
2
2
3
3
import android.content.Context
4
+ import android.graphics.Color.HSVToColor
5
+ import android.graphics.Color.RGBToHSV
4
6
import android.graphics.RectF
5
7
import android.widget.Toast
6
8
@@ -43,20 +45,20 @@ fun ColorPickerComponent(
43
45
initialColor : Color = Color .Transparent ,
44
46
onColorChanged : OnColorChangedListener ? ,
45
47
showAlpha : Boolean = true,
46
- showARGB : Boolean = true,
47
48
showHSV : Boolean = true,
49
+ showARGB : Boolean = true,
48
50
showHex : Boolean = true
49
51
) {
50
52
51
53
val context = LocalContext .current
52
54
val rowPadding = dimensionResource(R .dimen.compose_row_padding)
53
55
val listener: OnColorChangedListener ? = onColorChanged
54
56
55
- var currentColor: Int by remember { mutableStateOf(initialColor.hashCode() ) }
56
- var currentAlpha: Float by remember { mutableStateOf(. 5F ) }
57
- var currentSat: Float by remember { mutableStateOf(. 5F ) }
58
- var currentVal: Float by remember { mutableStateOf(. 5F ) }
59
- var currentHue: Float by remember { mutableStateOf(180F ) }
57
+ var currentColor: Int by remember { mutableStateOf( getColorValue(initialColor) ) }
58
+ var currentAlpha: Float by remember { mutableStateOf( getAlphaValue(initialColor) ) }
59
+ var currentSat: Float by remember { mutableStateOf( getSatValue(initialColor) ) }
60
+ var currentVal: Float by remember { mutableStateOf( getValValue(initialColor) ) }
61
+ var currentHue: Float by remember { mutableStateOf( getHueValue(initialColor) ) }
60
62
61
63
var offsetSatVal by remember { mutableStateOf(Offset .Zero ) }
62
64
var sizeSatVal by remember { mutableStateOf(IntSize .Zero ) }
@@ -224,9 +226,9 @@ fun ColorPickerComponent(
224
226
contentScale = ContentScale .FillBounds ,
225
227
painter = SatValPainter (Size (900F , 900F )).also {
226
228
it.setAlpha(currentAlpha)
229
+ it.setHue(currentHue)
227
230
it.setSat(currentSat)
228
231
it.setValue(currentVal)
229
- it.setHue(currentHue)
230
232
},
231
233
modifier = Modifier
232
234
.layoutId(SatVal )
@@ -433,7 +435,7 @@ fun onButtonClick(context: Context, layoutId: LayoutId, value: Int, listener: On
433
435
434
436
fun toIntColor (alpha : Float , hue : Float , sat : Float , value : Float ): Int {
435
437
println (" alpha: $alpha , hue: $hue , saturation: $sat , value: $value " )
436
- return android.graphics. Color . HSVToColor (
438
+ return HSVToColor (
437
439
(alpha * 255 ).toInt(), floatArrayOf(hue, sat, value)
438
440
)
439
441
}
@@ -453,6 +455,32 @@ fun toARGB(color: Int): String {
453
455
return String .format(" #%s%s%s%s" , alpha, red, green, blue)
454
456
}
455
457
458
+ fun getColorValue (value : Color ): Int {
459
+ return value.hashCode()
460
+ }
461
+
462
+ fun getAlphaValue (value : Color ): Float {
463
+ return value.alpha
464
+ }
465
+
466
+ fun getHSV (value : Color ): FloatArray {
467
+ val hsv = FloatArray (3 )
468
+ RGBToHSV (value.red.times(255 ).toInt(), value.green.times(255 ).toInt(), value.blue.times(255 ).toInt(), hsv)
469
+ return hsv
470
+ }
471
+
472
+ fun getHueValue (value : Color ): Float {
473
+ return getHSV(value)[0 ]
474
+ }
475
+
476
+ fun getSatValue (value : Color ): Float {
477
+ return getHSV(value)[1 ]
478
+ }
479
+
480
+ fun getValValue (value : Color ): Float {
481
+ return getHSV(value)[2 ]
482
+ }
483
+
456
484
/* *
457
485
* @param color the color value to convert.
458
486
* @return channel integer value as String.
0 commit comments