@@ -13,12 +13,15 @@ import androidx.compose.foundation.layout.height
1313import androidx.compose.foundation.layout.padding
1414import androidx.compose.foundation.layout.width
1515import androidx.compose.foundation.shape.RoundedCornerShape
16+ import androidx.compose.foundation.text.BasicTextField
17+ import androidx.compose.foundation.text.KeyboardOptions
1618import androidx.compose.material3.Icon
1719import androidx.compose.material3.MaterialTheme
1820import androidx.compose.material3.OutlinedTextField
1921import androidx.compose.material3.Slider
2022import androidx.compose.material3.SliderDefaults
2123import androidx.compose.material3.Text
24+ import androidx.compose.material3.TextFieldDefaults
2225import androidx.compose.runtime.Composable
2326import androidx.compose.runtime.MutableState
2427import androidx.compose.runtime.getValue
@@ -32,6 +35,8 @@ import androidx.compose.ui.graphics.RectangleShape
3235import androidx.compose.ui.platform.LocalClipboardManager
3336import androidx.compose.ui.res.painterResource
3437import androidx.compose.ui.res.stringResource
38+ import androidx.compose.ui.text.TextStyle
39+ import androidx.compose.ui.text.input.KeyboardType
3540import androidx.compose.ui.text.input.TextFieldValue
3641import androidx.compose.ui.text.style.TextAlign
3742import androidx.compose.ui.unit.dp
@@ -40,7 +45,6 @@ import com.kavi.droid.color.palette.KvColorPalette
4045import com.kavi.droid.color.palette.model.KvColor
4146import com.kavi.droid.color.palette.util.ColorUtil
4247import com.kavi.droid.color.picker.R
43- import com.kavi.droid.color.picker.extension.toColorRangeInt
4448
4549/* *
4650 * A composable function that creates a slider for adjusting a float value associated with a color.
@@ -53,6 +57,9 @@ import com.kavi.droid.color.picker.extension.toColorRangeInt
5357 */
5458@Composable
5559internal fun ColorSlider (colorLabel : String , colorValueState : MutableState <Float >, color : Color ) {
60+
61+ val sliderValue = remember { mutableStateOf(TextFieldValue (" 0" )) }
62+
5663 /* *
5764 * Displays a slider for adjusting the given [colorValueState] associated with the provided [colorLabel].
5865 * The slider's active track color is set to [color].
@@ -69,22 +76,61 @@ internal fun ColorSlider(colorLabel: String, colorValueState: MutableState<Float
6976 )
7077 Slider (
7178 value = colorValueState.value,
72- onValueChange = colorValueState.component2(),
79+ onValueChange = { newValue ->
80+ colorValueState.value = newValue
81+ sliderValue.value = TextFieldValue (toColorRange(colorValueState.value).toString())
82+ },
7383 colors = SliderDefaults .colors(
7484 thumbColor = MaterialTheme .colorScheme.primary,
7585 activeTrackColor = color
7686 ),
7787 modifier = Modifier .weight(.8f )
7888 )
79- Text (
80- text = colorValueState.value.toColorRangeInt().toString(),
89+ Box (
8190 modifier = Modifier
82- .width(25 .dp)
83- .weight(.1f ),
84- textAlign = TextAlign .End ,
85- style = MaterialTheme .typography.bodySmall,
86- color = Color .Black
87- )
91+ .width(80 .dp)
92+ .weight(.2f )
93+ .border(1 .dp, MaterialTheme .colorScheme.primary, RoundedCornerShape (20 ))
94+ ) {
95+ BasicTextField (
96+ value = sliderValue.value,
97+ onValueChange = {
98+ if (it.text != " " && it.text.toInt() > 255 )
99+ sliderValue.value = TextFieldValue (" 255" )
100+ else
101+ sliderValue.value = it
102+
103+ colorValueState.value = sliderRange(sliderValue.value.text)
104+ },
105+ modifier = Modifier
106+ .width(80 .dp)
107+ .padding(5 .dp),
108+ textStyle = TextStyle (fontSize = 12 .sp, color = Color .Black , textAlign = TextAlign .Center ),
109+ maxLines = 1 ,
110+ keyboardOptions = KeyboardOptions (keyboardType = KeyboardType .Number )
111+ )
112+ }
113+
114+ }
115+ }
116+
117+ /* *
118+ * Converts a float value in the range [0, 1] to an integer color component in the range [0, 255].
119+ *
120+ * @return The integer representation of the color component.
121+ */
122+ private fun toColorRange (value : Float ): Int = (value * 255 + 0.5f ).toInt()
123+
124+ /* *
125+ * Converts a string value in the range [0, 255] to a float value of slider in tha range of [0, 1]
126+ *
127+ * @return The float representation of the slider component
128+ */
129+ private fun sliderRange (value : String ): Float {
130+ return if (value == " " ) {
131+ 0f
132+ } else {
133+ (value.toFloat() / 255 )
88134 }
89135}
90136
@@ -126,16 +172,16 @@ internal fun AlphaSlider(alphaValueState: MutableState<Float>, color: Color) {
126172 text = DecimalFormat (" #.##" ).format(alphaValueState.value).toString(),
127173 modifier = Modifier
128174 .width(25 .dp)
129- .weight(.1f ),
130- textAlign = TextAlign .End ,
175+ .weight(.2f ),
176+ textAlign = TextAlign .Center ,
131177 style = MaterialTheme .typography.bodySmall,
132178 color = Color .Black
133179 )
134180 }
135181}
136182
137183/* *
138- * A composable function that creates a slider for adjusting a float value associated with a color alpha valuw .
184+ * A composable function that creates a slider for adjusting a float value associated with a color alpha value .
139185 *
140186 * @param label: String: The label to display alongside the slider.
141187 * @param valueState: MutableState<Float>: The mutable state holding the current value of the slider.
0 commit comments