Skip to content

Commit beef115

Browse files
committed
color.contrast implementation with example
1 parent 5fe2c71 commit beef115

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

src/color/p5.Color.js

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,33 +295,91 @@ class Color {
295295
return colorString;
296296
}
297297
/**
298-
* Checks if two colors contrast ratio is WCAG 2.1 compliant and returns the ratio
298+
* Checks the contrast between two colors, to make sure that they
299+
* 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`.
304+
*
305+
* You can also explore this
306+
* <a href="https://webaim.org/resources/contrastchecker/">contrast checker tool</a>.
307+
* The contrast function in p5.js uses the WCAG 2.1 method the
308+
* <a href="https://colorjs.io/docs/contrast.html">color.js contrast</a>
309+
* utility.
310+
*
311+
299312
*
300313
* @param {Color} other
301-
* @returns {{ ratio: Number, passes: boolean }}
314+
* @returns {{ ratio: Number }}
302315
* @example
303316
* <div>
304317
* <code>
305318
*
319+
* // The contrast checker can be used both during development
320+
* // with `print()`, or to help select readable colors on the fly.
321+
* // This example shows both uses.
322+
*
323+
* let bgColor;
324+
* let fg1Color;
325+
* let fg2Color;
326+
*
306327
* function setup() {
307-
* // Define colors
308-
* let color1 = color(255, 255, 255);
309-
* let color2 = color(0);
310-
*
311-
* // Test for contrast
312-
* let result = color1.contrast(color2)
328+
* createCanvas(100, 100);
329+
* bgColor = color(0);
330+
* fg1Color = color(120);
331+
* fg2Color = color(255);
332+
*
333+
* describe('A small square canvas with acentered text outlined by a thick stroke. The text reads 'click again!'. On every mouse click, the background, square outline, and text colors randomize, with high enough contrast for readability.');
334+
* }
313335
*
314-
* console.log(result)
336+
* function draw() {
337+
* background(bgColor);
338+
* stroke(fg1Color);
339+
* noFill();
340+
* strokeWeight(5);
341+
* rect(10, 10, 80, 80);
315342
*
343+
* noStroke();
344+
* fill(fg2Color);
345+
* textAlign(CENTER, CENTER);
346+
* textSize(20);
347+
* text("click\nagain!", 50, 50);
348+
* }
349+
*
350+
* function mouseClicked(){
351+
* let newBgColor;
352+
* let newFg1Color;
353+
* let newFg2Color;
354+
*
355+
* // The loop may go for a long time, but it will not go on forever
356+
* // It will stop the first time that the random colors contrast enough
357+
* for (let i = 0; i < 10000; i += 1){
358+
* newBgColor = color(random(255), random(255), random(255));
359+
* newFg1Color = color(random(255), random(255), random(255));
360+
* newFg2Color = color(random(255), random(255), random(255));
361+
* 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 ){
365+
*
366+
* bgColor = newBgColor;
367+
* fg1Color = newFg1Color;
368+
* fg2Color = newFg2Color;
369+
*
370+
* break;
371+
* }
372+
* }
373+
*
374+
* print("Contrast (rect)", bgColor.contrast(fg1Color));
375+
* print("Contrast (text)", bgColor.contrast(fg2Color));
316376
* }
317377
* </code>
318378
* </div>
319379
*/
320-
contrast(new_other) {
321-
const contrast_method = 'WCAG21';
322-
const ratio = contrast(this._color, new_other._color, contrast_method);
323-
return ratio;
324-
};
380+
contrast(other_color) {
381+
return contrastWCAG21(this._color, other_color._color);
382+
};
325383

326384
/**
327385
* Sets the red component of a color.

0 commit comments

Comments
 (0)