Is there a way to go from a Color to the HSV / HSL values? I can not find any method in `androidx.ui...
a
Is there a way to go from a Color to the HSV / HSL values? I can not find any method in
androidx.ui.graphics
for this
m
@Sergey Y. But that’s only Android, isn’t it? But this is the compose desktop channel.
s
I guess the topic starter confused the channel because he mentioned Android SDK package originally, and the question was about compose for android.
@Arjan van Wieringen #compose-android
a
No I didn’t t confuse it. The compose desktop Jetbrains packages have a lot of Android package names in them
s
s
android.graphics
package and
androidx.*
are not the same.
android.graphics
package is part of Android SDK and it only works on Android OS. Androidx is a collection of separate libraries and some of them can work on different platforms.
a
You’re right, that was a typo on my part
a
ChatGPT suggests this:
Copy code
import androidx.compose.ui.graphics.Color
import kotlin.math.max
import kotlin.math.min

fun Color.toHsl(): FloatArray {
    val r = red
    val g = green
    val b = blue

    val max = max(r, max(g, b))
    val min = min(r, min(g, b))

    val h: Float
    val s: Float
    val l = (max + min) / 2

    if (max == min) {
        // achromatic
        h = 0f
        s = 0f
    } else {
        val d = max - min
        s = if (l > 0.5f) d / (2 - max - min) else d / (max + min)
        h = when (max) {
            r -> (g - b) / d + (if (g < b) 6 else 0)
            g -> (b - r) / d + 2
            b -> (r - g) / d + 4
            else -> 0f
        }
    }

    val hsl = FloatArray(3)
    hsl[0] = h * 60 // converting to degrees
    hsl[1] = s
    hsl[2] = l
    return hsl
}
Or you can use
java.awt.Color.*RGBtoHSB*
a
I am in CommonMain so no Java.awt for me :)
a
You could do an expect/actual with ColorUtils on Android and awt Color on desktop.
Unless you need iOS, JS or native