Skip to content

Add contrast() function to check contrast ratio for two colors #7944

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: dev-2.0
Choose a base branch
from

Conversation

FerrinThreatt
Copy link

Resolves #6971

Changes:

Added a new contrast function that checks the color contrast ratio for two colors
This function allows users to see if their colors align with WCAG21 accessibility standards. There are supplementary double AA and triple AAA constants for provided as well.

PR Checklist

Copy link
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

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

Thanks for making this change! For my own understanding / for possible future documentation, do you know how this handles colors with alpha?

@@ -25,7 +26,7 @@ import {

OKLab,
OKLCH as OKLCHSpace,

contrast,
Copy link
Contributor

Choose a reason for hiding this comment

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

cool, TIL colorjs has this built in!

* // Test for contrast
* let result = color1.contrast(color2)
*
* console.log(result)
Copy link
Contributor

Choose a reason for hiding this comment

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

On the p5.js reference, this will require users to open the console to see the result. Do you think we could show the result on the screen with something like text()?

* Checks if two colors contrast ratio is WCAG 2.1 compliant and returns the ratio
*
* @param {Color} other
* @returns {{ ratio: Number, passes: boolean }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know if passes is using AA or AAA or something else?

@ksen0
Copy link
Member

ksen0 commented Jul 15, 2025

Hi @davepagurek, I'll be taking this on one on from now, since @FerrinThreatt will be focusing on sound!

For my own understanding / for possible future documentation, do you know how this handles colors with alpha?

We've talked about adding unit tests for this too, so I think it should include examples with alpha, and explain in the description what the behavior is. I am not sure what the colorjs behavior is, but I would expect p5.js behavior to be undefined because it is not possible to judge color contrast in isolation.

Do you know if passes is using AA or AAA or something else?

Something that ended up being out of scope for this PR but which we discussed would be functionality and usage as follows:

  • export the color.contrast(other_color) function, which returns a floating point number rather than obbject, matching the colorjs pattern but requiring no parameters; and also export two constants, eg, WCAG_AAA_CONTRAST_MINIMUM and WCAG_AA_CONTRAST_MINIMUM
  • Recommended usage for accessibility would be something like if (color.contrast(other_color) >= WCAG_AAA_CONTRAST_MINIMUM). This could both help the user learn about accessibility - and shows rather than hides the AA/AAA framework - and does not require memorizing threshold values

On the p5.js reference, this will require users to open the console to see the result. Do you think we could show the result on the screen with something like text()?

Great idea, agree! Also, in this or another example the background and text could have randomly-selected colors, but that pass AAA - to demonstrate how this could be used inside generative art logic, rather than only as a debug check

What do you think?

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