Skip to content

Commit 5fe2c71

Browse files
committed
contrast function and AA/AAA constants
1 parent a79b653 commit 5fe2c71

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

src/color/p5.Color.js

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { RGB, RGBHDR, HSL, HSB, HWB, LAB, LCH, OKLAB, OKLCH } from './creating_reading';
1010

11+
1112
import {
1213
ColorSpace,
1314
to,
@@ -25,10 +26,9 @@ import {
2526

2627
OKLab,
2728
OKLCH as OKLCHSpace,
28-
29+
contrast,
2930
P3
3031
} from 'colorjs.io/fn';
31-
3232
import HSBSpace from './color_spaces/hsb.js';
3333

3434
const map = (n, start1, stop1, start2, stop2, clamp) => {
@@ -42,16 +42,8 @@ const map = (n, start1, stop1, start2, stop2, clamp) => {
4242

4343
const serializationMap = {};
4444

45-
const SRGB_THRESHOLD = 0.03928;
46-
const SRGB_DIVISOR = 12.92;
47-
const GAMMA_OFFSET = 0.055;
48-
const GAMMA_DIVISOR = 1.055;
49-
const GAMMA_EXPONENT = 2.4;
50-
const LUMINANCE_RED = 0.2126;
51-
const LUMINANCE_GREEN = 0.7152;
52-
const LUMINANCE_BLUE = 0.0722;
53-
const EPSILON = 0.05;
54-
const THRESHOLD = 4.5;
45+
46+
5547

5648
class Color {
5749
// Reference to underlying color object depending on implementation
@@ -302,7 +294,6 @@ class Color {
302294
}
303295
return colorString;
304296
}
305-
306297
/**
307298
* Checks if two colors contrast ratio is WCAG 2.1 compliant and returns the ratio
308299
*
@@ -326,35 +317,12 @@ class Color {
326317
* </code>
327318
* </div>
328319
*/
329-
330-
contrast(new_other) {
331-
//Constants for contrast ratio cutoffs
332-
const CONTRAST_AA_NORMAL = 4.5;
333-
const CONTRAST_AA_LARGE = 3.0;
334-
const CONTRAST_AAA_NORMAL = 7.0;
335-
const CONTRAST_AAA_LARGE = 4.5;
336-
337-
//helper function for luminance aka brightness
338-
let luminance = (c) => {
339-
//putting RGB values into array and convert colors to value between 0-1
340-
let rgb = [red(c), green(c), blue(c)].map(v => {
341-
v /= 255;
342-
return v <= SRGB_THRESHOLD
343-
? v / SRGB_DIVISOR
344-
: Math.pow((v + GAMMA_OFFSET) / GAMMA_DIVISOR, GAMMA_EXPONENT);
345-
});
346-
return LUMINANCE_RED * rgb[0] + LUMINANCE_GREEN * rgb[1] + LUMINANCE_BLUE * rgb[2];
320+
contrast(new_other) {
321+
const contrast_method = 'WCAG21';
322+
const ratio = contrast(this._color, new_other._color, contrast_method);
323+
return ratio;
347324
};
348325

349-
let l1 = luminance(this);
350-
let l2 = luminance(new_other);
351-
//Epsilon to avoid dividing by zero
352-
let ratio = (Math.max(l1, l2) + EPSILON) / (Math.min(l1, l2) + EPSILON);
353-
354-
return { ratio, pass: ratio >= THRESHOLD };
355-
356-
};
357-
358326
/**
359327
* Sets the red component of a color.
360328
*

src/core/constants.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,18 @@ export const EXCLUDE = Symbol('exclude');
13711371
* @private
13721372
*/
13731373
export const JOIN = Symbol('join');
1374+
1375+
/**
1376+
* @typedef {'color-contrast-threshold-aa'} COLOR_CONTRAST_THRESHOLD_AA
1377+
* @property {COLOR_CONTRAST_THRESHOLD_AA} COLOR_CONTRAST_THRESHOLD_AA
1378+
* @final
1379+
*/
1380+
export const COLOR_CONTRAST_THRESHOLD_AA = 4.5;
1381+
1382+
1383+
/**
1384+
* @typedef {'color-contrast-threshold-aaa'} COLOR_CONTRAST_THRESHOLD_AAA
1385+
* @property {COLOR_CONTRAST_THRESHOLD_AAA} COLOR_CONTRAST_THRESHOLD_AAA
1386+
* @final
1387+
*/
1388+
export const COLOR_CONTRAST_THRESHOLD_AAA = 7.0;

0 commit comments

Comments
 (0)