As I was playing around with contrast-color(), I got a wild idea that you could use contrast-color() to invert its return value by nesting it: contrast-color(contrast-color(var(--some-color)). When would this be useful? Uh… Good question. I couldn’t come up with an example right away but after a bit I found one sitting right under my nose….

Our focus-rings in Fluent use a 1px inset white highlight and a 2px offset black focus-ring. It’s a smidge chonkier than the Chromium default. The reason we do this is to guarantee contrast against the focused-element, in the above example, a blue button. Without the addition of the white stroke, the black outline wouldn’t “pop” with enough contrast to the blue background.
To make this work, we have a --focus-inner-ring token and a --focus-outer-ring token themed for both light and dark modes.
*:focus-visible {
box-shadow: 0 0 1px 0 var(--focus-inner-ring);
outline: 2px solid var(--focus-outer-ring);
outline-offset: 1px;
}
How would this change with nested contrast-color()?
*:focus-visible {
outline: 2px solid contrast-color(var(--page-bg));
outline-offset: 1px;
box-shadow: 0 0 1px 0 contrast-color(contrast-color(var(--page-bg)));
}
All you would need is one token you probably already have (--page-bg) vs two tokens. Neat.
One consideration… your focus-ring isn’t always on a --page-bg. Sometimes it shows up on a --card-bg and this little trick might fall apart. As always, your mileage may vary.
Aside: This got me thinking it’d be nice to have a currentBackgroundColor like we have currentColor in CSS today. I’m not sure how much career I’ve got left to wait for that, but who knows.
On second thought…
Mulling this over a bit more… you might be better off using color-scheme and light-dark() for this instead.
:root {
color-scheme: light dark;
}
*:focus-visible {
box-shadow: 0 0 1px 0 light-dark(white, black);
outline: 2px solid light-dark(black, white);
outline-offset: 1px;
}
Yeah… I’d probably do that. You dodge the spotty contrast-color() support and have a singular function instead of a nested situation. It keeps it simple and readable.
If you converted this to tokens, you’d probably need four tokens to fill out the inner/outer and light/dark matrix. But I’d consider not using custom color tokens for focus-rings at all and embrace the higher contrast… or limit tokens to the outer-ring.