@@ -266,33 +266,41 @@ colorFunctions = {
266
266
greyscale : function ( color ) {
267
267
return colorFunctions . desaturate ( color , new Dimension ( 100 ) ) ;
268
268
} ,
269
- contrast : function ( color , dark , light , threshold ) {
269
+ contrast : function ( color , color1 , color2 , threshold ) {
270
+ // Return which of `color1` and `color2` has the greatest contrast with `color`
271
+ // according to the standard WCAG contrast ratio calculation.
272
+ // http://www.w3.org/TR/WCAG20/#contrast-ratiodef
273
+ // The threshold param is no longer used, in line with SASS.
270
274
// filter: contrast(3.2);
271
275
// should be kept as is, so check for color
272
276
if ( ! color . rgb ) {
273
277
return null ;
274
278
}
275
- if ( typeof light === 'undefined' ) {
276
- light = colorFunctions . rgba ( 255 , 255 , 255 , 1.0 ) ;
279
+ if ( typeof color1 === 'undefined' ) {
280
+ color1 = colorFunctions . rgba ( 0 , 0 , 0 , 1.0 ) ;
277
281
}
278
- if ( typeof dark === 'undefined' ) {
279
- dark = colorFunctions . rgba ( 0 , 0 , 0 , 1.0 ) ;
282
+ if ( typeof color2 === 'undefined' ) {
283
+ color2 = colorFunctions . rgba ( 255 , 255 , 255 , 1.0 ) ;
280
284
}
281
- //Figure out which is actually light and dark!
282
- if ( dark . luma ( ) > light . luma ( ) ) {
283
- var t = light ;
284
- light = dark ;
285
- dark = t ;
285
+ var contrast1 , contrast2 ;
286
+ var luma = color . luma ( ) ;
287
+ var luma1 = color1 . luma ( ) ;
288
+ var luma2 = color2 . luma ( ) ;
289
+ // Calculate contrast ratios for each color
290
+ if ( luma > luma1 ) {
291
+ contrast1 = ( luma + 0.05 ) / ( luma1 + 0.05 ) ;
292
+ } else {
293
+ contrast1 = ( luma1 + 0.05 ) / ( luma + 0.05 ) ;
286
294
}
287
- if ( typeof threshold === 'undefined' ) {
288
- threshold = 0.43 ;
295
+ if ( luma > luma2 ) {
296
+ contrast2 = ( luma + 0.05 ) / ( luma2 + 0.05 ) ;
289
297
} else {
290
- threshold = number ( threshold ) ;
298
+ contrast2 = ( luma2 + 0.05 ) / ( luma + 0.05 ) ;
291
299
}
292
- if ( color . luma ( ) < threshold ) {
293
- return light ;
300
+ if ( contrast1 > contrast2 ) {
301
+ return color1 ;
294
302
} else {
295
- return dark ;
303
+ return color2 ;
296
304
}
297
305
} ,
298
306
argb : function ( color ) {
0 commit comments