styled-components / polished

A lightweight toolset for writing styles in JavaScript ✨
https://polished.js.org/
MIT License
7.6k stars 209 forks source link

Implement Advanced Perceptual Contrast Algorithm (APCA) #562

Open djfarly opened 3 years ago

djfarly commented 3 years ago

Summary

Google Chrome added a new experimental feature to replace the AA/AAA contrast ratio guidelines with APCA.

In that spirit, it seems reasonable to me to add that algorithm to polished as well.

The Algorithm is said to be more context depended (font size, weight). I am not sure, whether Chrome uses those values as parameters for the calculation or just to make suggestions. (This needs investigation).

The standard implementation only takes two colors as input which seems to make the most sense to me, as polished is a color lib.

You can also check out this Demo, which is one of the more confusing things I've looked at.

Basic Example

getPerceptualContrast(color1: string, color2: string): number
import { getPerceptualContrast } from 'polished';

const perceptualContrast = getPerceptualContrast('#444', '#fff');

Reasoning

In the long run this may very well replace WCAG AA/AAA contrasts, as it is seemingly better suited for perceptual contrasts.

If used more it would presumably result in more accessible (color-contrast-wise) web in general.


I'd be happy to try and create a PR (following the standard implementation) if there is enough interest in this.

djfarly commented 3 years ago

Note that it might make sense to wait a little since there seem to be changes to the algorithm still: https://bugs.chromium.org/p/chromium/issues/detail?id=1121900#c26

djfarly commented 3 years ago

Here is the Chrome implementation: https://chromium.googlesource.com/devtools/devtools-frontend/+/c88c76b465f2dc8a853fe2a0997ccbfbff0e5bac/front_end/common/ColorUtils.js#88

Myndex commented 2 years ago

Hello, I just stumbled upon this issue thread. I wanted to mention a few related things.

Please feel free to hit me up with any questions or comments.

Thank you,

Andy

Andrew Somers
W3 AGWG Invited Expert
Color Science Researcher
Myndex Perception Research

THE REVOLUTION WILL BE READABLE