frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

How to have the browser pick a contrasting color in CSS

https://webkit.org/blog/16929/contrast-color/
67•Kerrick•3h ago

Comments

crtasm•2h ago
>This browser does not support contrast-color(). Try this demo in a browser that does, like Safari Technology Preview
judah•1h ago
And I don't yet see an entry for this on caniuse.com. I'm guessing this is super new.
miiiiiike•1h ago
Very new. I think Safari is the only one that ships it and even then it’s still in preview
homebrewer•1h ago
It works in "Gnome Web", which is mostly a wrapper around WebKit.

> Support for this feature first shipped in March 2021, in Safari Technology Preview 122.

https://webkit.org/blog/11577/release-notes-for-safari-techn...

> Added experimental support for CSS Color 5 color-contrast()

https://trac.webkit.org/changeset/273683/webkit/

ctippett•1h ago
You don't need the Technology Preview, it's available as a WebKit Feature Flag under the advanced settings of normal Safari. I just enabled it on my phone and was able to view the demos.
mediumsmart•1h ago
>But, on a large project, with a large team, carefully managing such details can become a really hard task to get right. Suddenly a dark button has unreadable black text, and users can’t figure out what to do.

Cant someone take a look at the buttons before the large project ships? Alternatively make it mandatory to never have black text on a dark button and tell every team member including the large ones.

Interesting to read about the perceptual contrast vs mathematical - I did not know that. Going to integrate that into my workflow.

johnisgood•1h ago
You may want to read about APCA, as you can have perceptual contrast calculations using the APCA algorithm.
refulgentis•1h ago
You can have them with WCAG2, the stock APCA example hides the ball significantly and leads to a lot of incorrect conclusions in the article (tl;dr: black has more contrast by either measure, its just that APCA says you don't need as much contrast, so you can use white and have sufficient contrast)
johnisgood•1h ago
I know about WCAG, too. You can also just implement a function that detects whether or not a color is dark or not. It is a general purpose function, e.g. my "isDark" function is: "func() < 0.5" (func() is omitted, but it is an algorithm). You can have "isLight", too, by doing "> 0.5". There are many ways to do this. You can just simply convert a hex color to RGB, then compute the luminance of the color, and then compare the luminance to a threshold (e.g. 0.5) to classify it as dark or light. The luminance function (WCAG luminance formula) converts RGB values to the range 0-1, applies gamma correction, and calculates luminance using the weighted sum of the gamma-corrected RGB values.

> APCA says you don't need as much contrast

You can always specify the threshold if you want, e.g. "apcaContrast(color)) >= $targetContrast" after adjusting, depending on what you want to do.

It really is easy, just make sure you have enough color space.

refulgentis•43m ago
The WCAG luminance formula (relative luminance in color science terms) has perceptual mid gray at 0.18, not 0.5.

re: just change APCA contrast target, that's separate from the Not Even Wrong stuff in the article. I didn't mean to imply APCA is wrong to say you need less contrast, but rather, that the article is wrong to conclude white has more contrast.

refulgentis•1h ago
The article is wrong:

- Their work does ensure contrast.

- The white on blue clearly has less contrast, not more. (squinting is a cheap way to test, or, walking backwards from your monitor)

With APCA, backgrounds around L* 60 tend to still allow white foregrounds, which is aesthetically closer to what the eye wants.

A black foreground would have more contrast regardless, even by APCA.

To be fair, this is how APCA is almost always demonstrated as a win over the long-running standard, so people run with the premise that the demo image of APCA is more contrast, rather than "ours say you'll have enough contrast to be accessible with a white foreground, even if it also says the contrast would be higher with a black foreground".

(source: in 2020 built color system around the same science, enabling latest iterations of Material theming)

refulgentis•42m ago
Voters, I'd be very happy for feedback, I'm quite surprised it is -3.
troupo•14m ago
I didn't vote, but "your article is wrong" take ignores literally the entire article, and the rather detailed explanation on why "bigger contrast by pure numbers is more contrast" does not work.

> in 2020 built color system around the same science, enabling latest iterations of Material theming

No wonder everything Google builds, including Material, always has issues with contrast.

qfr•1h ago
there is a way to do something close to this using lch:

  --text: lch(from var(--bg) calc((49.44 - l) * infinity) 0 0);
source: https://til.jakelazaroff.com/css/swap-between-black-and-whit...
politelemon•1h ago
I'm still not convinced that the contrasting colour should be the browser vendor's decision, it won't always be right or predictable. Will this be a definitive deterministic standard across all browsers? Instead this function feels like a tool to help UX teams during design phase.
refulgentis•1h ago
c.f. https://news.ycombinator.com/item?id=44015980, when you cut out the incorrect stuff due to confusion re: APCA's button example, it's a bit clearer that it's 100% right.

Consistent, it is not. Ex. we can imagine a background at L* 50 that is ~equally served with a white or black foreground - in that case, the aesthetic principles come into play.

To also disambiguate that, and get to 100% reliable, if both a darker and lighter color are available given contrast K and background color C, look at C, if it's L* is >= 60, choose lighter.

Then, it is 100% correct and consistent.

MBCook•44m ago
> Will this be a definitive deterministic standard across all browsers?

The article says the standard specifies the calculation to use.

jbritton•1h ago
At a minimum it would be nice to know good colors for the pseudo classes active, focus, hover, link, visited and their various combinations for a light and dark theme. Additionally material UI adds disabled, before, after.
dp-hackernews•1h ago
Surely the relative colour theory colour wheel is the answer to this problem.

"Color Wheel: The Basic Color Theory for Artists and Designers" https://dessign.net/color-wheel-theory/

atum47•23m ago
I made a video tutorial about a similar thing long time ago - choosing black or white for text color given a color background. My solution was very simplistic. I just transformed the color to gray scale and compared it between black and white. It was a fun project. I'm not good making videos though.

https://youtu.be/tUJvE4xfTgo?si=vFlegFA_7lzijfSR (warning: video is in Portuguese)

79% of LLM top-1 predictions on TinyStories explained by N-gram statistics

https://arxiv.org/abs/2407.12034
1•pona-a•28s ago•0 comments

How startups build trust (191 examples)

https://socialproofexamples.com/
1•carouselhero•2m ago•0 comments

How to get even a "quasi" objective view of quality OAIdeepresearch vs. Gemini

2•akkoceir•3m ago•0 comments

Wacom drawing tablets track the name of every application you open

https://robertheaton.com/2020/02/05/wacom-drawing-tablets-track-name-of-every-application-you-open/
1•dvrp•4m ago•0 comments

The Weird 1970s Mechanical PONG [video]

https://www.youtube.com/watch?v=wdtLDYTN-NE
1•tobr•7m ago•0 comments

Actory AI – Autonomous QA for Fast-Moving Dev Teams

https://www.actory.ai/
1•GTCHO•10m ago•1 comments

Clever Toyota GR86, Subaru BRZ Bolt-Ons Turn Control Arms into Brake Coolers

https://www.thedrive.com/news/clever-toyota-gr86-subaru-brz-bolt-ons-turn-control-arms-into-brake-coolers
1•PaulHoule•13m ago•0 comments

We fall for fake health information – and how it spreads faster than facts

https://theconversation.com/why-we-fall-for-fake-health-information-and-how-it-spreads-faster-than-facts-250718
2•rntn•14m ago•0 comments

FDA plan to ban fluoride supplements baffles and alarms dental experts

https://www.sciencenews.org/article/fda-fluoride-supplements-dental
3•XzetaU8•15m ago•0 comments

The Khashoggi Compromise

https://www.status.news/p/jeff-bezos-jamal-khashoggi-saudi-arabia-ai-deal
1•archagon•20m ago•1 comments

Arenas and Rust

https://blog.reverberate.org/2021/12/19/arenas-and-rust.html
1•whatever3•22m ago•0 comments

The Stochastic-Quantum Correspondence (2023)

https://inspirehep.net/literature/2635190
1•lisper•25m ago•0 comments

Latency numbers every programmer should know

https://gist.github.com/hellerbarde/2843375
1•behnamoh•26m ago•0 comments

The Ingredients of a Productive Monorepo

https://blog.swgillespie.me/posts/monorepo-ingredients/
1•swgillespie•27m ago•0 comments

Do these Buddhist gods hint at the purpose of China's super-secret satellites?

https://arstechnica.com/space/2025/05/do-these-buddhist-gods-hint-at-the-purpose-of-chinas-super-secret-satellites/
3•bundie•31m ago•0 comments

Emulator Debugging: Area 5150's Lake Effect

https://martypc.blogspot.com/2025/05/emulator-debugging-area-5150s-lake.html
1•ingve•35m ago•0 comments

In the Grand Scheme of Things

https://www.rxjourney.net/in-the-grand-scheme-of-things
2•bertblaast•36m ago•0 comments

Strands Agents, an Open Source AI Agents SDK

https://aws.amazon.com/blogs/opensource/introducing-strands-agents-an-open-source-ai-agents-sdk/
1•devoxi•36m ago•0 comments

Starship Troopers Revolutionize Warfighting

https://perfectingequilibrium.substack.com/p/starship-troopers-revolutionize-warfighting
2•Michelangelo11•37m ago•0 comments

Directory of MCP Servers

https://github.com/chatmcp/mcpso
2•saikatsg•41m ago•0 comments

Software engineer lost his $150K-a-year job to AI and forced to DoorDash

https://www.yahoo.com/news/software-engineer-lost-150k-job-090000839.html
2•namanyayg•42m ago•0 comments

Toshiba says Europe doesn't need 24TB HDDs, witholds beefy models from region

https://www.tomshardware.com/pc-components/hdds/toshiba-says-europe-doesnt-need-24tb-hdds-witholds-beefy-models-from-region
2•taubek•42m ago•0 comments

Norway signs the Artemis Accords for responsible space exploration

https://spacenews.com/norway-signs-artemis-accords/
2•namanyayg•42m ago•0 comments

Twilio denies breach following leak of alleged Steam 2FA codes

https://www.bleepingcomputer.com/news/security/twilio-denies-breach-following-leak-of-alleged-steam-2fa-codes/
1•namanyayg•43m ago•0 comments

OCaml Web Development: Essential Tools and Libraries in 2025

https://tarides.com/blog/2025-05-15-ocaml-web-development-essential-tools-and-libraries-in-2025/
13•birdculture•43m ago•3 comments

CT Towing Co's Can Hold Personal Property for Ransom Because It Was Inside a Car

https://www.jalopnik.com/1861611/towing-companines-holding-personal-property-ranson/
4•rntn•43m ago•0 comments

Chinese 'kill switches' found hidden in US solar farms

https://www.thetimes.com/us/news-today/article/china-solar-panels-kill-switch-vptfnbx7v
13•veqq•47m ago•1 comments

Machine Learning and the Machinic Unconscious

https://spheres-journal.org/contribution/micropolitics-of-a-recommender-system-machine-learning-and-the-machinic-unconscious/
1•corvus-cornix•47m ago•1 comments

FreeBSD 14.3 Beta 3 Brings KDE Plasma 6 Packages to the DVD ISOs

https://www.phoronix.com/news/FreeBSD-14.3-Beta-3
1•mikece•48m ago•0 comments

Arithmophone

https://chielzwinkels.net/arithmophone/
1•anigbrowl•48m ago•0 comments