Skip to content

Commit 29357b5

Browse files
authored
Merge pull request #9 from KvColorPalette/feature/make-editable-value-in-pickers
Feature/make editable value in pickers
2 parents bf2089c + d305eb7 commit 29357b5

File tree

5 files changed

+66
-31
lines changed

5 files changed

+66
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ Add the following dependency to your `build.gradle` / `build.gradle.kts` file:
2626
For Groovy - `build.gradle`:
2727
````
2828
dependencies {
29-
implementation 'com.github.KvColorPalette:KvColorPicker-Android:2.0.0'
29+
implementation 'com.github.KvColorPalette:KvColorPicker-Android:2.1.0'
3030
}
3131
````
3232
For Kotlin DSL - `build.gradle.kts`:
3333
````
3434
dependencies {
35-
implementation("com.github.KvColorPalette:KvColorPicker-Android:2.0.0")
35+
implementation("com.github.KvColorPalette:KvColorPicker-Android:2.1.0")
3636
}
3737
````
3838

gradle/version-catalog/libs.versions.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[versions]
22
minSdkVersion = "26"
33
compilerSdkVersion = "35"
4-
targetSdkVersion = "35"
54
jvmVersion = "21"
65

76
agp = "8.7.3"
@@ -12,13 +11,12 @@ junit = "4.13.2"
1211
junitVersion = "1.2.1"
1312
espressoCore = "3.6.1"
1413
lifecycleRuntimeKtx = "2.8.7"
15-
activityCompose = "1.10.0"
16-
composeBom = "2025.01.00"
14+
activityCompose = "1.10.1"
15+
composeBom = "2025.02.00"
1716
appcompat = "1.7.0"
1817
material = "1.12.0"
19-
composeMaterial = "1.7.6"
20-
composeNavigation = "2.8.5"
21-
kvColorPalette = "2.1.0"
18+
composeMaterial = "1.7.8"
19+
kvColorPalette = "2.1.1"
2220

2321
[libraries]
2422
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -36,7 +34,6 @@ androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit
3634
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
3735
androidx-compose-material = { group = "androidx.compose.material", name = "material", version.ref = "composeMaterial"}
3836
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
39-
androidx-navigation-compose = { group = "androidx.navigation", name = "navigation-compose", version.ref = "composeNavigation" }
4037

4138
kv-color-palette = { group = "com.github.KvColorPalette", name = "KvColorPalette-Android", version.ref = "kvColorPalette" }
4239

kv-color-picker/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
kvColorPaletteGroupId=com.github.KvColorPalette
22
kvColorPickerArtifactId=KvColorPicker-Android
3-
kvColorPickerVersion=2.0.0
3+
kvColorPickerVersion=2.1.0

kv-color-picker/src/main/kotlin/com/kavi/droid/color/picker/extension/Extension.kt

Lines changed: 0 additions & 8 deletions
This file was deleted.

kv-color-picker/src/main/kotlin/com/kavi/droid/color/picker/ui/common/CommonUI.kt

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import androidx.compose.foundation.layout.height
1313
import androidx.compose.foundation.layout.padding
1414
import androidx.compose.foundation.layout.width
1515
import androidx.compose.foundation.shape.RoundedCornerShape
16+
import androidx.compose.foundation.text.BasicTextField
17+
import androidx.compose.foundation.text.KeyboardOptions
1618
import androidx.compose.material3.Icon
1719
import androidx.compose.material3.MaterialTheme
1820
import androidx.compose.material3.OutlinedTextField
1921
import androidx.compose.material3.Slider
2022
import androidx.compose.material3.SliderDefaults
2123
import androidx.compose.material3.Text
24+
import androidx.compose.material3.TextFieldDefaults
2225
import androidx.compose.runtime.Composable
2326
import androidx.compose.runtime.MutableState
2427
import androidx.compose.runtime.getValue
@@ -32,6 +35,8 @@ import androidx.compose.ui.graphics.RectangleShape
3235
import androidx.compose.ui.platform.LocalClipboardManager
3336
import androidx.compose.ui.res.painterResource
3437
import androidx.compose.ui.res.stringResource
38+
import androidx.compose.ui.text.TextStyle
39+
import androidx.compose.ui.text.input.KeyboardType
3540
import androidx.compose.ui.text.input.TextFieldValue
3641
import androidx.compose.ui.text.style.TextAlign
3742
import androidx.compose.ui.unit.dp
@@ -40,7 +45,6 @@ import com.kavi.droid.color.palette.KvColorPalette
4045
import com.kavi.droid.color.palette.model.KvColor
4146
import com.kavi.droid.color.palette.util.ColorUtil
4247
import 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
5559
internal 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

Comments
 (0)