https://kotlinlang.org logo
Title
g

gpaligot

08/24/2021, 7:11 PM
Hello all! I'm looking for a function to convert a rgb
Color
to a HSL representation but I can only find a HSL to RGB function in the
Color
class. Someone can confirm me that the RGB to HSL function doesn't exist?
z

Zach Klippenstein (he/him) [MOD]

08/24/2021, 7:33 PM
I think something like this should do it:
val (h, s, l) = color.convertTo(ColorSpaces.HSL)
The component/rgb accessors on
Color
are really just whatever components the color space defines. They’re just called “rgb” because it’s the most common, presumably
r

romainguy

08/24/2021, 7:41 PM
HSL
is not a color space, so you won’t find it in
ColorSpaces
(it’s a color model)
g

gpaligot

08/24/2021, 7:44 PM
I hoped so much when I read your message Zach but yes, HSL isn't in
ColorSpaces
. In french, we call this an emotional elevator. Don't know if this expression is valid in English. ^^
r

romainguy

08/24/2021, 7:45 PM
And there’s no
toHsl()
but if you are ok with HSV instead of HSL you can use android.graphics.Color
Oh wait 1 sec
I just remembered there’s something in
androidx.core.graphics
g

gpaligot

08/24/2021, 7:47 PM
Oh super cool! Thanks Romain!
z

Zach Klippenstein (he/him) [MOD]

08/24/2021, 8:51 PM
My bad, i could have sworn i saw hsl in the list but clearly was thinking of something else
r

romainguy

08/24/2021, 9:06 PM
@Zach Klippenstein (he/him) [MOD] I’ll give you a course on color management and bore you to death :))
😂 1
1
z

Zach Klippenstein (he/him) [MOD]

08/24/2021, 9:07 PM
sounds pretty great actually 😛