https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hiring-french
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
j

julioromano

04/06/2022, 10:23 PM
How can I invert the luminance component of a
Color
? (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)
r

romainguy

04/06/2022, 10:27 PM
Don’t go through XYZ, instead you should use the HSL color space
j

julioromano

04/06/2022, 10:28 PM
Lightness == Luminance ? (well… if we translate latin to english it would seems so…)
But how to convert to HSL an sRGB
androidx.compose.ui.graphics.Color
?
r

romainguy

04/06/2022, 10:31 PM
Well technically the L in HSL is not really lightness either
😅 1
it’s more like brightness or luminance
j

julioromano

04/06/2022, 10:36 PM
I found a
Color.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?
r

romainguy

04/07/2022, 1:52 AM
Sorry I missed this question. But yeah you found the answer :)
Anyway to expand on what I was saying earlier:
• luminance is emitted light
• brightness is perceived luminance
• lightness is brightness relative to other brightness (spatial variation of perceived luminance)
j

julioromano

04/07/2022, 10:32 AM
You’re a well of wisdom.
This is my concoction to try to achieve luminance inversion:
val 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” ?
r

romainguy

04/07/2022, 4:18 PM
It’s not “uncharted” territory, the formulas are well defined and not hard to implement
But instead of YPbPr you could use Lab which is provided by Compose’s APIs already
Note that in Lab the values are not necessarily in the range 0..1
L
in particular is in the range
0..100
You can also use Oklab instead of Lab to get a more perceptually linear model
j

julioromano

04/07/2022, 4:44 PM
Thanks! What about:
val 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.
Note that in Oklab, the L component is in the range 0..1 🙂
(you can call
getMin/MaxValue(0)
on the
ColorSpace
object to automate this)
What are you trying to do btw?
j

julioromano

04/07/2022, 7:32 PM
It’s there: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]/androidx/compose/ui/graphics/colorspace/ColorSpaces.kt;l=280
That’s why autocomplete didn’t find it: In compose 1.1.1 that line is marked as
internal
.
What 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...).
r

romainguy

04/07/2022, 7:38 PM
Ah right. Well the idea is not bad but the problem is that it’s not how our perception works (which is really annoying). It’ll mostly work, kind of 🙂
j

julioromano

04/07/2022, 7:49 PM
“Mostly, kind of” is our measure of success here 😄. Moreover that's what you get when the chap writing the code has color blindness 😅.
Btw, thanks for bearing with me until here. Appreciate it.
r

romainguy

04/07/2022, 7:51 PM
Happy to help!
Let me know how CIE Lab and Oklab work for you, and if that’s not good enough, there are other solutions
👌 1
Btw your approach won't work when luminance is in the mid range (say luminance = 0.5 or close)
j

julioromano

04/07/2022, 9:46 PM
Btw 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… 🤦
This is a rough preview using CieLab:
Red is a bit “unlucky”.
This test pattern has the same line of text drawn twice, the first line is not visible because its color is the same as the bg (to simulate the worst case, least contrast scenario) the second line has the color with the transform applied.
Interestingly the last test color (green-ish) has the first line of text also visible (barely), even though technically it is the same exact color as the background.
(I’d like to test Oklab too but AS Bumblebee preview window goes bonkers with Compose 1.2.0-alpha… I’ll try to fix this later on…)
r

romainguy

04/07/2022, 10:18 PM
Blue is the color of least luminance so that result isn't surprising
You could use an LCh model instead
Oh wait no nevermind
Wrong "C" :)
j

julioromano

04/08/2022, 12:43 PM
I was able to upgrade to compose 1.2.0-alpha07 so here are Lab, Xyz and Oklab compared. Strangely enough the last (bottom) bg color, which is rgb(0,139,0), looked different with compose 1.1.1 and now it’s changed considerably.
You could use an LCh model instead
You mean this: https://en.wikipedia.org/wiki/HCL_color_space ? It’d be fun if compose had APIs for it.
But I guess that there is some color “trickery” in our testing pipeline 🤣 I 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 🤦
r

romainguy

04/08/2022, 3:49 PM
Note: I was playing with Oklab yesterday and I found several bugs in the implementation
I wouldn’t trust it for now 😕
👌 1
I 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
Welcome to color management
This means one or more of those apps are not handling the color profiles properly
I recently filed bugs to both the Studio and emulator teams because if your display is not exaclty sRGB, then colors are displayed incorrectly
I know Studio has been fixed already, so latest canaries should be good to go
👌 1
j

julioromano

04/11/2022, 6:05 AM
I’ll stick with CieLab for now and will try to switch to OkLab when compose 1.2 hits stable. I guess we can call this an exercise 🙂. Anyhow its output, albeit far from perfect, is enough for what I needed. Thank you!