11package com.smarttoolfactory.colorpicker.picker
22
3- import androidx.compose.foundation.background
43import androidx.compose.foundation.layout.*
5- import androidx.compose.foundation.shape.RoundedCornerShape
64import androidx.compose.material.Text
75import androidx.compose.material.TextButton
86import androidx.compose.runtime.*
@@ -12,90 +10,84 @@ import androidx.compose.ui.graphics.Color
1210import androidx.compose.ui.text.font.FontWeight
1311import androidx.compose.ui.unit.dp
1412import androidx.compose.ui.unit.sp
15- import com.smarttoolfactory.colorpicker.argbToHex
16- import com.smarttoolfactory.colorpicker.colorToHSL
1713import com.smarttoolfactory.colorpicker.model.ColorHSL
1814import com.smarttoolfactory.colorpicker.model.ColorModel
1915import com.smarttoolfactory.colorpicker.selector.HueSelectorRing
20- import com.smarttoolfactory.colorpicker.selector.SLSelectorDiamondHSL
16+ import com.smarttoolfactory.colorpicker.selector.SaturationLightnessSelectorDiamondHSL
2117import com.smarttoolfactory.colorpicker.slider.CompositeSliderPanel
22- import com.smarttoolfactory.colorpicker.widget.drawChecker
18+ import com.smarttoolfactory.colorpicker.util.argbToHex
19+ import com.smarttoolfactory.colorpicker.util.colorToHSL
20+ import com.smarttoolfactory.colorpicker.widget.ColorDisplayRoundedRect
21+ import com.smarttoolfactory.colorpicker.widget.HexDisplay
2322
2423@Composable
25- fun ColorPickerHSL (modifier : Modifier = Modifier , initialColor : Color ) {
24+ fun ColorPickerRingDiamondHSL (
25+ modifier : Modifier = Modifier ,
26+ initialColor : Color ,
27+ outerRadiusFraction : Float = .9f,
28+ innerRadiusFraction : Float = .6f,
29+ onColorChange : (Color ) -> Unit
30+ ) {
31+
32+ var inputColorModel by remember { mutableStateOf(ColorModel .HSL ) }
33+
34+ val hslArray = colorToHSL(initialColor)
35+
36+ var hue by remember { mutableStateOf(hslArray[0 ]) }
37+ var saturation by remember { mutableStateOf(hslArray[1 ]) }
38+ var lightness by remember { mutableStateOf(hslArray[2 ]) }
39+ var alpha by remember { mutableStateOf(initialColor.alpha) }
40+
41+ val currentColor =
42+ Color .hsl(hue = hue, saturation = saturation, lightness = lightness, alpha = alpha)
43+
44+ onColorChange(currentColor)
45+
46+ var hexString by remember(currentColor) {
47+ mutableStateOf(
48+ argbToHex(
49+ currentColor.alpha,
50+ currentColor.red,
51+ currentColor.green,
52+ currentColor.blue
53+ )
54+ )
55+ }
56+
2657 Column (
2758 modifier = modifier,
28- horizontalAlignment = Alignment .CenterHorizontally
59+ horizontalAlignment = Alignment .CenterHorizontally ,
60+ verticalArrangement = Arrangement .Center
2961 ) {
3062
31- val hslArray = colorToHSL(initialColor)
32- var hue by remember { mutableStateOf(hslArray[0 ]) }
33- var saturation by remember { mutableStateOf(hslArray[1 ]) }
34- var lightness by remember { mutableStateOf(hslArray[2 ]) }
35- var alpha by remember { mutableStateOf(initialColor.alpha) }
36-
37- val currentColor =
38- Color .hsl(hue = hue, saturation = saturation, lightness = lightness, alpha = alpha)
39-
40- var hexString by remember { mutableStateOf(" " ) }
41-
42- hexString = argbToHex(
43- currentColor.alpha,
44- currentColor.red,
45- currentColor.green,
46- currentColor.blue
47- )
48-
49- var inputColorModel by remember { mutableStateOf(ColorModel .HSL ) }
50-
5163 Spacer (modifier = Modifier .height(10 .dp))
5264
5365 // Initial and Current Colors
54- Row (
66+ ColorDisplayRoundedRect (
5567 modifier = Modifier
5668 .fillMaxWidth()
57- .padding(horizontal = 50 .dp, vertical = 10 .dp)
58- ) {
69+ .padding(horizontal = 50 .dp, vertical = 10 .dp),
70+ initialColor = initialColor,
71+ currentColor = currentColor
72+ )
5973
60- Box (
61- modifier = Modifier
62- .weight(1f )
63- .height(40 .dp)
64- .background(
65- initialColor,
66- shape = RoundedCornerShape (topStart = 8 .dp, bottomStart = 8 .dp)
67- )
68- )
69- Box (
70- modifier = Modifier
71- .weight(1f )
72- .height(40 .dp)
73- .drawChecker(RoundedCornerShape (topEnd = 8 .dp, bottomEnd = 8 .dp))
74- .background(
75- currentColor,
76- shape = RoundedCornerShape (topEnd = 8 .dp, bottomEnd = 8 .dp)
77- )
78- )
79- }
8074
81- // ColorWheel for hue selection
82- // SaturationDiamond for saturation and lightness selections
83- Box (
84- modifier = Modifier ,
85- contentAlignment = Alignment .Center
86- ) {
75+ Box (contentAlignment = Alignment .Center ) {
8776
77+ // Ring Shaped Hue Selector
8878 HueSelectorRing (
89- modifier = Modifier .size( 350 .dp ),
79+ modifier = Modifier .fillMaxWidth( 1f ),
9080 hue = hue,
81+ outerRadiusFraction = outerRadiusFraction,
82+ innerRadiusFraction = innerRadiusFraction,
9183 selectionRadius = 8 .dp
9284 ) { hueChange ->
9385 hue = hueChange
94-
9586 }
9687
97- SLSelectorDiamondHSL (
98- modifier = Modifier .size(200 .dp),
88+ // Diamond Shaped Saturation and Lightness Selector
89+ SaturationLightnessSelectorDiamondHSL (
90+ modifier = Modifier .fillMaxWidth(innerRadiusFraction * .9f ),
9991 hue = hue,
10092 saturation = saturation,
10193 lightness = lightness,
@@ -104,9 +96,9 @@ fun ColorPickerHSL(modifier: Modifier = Modifier, initialColor: Color) {
10496 saturation = s
10597 lightness = l
10698 }
107-
10899 }
109100
101+ // HSL-HSV-RGB Color Model Change Tabs
110102 Row (
111103 modifier = Modifier .width(350 .dp),
112104 horizontalArrangement = Arrangement .SpaceEvenly
@@ -132,9 +124,8 @@ fun ColorPickerHSL(modifier: Modifier = Modifier, initialColor: Color) {
132124 }
133125 }
134126
135-
127+ // HSL-HSV-RGB Sliders
136128 CompositeSliderPanel (
137- modifier = Modifier .widthIn(340 .dp),
138129 compositeColor = ColorHSL (
139130 hue = hue,
140131 saturation = saturation,
@@ -155,13 +146,5 @@ fun ColorPickerHSL(modifier: Modifier = Modifier, initialColor: Color) {
155146 inputColorModel = inputColorModel,
156147 outputColorModel = ColorModel .HSL
157148 )
158-
159- Text (
160- text = hexString,
161- fontWeight = FontWeight .Bold ,
162- fontSize = 18 .sp,
163- color = currentColor
164- )
165-
166149 }
167150}
0 commit comments