Skip to content

Commit 0e269b4

Browse files
committed
Added color contrast minimum constants documentation
1 parent beef115 commit 0e269b4

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/color/p5.Color.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626

2727
OKLab,
2828
OKLCH as OKLCHSpace,
29-
contrast,
29+
contrastWCAG21,
3030
P3
3131
} from 'colorjs.io/fn';
3232
import HSBSpace from './color_spaces/hsb.js';
@@ -294,15 +294,22 @@ class Color {
294294
}
295295
return colorString;
296296
}
297-
/**
297+
298+
/**
298299
* Checks the contrast between two colors, to make sure that they
299300
* are different enough to be readable. The result of this function is
300-
* a number that can be compared to `COLOR_CONTRAST_THRESHOLD_AA`,
301-
* or to `COLOR_CONTRAST_THRESHOLD_AAA`. Shapes and UI elements should have color
302-
* contrast at least `COLOR_CONTRAST_THRESHOLD_AA`, and text
303-
* benefits from higher contrast of at least `COLOR_CONTRAST_THRESHOLD_AAA`.
301+
* a color contrast ratio that can be compared to `COLOR_CONTRAST_MINIMUM_GRAPHICS`,
302+
* or to `COLOR_CONTRAST_MINIMUM_TEXT`. The higher the ratio, the more
303+
* different colors are, and the more legible to a user.
304+
*
305+
* Graphics, interface elements, and large text should have a color
306+
* contrast ratio of at least 4.5 (`COLOR_CONTRAST_MINIMUM_GRAPHICS`)
307+
*
308+
* Smaller text - less than at least 14 point or 19 pixels -
309+
* should have a color contrast ratio of at least 7
310+
* (`COLOR_CONTRAST_MINIMUM_TEXT`)
304311
*
305-
* You can also explore this
312+
* The constants are based on WCAG AAA recommendations, which you can also explore in this
306313
* <a href="https://webaim.org/resources/contrastchecker/">contrast checker tool</a>.
307314
* The contrast function in p5.js uses the WCAG 2.1 method the
308315
* <a href="https://colorjs.io/docs/contrast.html">color.js contrast</a>
@@ -359,9 +366,9 @@ class Color {
359366
* newFg1Color = color(random(255), random(255), random(255));
360367
* newFg2Color = color(random(255), random(255), random(255));
361368
* if (
362-
* newBgColor.contrast(newFg2Color) > COLOR_CONTRAST_THRESHOLD_AAA &&
363-
* newBgColor.contrast(newFg1Color) > COLOR_CONTRAST_THRESHOLD_AA &&
364-
* newBgColor.contrast(newFg1Color) < COLOR_CONTRAST_THRESHOLD_AAA ){
369+
* newBgColor.contrast(newFg2Color) >= COLOR_CONTRAST_MINIMUM_TEXT &&
370+
* newBgColor.contrast(newFg1Color) >= COLOR_CONTRAST_MINIMUM_GRAPHICS &&
371+
* newBgColor.contrast(newFg1Color) < COLOR_CONTRAST_MINIMUM_TEXT ){
365372
*
366373
* bgColor = newBgColor;
367374
* fg1Color = newFg1Color;

src/core/constants.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,16 +1373,20 @@ export const EXCLUDE = Symbol('exclude');
13731373
export const JOIN = Symbol('join');
13741374

13751375
/**
1376-
* @typedef {'color-contrast-threshold-aa'} COLOR_CONTRAST_THRESHOLD_AA
1377-
* @property {COLOR_CONTRAST_THRESHOLD_AA} COLOR_CONTRAST_THRESHOLD_AA
1376+
* Can be used with `Color.contrast` to check if graphics, UI elements, and large text
1377+
* have enough contrast.
1378+
* @typedef {'color-contrast-minimum-graphics'} COLOR_CONTRAST_MINIMUM_GRAPHICS
1379+
* @property {COLOR_CONTRAST_MINIMUM_GRAPHICS} COLOR_CONTRAST_MINIMUM_GRAPHICS
13781380
* @final
13791381
*/
1380-
export const COLOR_CONTRAST_THRESHOLD_AA = 4.5;
1382+
export const COLOR_CONTRAST_MINIMUM_GRAPHICS = 4.5;
13811383

13821384

13831385
/**
1384-
* @typedef {'color-contrast-threshold-aaa'} COLOR_CONTRAST_THRESHOLD_AAA
1385-
* @property {COLOR_CONTRAST_THRESHOLD_AAA} COLOR_CONTRAST_THRESHOLD_AAA
1386+
* Can be used with `Color.contrast` to check if text smaller than 14pt (~19px)
1387+
* has enough contrast.
1388+
* @typedef {'color-contrast-minimum-text'} COLOR_CONTRAST_MINIMUM_TEXT
1389+
* @property {COLOR_CONTRAST_MINIMUM_TEXT} COLOR_CONTRAST_MINIMUMD_TEXT
13861390
* @final
13871391
*/
1388-
export const COLOR_CONTRAST_THRESHOLD_AAA = 7.0;
1392+
export const COLOR_CONTRAST_MINIMUM_TEXT = 7.0;

0 commit comments

Comments
 (0)