88
99import { RGB , RGBHDR , HSL , HSB , HWB , LAB , LCH , OKLAB , OKLCH } from './creating_reading' ;
1010
11+
1112import {
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-
3232import HSBSpace from './color_spaces/hsb.js' ;
3333
3434const map = ( n , start1 , stop1 , start2 , stop2 , clamp ) => {
@@ -42,16 +42,8 @@ const map = (n, start1, stop1, start2, stop2, clamp) => {
4242
4343const 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
5648class 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 *
0 commit comments