-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import { RGB, RGBHDR, HSL, HSB, HWB, LAB, LCH, OKLAB, OKLCH } from './creating_reading'; | ||
|
||
|
||
import { | ||
ColorSpace, | ||
to, | ||
|
@@ -25,7 +26,7 @@ import { | |
|
||
OKLab, | ||
OKLCH as OKLCHSpace, | ||
|
||
contrast, | ||
P3 | ||
} from 'colorjs.io/fn'; | ||
import HSBSpace from './color_spaces/hsb.js'; | ||
|
@@ -41,6 +42,9 @@ const map = (n, start1, stop1, start2, stop2, clamp) => { | |
|
||
const serializationMap = {}; | ||
|
||
|
||
|
||
|
||
class Color { | ||
// Reference to underlying color object depending on implementation | ||
// Not meant to be used publicly unless the implementation is known for sure | ||
|
@@ -290,6 +294,34 @@ class Color { | |
} | ||
return colorString; | ||
} | ||
/** | ||
* 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 commentThe reason will be displayed to describe this comment to others. Learn more. Do you know if |
||
* @example | ||
* <div> | ||
* <code> | ||
* | ||
* function setup() { | ||
* // Define colors | ||
* let color1 = color(255, 255, 255); | ||
* let color2 = color(0); | ||
* | ||
* // Test for contrast | ||
* let result = color1.contrast(color2) | ||
* | ||
* console.log(result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
* | ||
* } | ||
* </code> | ||
* </div> | ||
*/ | ||
contrast(new_other) { | ||
const contrast_method = 'WCAG21'; | ||
const ratio = contrast(this._color, new_other._color, contrast_method); | ||
return ratio; | ||
}; | ||
|
||
/** | ||
* Sets the red component of a color. | ||
|
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!