You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Reference to underlying color object depending on implementation
46
51
// Not meant to be used publicly unless the implementation is known for sure
@@ -326,6 +331,125 @@ class Color {
326
331
returncolorString;
327
332
}
328
333
334
+
/**
335
+
* Checks the contrast between two colors. This method returns a boolean
336
+
* value to indicate if the two color has enough contrast. `true` means that
337
+
* the colors has enough contrast to be used as background color and body
338
+
* text color. `false` means there is not enough contrast.
339
+
*
340
+
* A second argument can be passed to the method, `options` , which defines
341
+
* the algorithm to be used. The algorithms currently supported are
342
+
* WCAG 2.1 (`'WCAG21'`) or APCA (`'APCA'`). The default is WCAG 2.1. If a
343
+
* value of `'all'` is passed to the `options` argument, an object containing
344
+
* more details is returned. The details object will include the calculated
345
+
* contrast value of the colors and different passing criteria.
346
+
*
347
+
* For more details about color contrast, you can check out
348
+
* <a href="https://colorjs.io/docs/contrast">this page from color.js</a>, and the
349
+
* <a href="https://webaim.org/resources/contrastchecker/">WebAIM color contrast checker.</a>
350
+
*
351
+
* @param {Color} other
352
+
* @returns {boolean|object}
353
+
* @example
354
+
* <div>
355
+
* <code>
356
+
* let bgColor, fg1Color, fg2Color, msg1, msg2;
357
+
* function setup() {
358
+
* createCanvas(100, 100);
359
+
* bgColor = color(0);
360
+
* fg1Color = color(100);
361
+
* fg2Color = color(220);
362
+
*
363
+
* if(bgColor.contrast(fg1Color)){
364
+
* msg1 = 'good';
365
+
* }else{
366
+
* msg1 = 'bad';
367
+
* }
368
+
*
369
+
* if(bgColor.contrast(fg2Color)){
370
+
* msg2 = 'good';
371
+
* }else{
372
+
* msg2 = 'bad';
373
+
* }
374
+
*
375
+
* describe('A black canvas with a faint grey word saying "bad" at the top left and a brighter light grey word saying "good" in the middle of the canvas.');
376
+
* }
377
+
*
378
+
* function draw(){
379
+
* background(bgColor);
380
+
*
381
+
* textSize(18);
382
+
*
383
+
* fill(fg1Color);
384
+
* text(msg1, 10, 30);
385
+
*
386
+
* fill(fg2Color);
387
+
* text(msg2, 10, 60);
388
+
* }
389
+
* </code>
390
+
* </div>
391
+
*
392
+
* <div>
393
+
* <code>
394
+
* let bgColor, fgColor, contrast;
395
+
* function setup() {
396
+
* createCanvas(100, 100);
397
+
* bgColor = color(0);
398
+
* fgColor = color(200);
399
+
* contrast = bgColor.contrast(fgColor, 'all');
400
+
*
401
+
* describe('A black canvas with four short lines of grey text that respectively says: "WCAG 2.1", "12.55", "APCA", and "-73.30".');
0 commit comments