@@ -33,8 +33,6 @@ import io.syslogic.colorpicker.OnColorChangedListener
33
33
import io.syslogic.colorpicker.R
34
34
import io.syslogic.colorpicker.compose.LayoutId.*
35
35
36
- import java.util.*
37
-
38
36
/* *
39
37
* Jetpack Compose Color-Picker Component
40
38
*
@@ -54,11 +52,12 @@ fun ColorPickerComponent(
54
52
val rowPadding = dimensionResource(R .dimen.compose_row_padding)
55
53
val listener: OnColorChangedListener ? = onColorChanged
56
54
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) ) }
55
+ /* The initial value must be `initialColor` for all of them. */
56
+ var currentColor: Int by remember { mutableStateOf(initialColor.hashCode()) }
57
+ var currentAlpha: Float by remember { mutableStateOf(initialColor.alpha) }
58
+ var currentHue: Float by remember { mutableStateOf(getHSV(initialColor)[0 ]) }
59
+ var currentSat: Float by remember { mutableStateOf(getHSV(initialColor)[1 ]) }
60
+ var currentVal: Float by remember { mutableStateOf(getHSV(initialColor)[2 ]) }
62
61
63
62
var offsetSatVal by remember { mutableStateOf(Offset .Zero ) }
64
63
var sizeSatVal by remember { mutableStateOf(IntSize .Zero ) }
@@ -417,6 +416,13 @@ fun getLayoutBounds(position: Offset, size: IntSize): RectF {
417
416
)
418
417
}
419
418
419
+ /* *
420
+ * On button click.
421
+ * @param context the Context of the Composable
422
+ * @param layoutId either OldColor or NewColor
423
+ * @param value the selected integer color value
424
+ * @param listener {link OnColorChangedListener} or null
425
+ */
420
426
fun onButtonClick (context : Context , layoutId : LayoutId , value : Int , listener : OnColorChangedListener ? ) {
421
427
val message: String
422
428
when (layoutId) {
@@ -433,55 +439,44 @@ fun onButtonClick(context: Context, layoutId: LayoutId, value: Int, listener: On
433
439
println (message)
434
440
}
435
441
436
- fun toIntColor (alpha : Float , hue : Float , sat : Float , value : Float ): Int {
437
- println (" alpha: $alpha , hue: $hue , saturation: $sat , value: $value " )
438
- return HSVToColor (
439
- (alpha * 255 ).toInt(), floatArrayOf(hue, sat, value)
440
- )
441
- }
442
-
443
442
/* *
444
- * @param color the color value to convert.
443
+ * Initialize H/S/V "Float by remember" with a {@link Color}.
444
+ * @param value the color to convert.
445
+ * @return float-array HSV.
445
446
*/
446
- fun toARGB (color : Int ): String {
447
- var alpha = Integer .toHexString(Color (color).alpha.times(255 ).toInt()).uppercase(Locale .ROOT )
448
- var red = Integer .toHexString(Color (color).red.times(255 ).toInt()).uppercase(Locale .ROOT )
449
- var green = Integer .toHexString(Color (color).green.times(255 ).toInt()).uppercase(Locale .ROOT )
450
- var blue = Integer .toHexString(Color (color).blue.times(255 ).toInt()).uppercase(Locale .ROOT )
451
- if (alpha.length == 1 ) {alpha = String .format(" 0%s" , alpha)}
452
- if (red.length == 1 ) {red = String .format(" 0%s" , red)}
453
- if (green.length == 1 ) {green = String .format(" 0%s" , green)}
454
- if (blue.length == 1 ) {blue = String .format(" 0%s" , blue)}
455
- return String .format(" #%s%s%s%s" , alpha, red, green, blue)
456
- }
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
447
fun getHSV (value : Color ): FloatArray {
467
448
val hsv = FloatArray (3 )
468
- RGBToHSV (value.red.times(255 ).toInt(), value.green.times(255 ).toInt(), value.blue.times(255 ).toInt(), hsv)
449
+ val red = value.red.times(255 ).toInt()
450
+ val green = value.green.times(255 ).toInt()
451
+ val blue = value.blue.times(255 ).toInt()
452
+ RGBToHSV (red, green, blue, hsv)
469
453
return hsv
470
454
}
471
455
472
- fun getHueValue (value : Color ): Float {
473
- return getHSV(value)[0 ]
474
- }
475
-
476
- fun getSatValue (value : Color ): Float {
477
- return getHSV(value)[1 ]
456
+ /* *
457
+ * Convert H/S/V to Int.
458
+ * @param alpha the color to convert.
459
+ * @param hue the color to convert.
460
+ * @param sat the color to convert.
461
+ * @param value the color to convert.
462
+ * @return integer color value.
463
+ */
464
+ fun toIntColor (alpha : Float , hue : Float , sat : Float , value : Float ): Int {
465
+ println (" alpha: $alpha , hue: $hue , saturation: $sat , value: $value " )
466
+ return HSVToColor ((alpha * 255 ).toInt(), floatArrayOf(hue, sat, value))
478
467
}
479
468
480
- fun getValValue (value : Color ): Float {
481
- return getHSV(value)[2 ]
469
+ /* *
470
+ * Convert Int to A/R/G/B.
471
+ * @param color the color value to convert.
472
+ * @return an ARGB hexadecimal string.
473
+ */
474
+ fun toARGB (color : Int ): String {
475
+ return String .format(" #%1$02X" , color)
482
476
}
483
477
484
478
/* *
479
+ * Alpha channel in numeric notation 0-255.
485
480
* @param color the color value to convert.
486
481
* @return channel integer value as String.
487
482
*/
@@ -490,6 +485,7 @@ fun getAlphaChannel(color: Int): String {
490
485
}
491
486
492
487
/* *
488
+ * Red channel in numeric notation 0-255.
493
489
* @param color the color value to convert.
494
490
* @return channel integer value as String.
495
491
*/
@@ -498,6 +494,7 @@ fun getRedChannel(color: Int): String {
498
494
}
499
495
500
496
/* *
497
+ * Green channel in numeric notation 0-255.
501
498
* @param color the color value to convert.
502
499
* @return channel integer value as String.
503
500
*/
@@ -506,6 +503,7 @@ fun getGreenChannel(color: Int): String {
506
503
}
507
504
508
505
/* *
506
+ * Blue channel in numeric notation 0-255.
509
507
* @param color the color value to convert.
510
508
* @return channel integer value as String.
511
509
*/
@@ -515,35 +513,35 @@ fun getBlueChannel(color: Int): String {
515
513
516
514
/* *
517
515
* @param rect boundaries of the hue rectangle.
518
- * @param y the value on the y axis.
519
- * @return float hue.
516
+ * @param y the relative position on the y axis.
517
+ * @return hue 0.0F to 360.0F .
520
518
*/
521
519
private fun pointToHue (rect : RectF , y : Float ): Float {
522
520
return 360f - y * 360f / rect.height()
523
521
}
524
522
525
523
/* *
526
524
* @param rect boundaries of the sat/val rectangle.
527
- * @param x the value on the x axis.
528
- * @return float sat.
525
+ * @param x the relative position on the x axis.
526
+ * @return sat 0.0F to 1.0F .
529
527
*/
530
528
private fun pointToSat (rect : RectF , x : Float ): Float {
531
529
return 1f / rect.width() * x
532
530
}
533
531
534
532
/* *
535
533
* @param rect boundaries of the sat/val rectangle.
536
- * @param y the value on the x axis.
537
- * @return float value.
534
+ * @param y the relative position on the x axis.
535
+ * @return value 0.0F to 1.0F .
538
536
*/
539
537
private fun pointToVal (rect : RectF , y : Float ): Float {
540
538
return 1f - 1f / rect.height() * y
541
539
}
542
540
543
541
/* *
544
542
* @param rect boundaries of the alpha rectangle.
545
- * @param x the value on the x axis.
546
- * @return float alpha.
543
+ * @param x the relative position on the x axis.
544
+ * @return alpha 0.0F to 1.0F .
547
545
*/
548
546
private fun pointToAlpha (rect : RectF , x : Float ): Float {
549
547
return x / rect.width()
0 commit comments