Skip to content

Commit 41afdd0

Browse files
committed
code cleanup; comments added.
1 parent 70d28f3 commit 41afdd0

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

library/src/main/java/io/syslogic/colorpicker/compose/ColorPickerComponent.kt

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import io.syslogic.colorpicker.OnColorChangedListener
3333
import io.syslogic.colorpicker.R
3434
import io.syslogic.colorpicker.compose.LayoutId.*
3535

36-
import java.util.*
37-
3836
/**
3937
* Jetpack Compose Color-Picker Component
4038
*
@@ -54,11 +52,12 @@ fun ColorPickerComponent(
5452
val rowPadding = dimensionResource(R.dimen.compose_row_padding)
5553
val listener: OnColorChangedListener? = onColorChanged
5654

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]) }
6261

6362
var offsetSatVal by remember { mutableStateOf(Offset.Zero) }
6463
var sizeSatVal by remember { mutableStateOf(IntSize.Zero) }
@@ -417,6 +416,13 @@ fun getLayoutBounds(position: Offset, size: IntSize): RectF {
417416
)
418417
}
419418

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+
*/
420426
fun onButtonClick(context: Context, layoutId: LayoutId, value: Int, listener: OnColorChangedListener?) {
421427
val message: String
422428
when (layoutId) {
@@ -433,55 +439,44 @@ fun onButtonClick(context: Context, layoutId: LayoutId, value: Int, listener: On
433439
println(message)
434440
}
435441

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-
443442
/**
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.
445446
*/
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-
466447
fun getHSV(value: Color): FloatArray {
467448
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)
469453
return hsv
470454
}
471455

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))
478467
}
479468

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)
482476
}
483477

484478
/**
479+
* Alpha channel in numeric notation 0-255.
485480
* @param color the color value to convert.
486481
* @return channel integer value as String.
487482
*/
@@ -490,6 +485,7 @@ fun getAlphaChannel(color: Int): String {
490485
}
491486

492487
/**
488+
* Red channel in numeric notation 0-255.
493489
* @param color the color value to convert.
494490
* @return channel integer value as String.
495491
*/
@@ -498,6 +494,7 @@ fun getRedChannel(color: Int): String {
498494
}
499495

500496
/**
497+
* Green channel in numeric notation 0-255.
501498
* @param color the color value to convert.
502499
* @return channel integer value as String.
503500
*/
@@ -506,6 +503,7 @@ fun getGreenChannel(color: Int): String {
506503
}
507504

508505
/**
506+
* Blue channel in numeric notation 0-255.
509507
* @param color the color value to convert.
510508
* @return channel integer value as String.
511509
*/
@@ -515,35 +513,35 @@ fun getBlueChannel(color: Int): String {
515513

516514
/**
517515
* @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.
520518
*/
521519
private fun pointToHue(rect: RectF, y: Float): Float {
522520
return 360f - y * 360f / rect.height()
523521
}
524522

525523
/**
526524
* @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.
529527
*/
530528
private fun pointToSat(rect: RectF, x: Float): Float {
531529
return 1f / rect.width() * x
532530
}
533531

534532
/**
535533
* @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.
538536
*/
539537
private fun pointToVal(rect: RectF, y: Float): Float {
540538
return 1f - 1f / rect.height() * y
541539
}
542540

543541
/**
544542
* @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.
547545
*/
548546
private fun pointToAlpha(rect: RectF, x: Float): Float {
549547
return x / rect.width()

0 commit comments

Comments
 (0)