diff --git a/src/color/p5.Color.js b/src/color/p5.Color.js index d258bf10e2..185a703aeb 100644 --- a/src/color/p5.Color.js +++ b/src/color/p5.Color.js @@ -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 }} + * @example + *
+ * + * + * 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) + * + * } + * + *
+ */ + 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. diff --git a/src/core/constants.js b/src/core/constants.js index 942b48c9ad..c51bae9306 100644 --- a/src/core/constants.js +++ b/src/core/constants.js @@ -1371,3 +1371,18 @@ export const EXCLUDE = Symbol('exclude'); * @private */ export const JOIN = Symbol('join'); + +/** + * @typedef {'color-contrast-threshold-aa'} COLOR_CONTRAST_THRESHOLD_AA + * @property {COLOR_CONTRAST_THRESHOLD_AA} COLOR_CONTRAST_THRESHOLD_AA + * @final + */ +export const COLOR_CONTRAST_THRESHOLD_AA = 4.5; + + +/** + * @typedef {'color-contrast-threshold-aaa'} COLOR_CONTRAST_THRESHOLD_AAA + * @property {COLOR_CONTRAST_THRESHOLD_AAA} COLOR_CONTRAST_THRESHOLD_AAA + * @final + */ +export const COLOR_CONTRAST_THRESHOLD_AAA = 7.0;