Skip to content

Commit ca32a8c

Browse files
committed
library: Add a ColorPicker based on OkLab color space
1 parent 6bfb38c commit ca32a8c

File tree

4 files changed

+457
-97
lines changed

4 files changed

+457
-97
lines changed

example/src/commonMain/kotlin/component/OtherComponent.kt

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ import androidx.compose.animation.core.animateFloat
88
import androidx.compose.animation.core.infiniteRepeatable
99
import androidx.compose.animation.core.rememberInfiniteTransition
1010
import androidx.compose.animation.core.tween
11-
import androidx.compose.foundation.background
1211
import androidx.compose.foundation.layout.Arrangement
13-
import androidx.compose.foundation.layout.Box
1412
import androidx.compose.foundation.layout.FlowRow
1513
import androidx.compose.foundation.layout.PaddingValues
1614
import androidx.compose.foundation.layout.Row
1715
import androidx.compose.foundation.layout.Spacer
1816
import androidx.compose.foundation.layout.fillMaxWidth
19-
import androidx.compose.foundation.layout.height
2017
import androidx.compose.foundation.layout.padding
2118
import androidx.compose.foundation.layout.size
2219
import androidx.compose.foundation.layout.width
@@ -31,9 +28,9 @@ import androidx.compose.runtime.remember
3128
import androidx.compose.runtime.setValue
3229
import androidx.compose.ui.Alignment
3330
import androidx.compose.ui.Modifier
34-
import androidx.compose.ui.draw.clip
3531
import androidx.compose.ui.focus.FocusManager
3632
import androidx.compose.ui.graphics.Color
33+
import androidx.compose.ui.graphics.toArgb
3734
import androidx.compose.ui.graphics.vector.ImageVector
3835
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
3936
import androidx.compose.ui.platform.LocalHapticFeedback
@@ -61,8 +58,9 @@ import top.yukonga.miuix.kmp.basic.TextField
6158
import top.yukonga.miuix.kmp.icon.MiuixIcons
6259
import top.yukonga.miuix.kmp.icon.icons.useful.Like
6360
import top.yukonga.miuix.kmp.theme.MiuixTheme
61+
import top.yukonga.miuix.kmp.utils.ColorUtils.colorToHsv
62+
import top.yukonga.miuix.kmp.utils.ColorUtils.colorToOkLab
6463
import top.yukonga.miuix.kmp.utils.PressFeedbackType
65-
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
6664
import kotlin.math.round
6765

6866
fun LazyListScope.otherComponent(
@@ -313,8 +311,8 @@ fun LazyListScope.otherComponent(
313311
}
314312
}
315313

316-
item(key = "colorPicker") {
317-
SmallTitle(text = "ColorPicker")
314+
item(key = "colorPicker-hsva") {
315+
SmallTitle(text = "ColorPicker (HSVA)")
318316
val miuixColor = MiuixTheme.colorScheme.primary
319317
var selectedColor by remember { mutableStateOf(miuixColor) }
320318

@@ -329,43 +327,68 @@ fun LazyListScope.otherComponent(
329327
modifier = Modifier.padding(bottom = 12.dp),
330328
verticalAlignment = Alignment.CenterVertically
331329
) {
330+
val hsv = colorToHsv(selectedColor)
332331
Text(
333-
text = "Selected Color:\nRGBA: " +
334-
"${(selectedColor.red * 255).toInt()}," +
335-
"${(selectedColor.green * 255).toInt()}," +
336-
"${(selectedColor.blue * 255).toInt()}," +
332+
text = "HEX: #${selectedColor.toArgb().toHexString(HexFormat.UpperCase)}" +
333+
"\nRGBA: ${(selectedColor.red * 255).toInt()}, " +
334+
"${(selectedColor.green * 255).toInt()}, " +
335+
"${(selectedColor.blue * 255).toInt()}, " +
337336
"${(round(selectedColor.alpha * 100) / 100.0)}" +
338-
"\nHEX: #" +
339-
(selectedColor.alpha * 255).toInt().toString(16).padStart(2, '0')
340-
.uppercase() +
341-
(selectedColor.red * 255).toInt().toString(16).padStart(2, '0')
342-
.uppercase() +
343-
(selectedColor.green * 255).toInt().toString(16).padStart(2, '0')
344-
.uppercase() +
345-
(selectedColor.blue * 255).toInt().toString(16).padStart(2, '0')
346-
.uppercase(),
337+
"\nHSVA: ${(hsv[0]).toInt()}, " +
338+
"${(hsv[1] * 100).toInt()}%, " +
339+
"${(hsv[2] * 100).toInt()}%, " +
340+
"${(round(selectedColor.alpha * 100) / 100.0)}",
347341
modifier = Modifier.weight(1f)
348342
)
349-
Spacer(Modifier.width(12.dp))
350-
Box(
351-
modifier = Modifier
352-
.height(60.dp)
353-
.width(100.dp)
354-
.align(Alignment.CenterVertically)
355-
.clip(SmoothRoundedCornerShape(12.dp))
356-
.background(selectedColor)
357-
)
358343
}
359-
360344
ColorPicker(
361345
initialColor = selectedColor,
362346
onColorChanged = { selectedColor = it },
363-
showPreview = false,
364347
hapticEffect = SliderDefaults.SliderHapticEffect.Step
365348
)
366349
}
367350
}
368351

352+
item(key = "colorPicker-okLab") {
353+
SmallTitle(text = "ColorPicker (OkLab)")
354+
val miuixColor = MiuixTheme.colorScheme.primary
355+
var selectedColor by remember { mutableStateOf(miuixColor) }
356+
357+
Card(
358+
modifier = Modifier
359+
.fillMaxWidth()
360+
.padding(horizontal = 12.dp)
361+
.padding(bottom = 6.dp),
362+
363+
insideMargin = PaddingValues(16.dp)
364+
) {
365+
Row(
366+
modifier = Modifier.padding(bottom = 12.dp),
367+
verticalAlignment = Alignment.CenterVertically
368+
) {
369+
val okLab = colorToOkLab(selectedColor)
370+
Text(
371+
text = "HEX: #${selectedColor.toArgb().toHexString(HexFormat.UpperCase)}" +
372+
"\nRGBA: ${(selectedColor.red * 255).toInt()}, " +
373+
"${(selectedColor.green * 255).toInt()}, " +
374+
"${(selectedColor.blue * 255).toInt()}, " +
375+
"${(round(selectedColor.alpha * 100) / 100.0)}" +
376+
"\nOkLab: ${(okLab[0] * 1000).toInt() / 1000f}, " +
377+
"${(okLab[1] * 1000).toInt() / 1000f}, " +
378+
"${(okLab[2] * 1000).toInt() / 1000f} / " +
379+
"${(round(selectedColor.alpha * 100) / 100.0)}",
380+
modifier = Modifier.weight(1f)
381+
)
382+
}
383+
ColorPicker(
384+
initialColor = selectedColor,
385+
onColorChanged = { selectedColor = it },
386+
hapticEffect = SliderDefaults.SliderHapticEffect.Step,
387+
useOkLab = true
388+
)
389+
}
390+
}
391+
369392
item(key = "card") {
370393
SmallTitle(text = "Card")
371394
Card(

iosApp/iosApp/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundleShortVersionString</key>
1818
<string>1.0.4</string>
1919
<key>CFBundleVersion</key>
20-
<string>506</string>
20+
<string>508</string>
2121
<key>LSRequiresIPhoneOS</key>
2222
<true/>
2323
<key>CADisableMinimumFrameDurationOnPhone</key>

0 commit comments

Comments
 (0)