For bug 856812 I landed a preliminary API that lets a UI surface ask for a readable color given foreground and background colors. There are a few problems with my solution:
* The implementation does a binary search for the correct color. Hypothetically we could directly calculate the required luminance for minimum contrast and use that to derive a blend of the foreground color. However, the math is tricky.
* Callers often have to do something like: "foreground = IsDark(background) ? white : black" before calling GetColorWithMinimumContrast. Ideally we would clean that up, but we can't simply select the starting foreground color with the best contrast. Most surfaces have multiple colors that all need to be "dark themed" or "light themed" together and a naive solution could cause them to get out of sync.
* The implementation has to guess which way to blend the foreground color. If the caller gives a background a foreground on the same side of the spectrum (both light or both dark), it's not clear whether we should blend to the extreme of that spectrum (to preserve being on the same side of the spectrum) or blend the opposite way (to guarantee a good contrast).
Comment 1 by ligimole@google.com
, Nov 19