Skip to content

Commit f5a1f34

Browse files
atkayeKrinkle
authored andcommitted
contrast() now accepts percentage threshold
Fixes #501. Closes #523.
1 parent f65faed commit f5a1f34

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

lessc.inc.php

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,36 +1283,43 @@ protected function lib_mix($args) {
12831283
}
12841284

12851285
protected function lib_contrast($args) {
1286-
$darkColor = array('color', 0, 0, 0);
1287-
$lightColor = array('color', 255, 255, 255);
1288-
$threshold = 0.43;
1289-
1290-
if ( $args[0] == 'list' ) {
1291-
$inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0]) : $lightColor;
1292-
$darkColor = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1]) : $darkColor;
1293-
$lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2]) : $lightColor;
1294-
$threshold = ( isset($args[2][3]) ) ? $this->assertNumber($args[2][3]) : $threshold;
1295-
}
1296-
else {
1297-
$inputColor = $this->assertColor($args);
1298-
}
1299-
1300-
$inputColor = $this->coerceColor($inputColor);
1301-
$darkColor = $this->coerceColor($darkColor);
1302-
$lightColor = $this->coerceColor($lightColor);
1303-
1304-
//Figure out which is actually light and dark!
1305-
if ( $this->toLuma($darkColor) > $this->toLuma($lightColor) ) {
1306-
$t = $lightColor;
1307-
$lightColor = $darkColor;
1308-
$darkColor = $t;
1309-
}
1310-
1311-
$inputColor_alpha = $this->lib_alpha($inputColor);
1312-
if ( ( $this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1313-
return $lightColor;
1314-
}
1315-
return $darkColor;
1286+
$darkColor = array('color', 0, 0, 0);
1287+
$lightColor = array('color', 255, 255, 255);
1288+
$threshold = 0.43;
1289+
1290+
if ( $args[0] == 'list' ) {
1291+
$inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0]) : $lightColor;
1292+
$darkColor = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1]) : $darkColor;
1293+
$lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2]) : $lightColor;
1294+
1295+
if ( isset($args[2][3]) ) {
1296+
if ( isset($args[2][3][2]) && $args[2][3][2] === '%' ) {
1297+
$args[2][3][1] /= 100;
1298+
unset($args[2][3][2]);
1299+
}
1300+
$threshold = $this->assertNumber($args[2][3]);
1301+
}
1302+
}
1303+
else {
1304+
$inputColor = $this->assertColor($args);
1305+
}
1306+
1307+
$inputColor = $this->coerceColor($inputColor);
1308+
$darkColor = $this->coerceColor($darkColor);
1309+
$lightColor = $this->coerceColor($lightColor);
1310+
1311+
//Figure out which is actually light and dark!
1312+
if ( $this->toLuma($darkColor) > $this->toLuma($lightColor) ) {
1313+
$t = $lightColor;
1314+
$lightColor = $darkColor;
1315+
$darkColor = $t;
1316+
}
1317+
1318+
$inputColor_alpha = $this->lib_alpha($inputColor);
1319+
if ( ( $this->toLuma($inputColor) * $inputColor_alpha) < $threshold) {
1320+
return $lightColor;
1321+
}
1322+
return $darkColor;
13161323
}
13171324

13181325
private function toLuma($color) {

tests/inputs/colors.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ fade {
9898
.contrast {
9999
color1: contrast(#000, red, blue);
100100
color2: contrast(#fff, red, blue);
101+
color3: contrast(#333, #000, #fff); // Default threshold
102+
color4: contrast(#333, #000, #fff, 0.1); // Threshold weighted towards dark colour
103+
color5: contrast(#333, #000, #fff, 10%); // Threshold weighted as percentage
101104
}
102105

103106
.luma {

tests/outputs/colors.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ fade {
6868
.contrast {
6969
color1: #ff0000;
7070
color2: #0000ff;
71+
color3: #ffffff;
72+
color4: #000000;
73+
color5: #000000;
7174
}
7275
.luma {
7376
color: 44.11161568%;

0 commit comments

Comments
 (0)