julioromano
04/06/2022, 10:23 PMColor
? (by invert I mean doing 1-x, in other words if luminance is 0.75 inverting it yields a luminance of 0.25).
I’ve tried something like this but with little success:
val xyz = myColor.convert(ColorSpaces.CieXyz)
val invertedLuminance = xyz.copy(green = 1 - xyz.green)
val result = invertedLuminance.convert(ColorSpaces.Srgb)
romainguy
04/06/2022, 10:27 PMjulioromano
04/06/2022, 10:28 PMjulioromano
04/06/2022, 10:30 PMandroidx.compose.ui.graphics.Color
?romainguy
04/06/2022, 10:31 PMromainguy
04/06/2022, 10:31 PMjulioromano
04/06/2022, 10:36 PMColor.hsl(...)
experimental API that seems to create a Color
from HSL values, though my input is a RGB Color
: is there any existing API to transform RGB to HSL?julioromano
04/06/2022, 10:53 PMromainguy
04/07/2022, 1:52 AMromainguy
04/07/2022, 1:53 AMromainguy
04/07/2022, 1:53 AMromainguy
04/07/2022, 1:53 AMromainguy
04/07/2022, 1:54 AMjulioromano
04/07/2022, 10:32 AMjulioromano
04/07/2022, 10:34 AMval myColor = Color.White
val hslArray = FloatArray(3)
ColorUtils.RGBToHSL(
(myColor.red * 255.0f + 0.5f).toInt(),
(myColor.green * 255.0f + 0.5f).toInt(),
(myColor.blue * 255.0f + 0.5f).toInt(),
hslArray
)
val invertedHsl = FloatArray(3).apply {
this[0] = hslArray[0]
this[1] = hslArray[1]
this[2] = 1 - hslArray[2]
}
val invertedRgb = ColorUtils.HSLToColor(invertedHsl)
return Color(invertedRgb)
Whilst it works for white and black (it inverts them to black and white respectively) it’s not exactly working as expected for other colors.
What I had in mind by “inverting the luminance” was doing (1-x) with the Y value of the color converted to the YPrPb color space.
I tried to check here: http://colorizer.org/ for the intended effect and that’s exactly what I had in mind, though I can’t find APIs for conversion from RGB to YPbPr in Android, do you know if there is any or is this all “uncharted territory” ?romainguy
04/07/2022, 4:18 PMromainguy
04/07/2022, 4:18 PMromainguy
04/07/2022, 4:19 PMromainguy
04/07/2022, 4:19 PML
in particular is in the range 0..100
romainguy
04/07/2022, 4:19 PMjulioromano
04/07/2022, 4:44 PMval myColor = Color.White
val lab = myColor.convert(ColorSpaces.CieLab)
val inverted = lab.copy(red = 100 - lab.red)
val invertedRgb = inverted.convert(ColorSpaces.Srgb)
return invertedRgb
I couldn’t find Oklab though.romainguy
04/07/2022, 5:00 PMromainguy
04/07/2022, 5:01 PMromainguy
04/07/2022, 5:01 PMgetMin/MaxValue(0)
on the ColorSpace
object to automate this)romainguy
04/07/2022, 5:50 PMjulioromano
04/07/2022, 7:32 PMIt’s there: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]/androidx/compose/ui/graphics/colorspace/ColorSpaces.kt;l=280That’s why autocomplete didn’t find it: In compose 1.1.1 that line is marked as
internal
.julioromano
04/07/2022, 7:33 PMWhat are you trying to do btw?We display text onto a surface of arbitrary color. To avoid readability issues we detect when the contrast between the surface color (bg) and the text color (fg) falls under a certain threshold and when it happens we’d like to transform the fg color to increase readability. I’m trying to find a decent enough transform. Since contrast is more or less a ratio of luminances (pardon if this sounds as blasphemy to your ears 🙂 ), I thought that by inverting the luminance of the fg color I could increase the contrast with the bg whilst maintaining some sort of “similarity” between the input fg color and the output one (similarity in the sense that if it’s a blue it should still be some kind of blue and so on...).
romainguy
04/07/2022, 7:38 PMjulioromano
04/07/2022, 7:49 PMjulioromano
04/07/2022, 7:51 PMromainguy
04/07/2022, 7:51 PMromainguy
04/07/2022, 7:52 PMromainguy
04/07/2022, 8:40 PMjulioromano
04/07/2022, 9:46 PMBtw your approach won’t work when luminance is in the mid range (say luminance = 0.5 or close)Indeed, but let’s worry about this “subtle” detail later… 🤦
julioromano
04/07/2022, 9:46 PMjulioromano
04/07/2022, 9:47 PMjulioromano
04/07/2022, 9:48 PMjulioromano
04/07/2022, 9:49 PMjulioromano
04/07/2022, 9:51 PMromainguy
04/07/2022, 10:18 PMromainguy
04/07/2022, 10:19 PMromainguy
04/07/2022, 10:19 PMromainguy
04/07/2022, 10:19 PMjulioromano
04/08/2022, 12:43 PMjulioromano
04/08/2022, 12:45 PMYou could use an LCh model insteadYou mean this: https://en.wikipedia.org/wiki/HCL_color_space ? It’d be fun if compose had APIs for it.
julioromano
04/08/2022, 1:04 PMromainguy
04/08/2022, 3:49 PMromainguy
04/08/2022, 3:49 PMromainguy
04/08/2022, 3:49 PMI seem to perceive the colors in the testing pattern in a slightly different way when viewing them inside AS preview window, or Mac’s Preview app or Slack
romainguy
04/08/2022, 3:49 PMromainguy
04/08/2022, 3:49 PMromainguy
04/08/2022, 3:50 PMromainguy
04/08/2022, 3:50 PMjulioromano
04/11/2022, 6:05 AMjulioromano
04/11/2022, 6:08 AM