Skip to content

Calculate contrast ratio in sRGB #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sona-mini-studio
Copy link

Hi,

I notice that contrast ratio is only related to the value (in preferred color space) of color pair (two contrast colors). Contrast ratio of the same color pair varies by color space since colors are differently encoded. I've tested it out on Pika of a recent stable build (52, 1.0.2) and a recent one from source (85c3546).

WCAG 2.1 recommends sRGB for color contrast ratio:

Almost all systems used today to view Web content assume sRGB encoding. Unless it is known that another color space will be used to process and display the content, authors should evaluate using sRGB colorspace. If using other color spaces, see Understanding Success Criterion 1.4.3.

After the simple code tweak, contrast ratio will be independent of the color space and always comply with WCAG 2.1.

Color values are converted in sRGB for relative luminance. Extended range beyond [0, 1] is used so bright colors are unclamped for contrast ratio. The introduction of extended range might make negative luminance (Though I can't imagine how to trigger such bug), so luminance values are conservatively clamped to non-negative. The signature of toRGBAComponents() in extension of NSColor will changed without impact on other code.

That's it. Hope it helps the development of Pika!

Copy link

@jrtibbetts jrtibbetts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty neat change.

@superhighfives
Copy link
Owner

This is awesome, and I love how this simplifies the contrast ratio. Going to merge and release in the next beta. 🙌

@sona-mini-studio
Copy link
Author

Thanks for reviewing and commenting 🙂

@superhighfives superhighfives added this to the 1.1.0 milestone Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants