11package com.smarttoolfactory.composecolorpicker.demo
22
3- import androidx.compose.foundation.background
4- import androidx.compose.foundation.layout.*
3+ import androidx.compose.foundation.layout.Column
4+ import androidx.compose.foundation.layout.fillMaxSize
5+ import androidx.compose.foundation.layout.padding
56import androidx.compose.foundation.rememberScrollState
6- import androidx.compose.foundation.shape.RoundedCornerShape
77import androidx.compose.foundation.verticalScroll
8- import androidx.compose.material.Text
9- import androidx.compose.material.TextButton
10- import androidx.compose.runtime.*
11- import androidx.compose.ui.Alignment
8+ import androidx.compose.runtime.Composable
9+ import androidx.compose.runtime.getValue
10+ import androidx.compose.runtime.mutableStateOf
11+ import androidx.compose.runtime.remember
1212import androidx.compose.ui.Modifier
1313import androidx.compose.ui.graphics.Color
14- import androidx.compose.ui.text.font.FontWeight
1514import androidx.compose.ui.unit.dp
16- import androidx.compose.ui.unit.sp
17- import com.smarttoolfactory.colorpicker.argbToHex
18- import com.smarttoolfactory.colorpicker.model.ColorHSL
19- import com.smarttoolfactory.colorpicker.model.ColorModel
20- import com.smarttoolfactory.colorpicker.selector.HueSelectorRing
21- import com.smarttoolfactory.colorpicker.selector.SLSelectorFromHSLDiamond
22- import com.smarttoolfactory.colorpicker.slider.CompositeSliderPanel
23- import com.smarttoolfactory.colorpicker.widget.drawChecker
15+ import com.smarttoolfactory.colorpicker.picker.ColorPickerHSL
2416
2517@Composable
2618fun ColorPickerDemo () {
@@ -31,152 +23,10 @@ fun ColorPickerDemo() {
3123 .padding(8 .dp)
3224 .verticalScroll(rememberScrollState())
3325 ) {
34- val initialColor = Color ( 0xffccaa12 )
26+ val initialColor = Color .hsl( 0f , 0.5f , 0.5f )
3527
3628 val showColorDialog by remember { mutableStateOf(false ) }
3729 ColorPickerHSL (initialColor = initialColor)
3830 }
3931}
4032
41- @Composable
42- private fun ColorPickerHSL (initialColor : Color ) {
43- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
44-
45-
46- var hue by remember { mutableStateOf(0f ) }
47- var saturation by remember { mutableStateOf(.5f ) }
48- var lightness by remember { mutableStateOf(.5f ) }
49- var alpha by remember { mutableStateOf(1f ) }
50-
51- val currentColor =
52- Color .hsl(hue = hue, saturation = saturation, lightness = lightness, alpha = alpha)
53-
54- var hexString by remember {
55- mutableStateOf(
56- argbToHex(
57- currentColor.alpha,
58- currentColor.red,
59- currentColor.green,
60- currentColor.blue
61- )
62- )
63- }
64-
65- var inputColorModel by remember { mutableStateOf(ColorModel .HSL ) }
66-
67- Spacer (modifier = Modifier .height(10 .dp))
68-
69- // Initial and Current Colors
70- Row (
71- modifier = Modifier
72- .fillMaxWidth()
73- .padding(horizontal = 50 .dp, vertical = 10 .dp)
74- ) {
75-
76- Box (
77- modifier = Modifier
78- .weight(1f )
79- .height(40 .dp)
80- .background(
81- initialColor,
82- shape = RoundedCornerShape (topStart = 8 .dp, bottomStart = 8 .dp)
83- )
84- )
85- Box (
86- modifier = Modifier
87- .weight(1f )
88- .height(40 .dp)
89- .drawChecker(RoundedCornerShape (topEnd = 8 .dp, bottomEnd = 8 .dp))
90- .background(
91- currentColor,
92- shape = RoundedCornerShape (topEnd = 8 .dp, bottomEnd = 8 .dp)
93- )
94- )
95- }
96-
97- // ColorWheel for hue selection
98- // SaturationDiamond for saturation and lightness selections
99- Box (
100- modifier = Modifier ,
101- contentAlignment = Alignment .Center
102- ) {
103-
104- HueSelectorRing (
105- modifier = Modifier
106- .width(350 .dp)
107- .aspectRatio(1f ),
108- hue = hue,
109- selectionRadius = 8 .dp
110- ) { hueChange ->
111- hue = hueChange.toFloat()
112- }
113-
114- Column {
115- SLSelectorFromHSLDiamond (
116- modifier = Modifier .size(200 .dp),
117- hue = hue,
118- saturation = saturation,
119- lightness = lightness,
120- selectionRadius = 8 .dp
121- ) { s, l ->
122- saturation = s
123- lightness = l
124- }
125- }
126- }
127-
128- Row (modifier = Modifier .width(350 .dp), horizontalArrangement = Arrangement .SpaceEvenly ) {
129- TextButton (
130- onClick = { inputColorModel = ColorModel .HSL },
131- modifier = Modifier .weight(1f )
132- ) {
133- Text (text = " HSL" , fontWeight = FontWeight .Bold )
134- }
135- TextButton (
136- onClick = { inputColorModel = ColorModel .HSV },
137- modifier = Modifier .weight(1f )
138- ) {
139- Text (text = " HSV" , fontWeight = FontWeight .Bold )
140- }
141-
142- TextButton (
143- onClick = { inputColorModel = ColorModel .RGB },
144- modifier = Modifier .weight(1f )
145- ) {
146- Text (text = " RGB" , fontWeight = FontWeight .Bold )
147- }
148- }
149-
150-
151- CompositeSliderPanel (
152- modifier = Modifier .widthIn(340 .dp),
153- compositeColor = ColorHSL (
154- hue = hue,
155- saturation = saturation,
156- lightness = lightness,
157- alpha = alpha
158- ),
159- onColorChange = {
160- (it as ? ColorHSL )?.let { color ->
161- hue = color.hue
162- saturation = color.saturation
163- lightness = color.lightness
164- alpha = color.alpha
165- hexString = color.argbHexString
166- }
167-
168- },
169- showAlphaSlider = true ,
170- inputColorModel = inputColorModel,
171- outputColorModel = ColorModel .HSL
172- )
173-
174- Text (
175- text = hexString,
176- fontWeight = FontWeight .Bold ,
177- fontSize = 18 .sp,
178- color = currentColor
179- )
180-
181- }
182- }
0 commit comments