Skip to content

Commit 0439c47

Browse files
committed
code reduced.
1 parent fafbdf4 commit 0439c47

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

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

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fun ColorPickerComponent(
164164
modifier = Modifier.testTag("text_label_alpha")
165165
)
166166
Text(
167-
text = getAlphaChannel(currentColor),
167+
text = getChannelValue(currentColor, "alpha"),
168168
textAlign = TextAlign.End,
169169
modifier = Modifier
170170
.defaultMinSize(minWidth = argbValueMinWidth)
@@ -180,7 +180,7 @@ fun ColorPickerComponent(
180180
modifier = Modifier.testTag("text_label_Blue")
181181
)
182182
Text(
183-
text = getBlueChannel(currentColor),
183+
text = getChannelValue(currentColor, "blue"),
184184
textAlign = TextAlign.End,
185185
modifier = Modifier
186186
.defaultMinSize(minWidth = argbValueMinWidth)
@@ -196,7 +196,7 @@ fun ColorPickerComponent(
196196
modifier = Modifier.testTag("text_label_red")
197197
)
198198
Text(
199-
text = getRedChannel(currentColor),
199+
text = getChannelValue(currentColor, "red"),
200200
textAlign = TextAlign.End,
201201
modifier = Modifier
202202
.defaultMinSize(minWidth = argbValueMinWidth)
@@ -212,7 +212,7 @@ fun ColorPickerComponent(
212212
modifier = Modifier.testTag("text_label_green")
213213
)
214214
Text(
215-
text = getGreenChannel(currentColor),
215+
text = getChannelValue(currentColor, "green"),
216216
textAlign = TextAlign.End,
217217
modifier = Modifier
218218
.defaultMinSize(minWidth = argbValueMinWidth)
@@ -493,37 +493,18 @@ fun toARGB(color: Int): String {
493493
/**
494494
* Alpha channel in numeric notation 0-255.
495495
* @param color the color value to convert.
496+
* @param name the name of the channel.
496497
* @return channel integer value as String.
497498
*/
498-
fun getAlphaChannel(color: Int): String {
499-
return String.format("%s", Color(color).alpha.times(255).toInt())
500-
}
501-
502-
/**
503-
* Red channel in numeric notation 0-255.
504-
* @param color the color value to convert.
505-
* @return channel integer value as String.
506-
*/
507-
fun getRedChannel(color: Int): String {
508-
return String.format("%s", Color(color).red.times(255).toInt())
509-
}
510-
511-
/**
512-
* Green channel in numeric notation 0-255.
513-
* @param color the color value to convert.
514-
* @return channel integer value as String.
515-
*/
516-
fun getGreenChannel(color: Int): String {
517-
return String.format("%s", Color(color).green.times(255).toInt())
518-
}
519-
520-
/**
521-
* Blue channel in numeric notation 0-255.
522-
* @param color the color value to convert.
523-
* @return channel integer value as String.
524-
*/
525-
fun getBlueChannel(color: Int): String {
526-
return String.format("%s", Color(color).blue.times(255).toInt())
499+
fun getChannelValue(color: Int, name: String): String {
500+
val value: Float = when (name) {
501+
"alpha" -> { Color(color).alpha }
502+
"red" -> { Color(color).red }
503+
"green" -> { Color(color).green }
504+
"blue" -> { Color(color).blue }
505+
else -> { return "" }
506+
}
507+
return String.format("%s", value.times(255).toInt())
527508
}
528509

529510
/**

0 commit comments

Comments
 (0)