-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
base: dev-2.0
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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, |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 }} |
There was a problem hiding this comment.
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?
Hi @davepagurek, I'll be taking this on one on from now, since @FerrinThreatt will be focusing on sound!
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.
Something that ended up being out of scope for this PR but which we discussed would be functionality and usage as follows:
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? |
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
npm run lint
passes