Skip to content

Commit 987eb76

Browse files
add ColorMode for changing tabs
1 parent 3b796ba commit 987eb76

File tree

3 files changed

+92
-12
lines changed

3 files changed

+92
-12
lines changed

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/model/ColorModel.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ package com.smarttoolfactory.colorpicker.model
66
enum class ColorModel {
77
RGB, HSV, HSL
88
}
9+
10+
/**
11+
* Color Modes that contain HSL, HSV, RGB, and Gradient
12+
*/
13+
enum class ColorMode {
14+
HSL, HSV, RGB, Gradient
15+
}

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/widget/ColorModelChangeTabRow.kt

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package com.smarttoolfactory.colorpicker.widget
22

33
import androidx.compose.foundation.clickable
44
import androidx.compose.foundation.interaction.MutableInteractionSource
5-
import androidx.compose.foundation.layout.Arrangement
6-
import androidx.compose.foundation.layout.Box
7-
import androidx.compose.foundation.layout.Row
8-
import androidx.compose.foundation.layout.padding
5+
import androidx.compose.foundation.layout.*
96
import androidx.compose.material.Text
107
import androidx.compose.runtime.*
8+
import androidx.compose.ui.Alignment
119
import androidx.compose.ui.Modifier
1210
import androidx.compose.ui.text.font.FontWeight
1311
import androidx.compose.ui.unit.dp
1412
import androidx.compose.ui.unit.sp
13+
import com.smarttoolfactory.colorpicker.model.ColorMode
1514
import com.smarttoolfactory.colorpicker.model.ColorModel
1615
import com.smarttoolfactory.colorpicker.ui.Grey400
1716
import com.smarttoolfactory.colorpicker.ui.Grey600
@@ -69,4 +68,73 @@ fun ColorModelChangeTabRow(
6968
}
7069
}
7170

71+
/**
72+
* Tab Section for changing between HSL, HSV and RGB color models and Gradient color.
73+
* @param colorMode current color mode.
74+
* @param onColorModeChange callback that is triggered when user touches buttons.
75+
*/
76+
@Composable
77+
fun ColorGradientModeChangeTabRow(
78+
modifier: Modifier = Modifier,
79+
colorMode: ColorMode,
80+
onColorModeChange: (ColorMode) -> Unit
81+
) {
82+
var selectedIndex by remember {
83+
mutableStateOf(
84+
when (colorMode) {
85+
ColorMode.HSL -> 0
86+
ColorMode.HSV -> 1
87+
ColorMode.RGB -> 2
88+
ColorMode.Gradient -> 3
89+
}
90+
)
91+
}
92+
93+
Row(
94+
modifier = modifier
95+
.fillMaxWidth()
96+
.padding(horizontal = 10.dp),
97+
horizontalArrangement = Arrangement.SpaceEvenly,
98+
verticalAlignment = Alignment.CenterVertically
99+
) {
100+
modeList.forEachIndexed { index, s ->
101+
Box(
102+
modifier = Modifier
103+
.clickable(
104+
interactionSource = MutableInteractionSource(),
105+
indication = null,
106+
onClick = {
107+
selectedIndex = index
108+
val newColorMode = when (selectedIndex) {
109+
0 -> ColorMode.HSL
110+
1 -> ColorMode.HSV
111+
2 -> ColorMode.RGB
112+
else -> ColorMode.Gradient
113+
}
114+
onColorModeChange(newColorMode)
115+
}
116+
)
117+
.padding(10.dp),
118+
contentAlignment = Alignment.Center
119+
) {
120+
if (index != 3) {
121+
Text(
122+
text = s,
123+
fontWeight = FontWeight.Bold,
124+
fontSize = 18.sp,
125+
color = if (index == selectedIndex) Grey400 else Grey600
126+
)
127+
} else {
128+
ColorWheel(
129+
modifier = Modifier.size(18.dp),
130+
borderColor = if (index == selectedIndex) Grey400 else Grey600
131+
)
132+
}
133+
134+
}
135+
}
136+
}
137+
}
138+
72139
private val modelList = listOf("HSL", "HSV", "RGB")
140+
private val modeList = listOf("HSL", "HSV", "RGB", "Gradient")

colorpicker/src/main/java/com/smarttoolfactory/colorpicker/widget/ColorWheel.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.runtime.Composable
77
import androidx.compose.ui.Modifier
88
import androidx.compose.ui.geometry.Offset
99
import androidx.compose.ui.graphics.Brush
10+
import androidx.compose.ui.graphics.Color
1011
import androidx.compose.ui.graphics.drawscope.Stroke
1112
import androidx.compose.ui.unit.dp
1213
import com.smarttoolfactory.colorpicker.ui.gradientColorScaleRGB
@@ -16,7 +17,10 @@ import com.smarttoolfactory.colorpicker.ui.gradientColorScaleRGB
1617
* Simple circle with stroke to show rainbow colors as [Brush.sweepGradient]
1718
*/
1819
@Composable
19-
fun ColorWheel(modifier: Modifier = Modifier) {
20+
fun ColorWheel(
21+
modifier: Modifier = Modifier,
22+
borderColor: Color
23+
) {
2024

2125
Canvas(modifier = modifier.sizeIn(minWidth = 24.dp, minHeight = 24.dp)) {
2226
val canvasWidth = size.width
@@ -31,17 +35,18 @@ fun ColorWheel(modifier: Modifier = Modifier) {
3135
val cY = canvasHeight / 2
3236
val canvasRadius = canvasWidth.coerceAtMost(canvasHeight) / 2f
3337
val center = Offset(cX, cY)
34-
val strokeWidth = canvasRadius * .3f
35-
// Stroke is drawn out of the radius, so it's required to subtract stroke width from radius
36-
val radius = canvasRadius - strokeWidth
3738

3839
drawCircle(
3940
brush = Brush.sweepGradient(colors = gradientColorScaleRGB, center = center),
40-
radius = radius,
41+
radius = canvasRadius,
42+
center = center
43+
)
44+
45+
drawCircle(
46+
color = borderColor,
47+
radius = canvasRadius,
4148
center = center,
42-
style = Stroke(
43-
width = strokeWidth
44-
)
49+
style = Stroke(width = 2.dp.toPx())
4550
)
4651
}
4752
}

0 commit comments

Comments
 (0)