Skip to content

Commit ab9d452

Browse files
update Material2 Color Selection demo with accent colors
1 parent 72551e6 commit ab9d452

File tree

6 files changed

+123
-110
lines changed

6 files changed

+123
-110
lines changed

app/src/main/java/com/smarttoolfactory/composecolorsextended/MainActivity.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.activity.compose.setContent
66
import androidx.compose.animation.ExperimentalAnimationApi
77
import androidx.compose.foundation.layout.Column
88
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.offset
910
import androidx.compose.material.*
1011
import androidx.compose.runtime.*
1112
import androidx.compose.ui.Modifier
@@ -16,8 +17,8 @@ import com.google.accompanist.pager.HorizontalPager
1617
import com.google.accompanist.pager.PagerState
1718
import com.google.accompanist.pager.rememberPagerState
1819
import com.google.accompanist.systemuicontroller.rememberSystemUiController
19-
import com.smarttoolfactory.composecolorsextended.demo.MaterialColorSelectionDemo
20-
import com.smarttoolfactory.composecolorsextended.demo.PrimaryColorSelectionDemo
20+
import com.smarttoolfactory.composecolorsextended.demo.MD2ColorSelectionDemo
21+
import com.smarttoolfactory.composecolorsextended.demo.MD3ColorShadeSelectionDemo
2122
import com.smarttoolfactory.composecolorsextended.ui.theme.ComposeColorsExtendedTheme
2223
import kotlinx.coroutines.launch
2324

@@ -28,6 +29,8 @@ class MainActivity : ComponentActivity() {
2829
super.onCreate(savedInstanceState)
2930
setContent {
3031
ComposeColorsExtendedTheme {
32+
33+
Modifier.offset()
3134
// A surface container using the 'background' color from the theme
3235
Surface(
3336
modifier = Modifier.fillMaxSize(),
@@ -88,10 +91,10 @@ private fun HomeContent() {
8891
) { page: Int ->
8992

9093
when (page) {
91-
0 -> PrimaryColorSelectionDemo {
94+
0 -> MD2ColorSelectionDemo {
9295
backgroundColor = it
9396
}
94-
else -> MaterialColorSelectionDemo {
97+
else -> MD3ColorShadeSelectionDemo {
9598
backgroundColor = it
9699
}
97100
}
@@ -100,9 +103,8 @@ private fun HomeContent() {
100103

101104
internal val tabList =
102105
listOf(
103-
"MD2 Primary Colors",
104-
"MD2 Material Colors",
105-
"MD3 Shades",
106+
"Material Design2",
107+
"Material You/3",
106108
"Color Names",
107109
"Gradient Angles",
108110
"Color Conversions",

app/src/main/java/com/smarttoolfactory/composecolorsextended/MaterialColorPicker.kt

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,20 @@ fun MaterialColorPicker(onColorChange: (Color) -> Unit) {
4848

4949
val colorSwatch: LinkedHashMap<Int, Color> = ColorSwatch.primaryColorSwatches[headerIndex]
5050

51-
val keys: List<Int> = colorSwatch.keys.toList()
52-
val colors: List<Color> = colorSwatch.values.toList()
53-
54-
val result: Result<List<Color>> =
55-
runCatching { ColorSwatch.accentColorSwatches[headerIndex].values.toList() }
51+
val keys: MutableList<Int> = colorSwatch.keys.toMutableList()
52+
val colors: MutableList<Color> = colorSwatch.values.toMutableList()
5653

54+
val result: Result<LinkedHashMap<Int, Color>> =
55+
runCatching { ColorSwatch.accentColorSwatches[headerIndex] }
5756

57+
if (result.isSuccess) {
58+
result.getOrNull()?.let { accentColorSwatch: LinkedHashMap<Int, Color> ->
59+
val accentKeys = accentColorSwatch.keys.toList()
60+
val accentColors = accentColorSwatch.values.toList()
61+
keys.addAll(accentKeys)
62+
colors.addAll(accentColors)
63+
}
64+
}
5865
Divider(
5966
modifier = Modifier
6067
.fillMaxHeight()
@@ -68,23 +75,33 @@ fun MaterialColorPicker(onColorChange: (Color) -> Unit) {
6875
contentPadding = PaddingValues(horizontal = 10.dp, vertical = 8.dp)
6976
) {
7077
itemsIndexed(colors) { index: Int, item: Color ->
71-
ColorRowWithInfo(
72-
modifier =
73-
Modifier
74-
.graphicsLayer {
75-
scaleY = if (selectedColorIndex == index) 1.03f else 1f
76-
scaleX = if (selectedColorIndex == index) 1.03f else 1f
77-
}
78-
.shadow(2.dp, RoundedCornerShape(4.dp))
79-
.fillMaxWidth()
80-
.clickable {
81-
selectedColorIndex = index
82-
onColorChange(item)
83-
},
84-
title = keys[index].toString(),
85-
color = item,
86-
textColor = if (index < 5) Color.Black else Color.White
87-
)
78+
Column {
79+
if (index == 0 || index == 10) {
80+
Text(
81+
modifier = Modifier.padding(8.dp),
82+
text = if (index == 0) "Primary" else "Accent",
83+
fontSize = 24.sp,
84+
fontWeight = FontWeight.Bold
85+
)
86+
}
87+
ColorRowWithInfo(
88+
modifier =
89+
Modifier
90+
.graphicsLayer {
91+
scaleY = if (selectedColorIndex == index) 1.03f else 1f
92+
scaleX = if (selectedColorIndex == index) 1.03f else 1f
93+
}
94+
.shadow(2.dp, RoundedCornerShape(4.dp))
95+
.fillMaxWidth()
96+
.clickable {
97+
selectedColorIndex = index
98+
onColorChange(item)
99+
},
100+
title = keys[index].toString(),
101+
color = item,
102+
textColor = if (index < 5 || index > 9) Color.Black else Color.White
103+
)
104+
}
88105
}
89106
}
90107
}
@@ -116,7 +133,7 @@ fun ColorRowWithInfo(
116133
Text(
117134
text = colorToHex(color),
118135
color = textColor,
119-
fontSize = 24.sp,
136+
fontSize = 22.sp,
120137
fontWeight = FontWeight.Bold
121138
)
122139
}

app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/PrimaryColorSelectionDemo.kt renamed to app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/MD2ColorSelectionDemo.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import androidx.compose.ui.graphics.Color
55
import com.smarttoolfactory.composecolorsextended.MaterialColorPicker
66

77
@Composable
8-
fun PrimaryColorSelectionDemo(onColorChange: (Color) -> Unit) {
8+
fun MD2ColorSelectionDemo(onColorChange: (Color) -> Unit) {
99
MaterialColorPicker(onColorChange = onColorChange)
1010
}
11-

app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/MaterialColorSelectionDemo.kt renamed to app/src/main/java/com/smarttoolfactory/composecolorsextended/demo/MD3ColorShadeSelectionDemo.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.compose.ui.graphics.Color
55
import com.smarttoolfactory.composecolorsextended.MaterialColorPicker
66

77
@Composable
8-
fun MaterialColorSelectionDemo(onColorChange: (Color) -> Unit) {
8+
fun MD3ColorShadeSelectionDemo(onColorChange: (Color) -> Unit) {
99
MaterialColorPicker(onColorChange = onColorChange)
1010
}
11+

0 commit comments

Comments
 (0)