Skip to content

Commit bcc8ced

Browse files
committed
Merge pull request #2754 from Synchro/master
Update contrast function and tests, fixes #2743
2 parents 2f07cd7 + 6dd409a commit bcc8ced

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

lib/less/functions/color.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -266,33 +266,41 @@ colorFunctions = {
266266
greyscale: function (color) {
267267
return colorFunctions.desaturate(color, new Dimension(100));
268268
},
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.
270274
// filter: contrast(3.2);
271275
// should be kept as is, so check for color
272276
if (!color.rgb) {
273277
return null;
274278
}
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);
277281
}
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);
280284
}
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);
286294
}
287-
if (typeof threshold === 'undefined') {
288-
threshold = 0.43;
295+
if (luma > luma2) {
296+
contrast2 = (luma + 0.05) / (luma2 + 0.05);
289297
} else {
290-
threshold = number(threshold);
298+
contrast2 = (luma2 + 0.05) / (luma + 0.05);
291299
}
292-
if (color.luma() < threshold) {
293-
return light;
300+
if (contrast1 > contrast2) {
301+
return color1;
294302
} else {
295-
return dark;
303+
return color2;
296304
}
297305
},
298306
argb: function (color) {

test/css/functions.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
saturate-filter: saturate(5%);
3939
contrast-white: #000000;
4040
contrast-black: #ffffff;
41-
contrast-red: #ffffff;
41+
contrast-red: #000000;
4242
contrast-green: #000000;
4343
contrast-blue: #ffffff;
4444
contrast-yellow: #000000;
@@ -49,11 +49,11 @@
4949
contrast-light-thresh: #111111;
5050
contrast-dark-thresh: #eeeeee;
5151
contrast-high-thresh: #eeeeee;
52-
contrast-low-thresh: #111111;
52+
contrast-low-thresh: #eeeeee;
5353
contrast-light-thresh-per: #111111;
5454
contrast-dark-thresh-per: #eeeeee;
5555
contrast-high-thresh-per: #eeeeee;
56-
contrast-low-thresh-per: #111111;
56+
contrast-low-thresh-per: #eeeeee;
5757
replace: "Hello, World!";
5858
replace-captured: "This is a new string.";
5959
replace-with-flags: "2 + 2 = 4";

0 commit comments

Comments
 (0)