Hello! I have been trying to make a color picker f...
# compose
j
Hello! I have been trying to make a color picker for Jetpack Compose (Desktop) because I couldn't find any.
I made a color palette with this:
Copy code
@Composable
fun ColorPicker(modifier: Modifier = Modifier, onSelect: (Color) -> Unit) {
    BoxWithConstraints {
        var selectedColor by remember { mutableStateOf(Color.Red) }
        Box(
            modifier = modifier
                .fillMaxSize()
                .background(Brush.horizontalGradient(colors()))
        ) {

        }
    }

}

fun colors(n: Int = 359): List<Color> {
    val cols = mutableListOf<Color>()
    for (i in 0 until n) {
        val color = java.awt.Color.getHSBColor(i.toFloat() / n.toFloat(), 0.85f, 1.0f)
        cols.add(Color(color.red, color.green, color.blue, color.alpha))
    }
    return cols
}
Now how would I get the Color if the user clicks on the palette? (Or is there a different way for this?)
I don't know if that is a good way but it worked using clickable and the Robot class. So on click I just get the rgb on the current mouse position.
c
Yeah that is pretty much what I did as well - I also made a color picker in Compose Desktop as an experiment 😎 https://github.com/c5inco/IntelliJ-Experiments/blob/main/src/main/kotlin/com/c5inco/idea/apps/colorpicker/ColorPicker.kt
t
You can also use the click coordinates with some math to get the color.
1