From f5a1f3446d12feca7720280600f30c45260264c6 Mon Sep 17 00:00:00 2001 From: Alex Kaye Date: Sat, 21 Dec 2013 17:51:45 +0000 Subject: [PATCH] contrast() now accepts percentage threshold Fixes #501. Closes #523. --- lessc.inc.php | 67 ++++++++++++++++++++++------------------ tests/inputs/colors.less | 3 ++ tests/outputs/colors.css | 3 ++ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/lessc.inc.php b/lessc.inc.php index cfff748c..d369d3a7 100644 --- a/lessc.inc.php +++ b/lessc.inc.php @@ -1283,36 +1283,43 @@ protected function lib_mix($args) { } protected function lib_contrast($args) { - $darkColor = array('color', 0, 0, 0); - $lightColor = array('color', 255, 255, 255); - $threshold = 0.43; - - if ( $args[0] == 'list' ) { - $inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0]) : $lightColor; - $darkColor = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1]) : $darkColor; - $lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2]) : $lightColor; - $threshold = ( isset($args[2][3]) ) ? $this->assertNumber($args[2][3]) : $threshold; - } - else { - $inputColor = $this->assertColor($args); - } - - $inputColor = $this->coerceColor($inputColor); - $darkColor = $this->coerceColor($darkColor); - $lightColor = $this->coerceColor($lightColor); - - //Figure out which is actually light and dark! - if ( $this->toLuma($darkColor) > $this->toLuma($lightColor) ) { - $t = $lightColor; - $lightColor = $darkColor; - $darkColor = $t; - } - - $inputColor_alpha = $this->lib_alpha($inputColor); - if ( ( $this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { - return $lightColor; - } - return $darkColor; + $darkColor = array('color', 0, 0, 0); + $lightColor = array('color', 255, 255, 255); + $threshold = 0.43; + + if ( $args[0] == 'list' ) { + $inputColor = ( isset($args[2][0]) ) ? $this->assertColor($args[2][0]) : $lightColor; + $darkColor = ( isset($args[2][1]) ) ? $this->assertColor($args[2][1]) : $darkColor; + $lightColor = ( isset($args[2][2]) ) ? $this->assertColor($args[2][2]) : $lightColor; + + if ( isset($args[2][3]) ) { + if ( isset($args[2][3][2]) && $args[2][3][2] === '%' ) { + $args[2][3][1] /= 100; + unset($args[2][3][2]); + } + $threshold = $this->assertNumber($args[2][3]); + } + } + else { + $inputColor = $this->assertColor($args); + } + + $inputColor = $this->coerceColor($inputColor); + $darkColor = $this->coerceColor($darkColor); + $lightColor = $this->coerceColor($lightColor); + + //Figure out which is actually light and dark! + if ( $this->toLuma($darkColor) > $this->toLuma($lightColor) ) { + $t = $lightColor; + $lightColor = $darkColor; + $darkColor = $t; + } + + $inputColor_alpha = $this->lib_alpha($inputColor); + if ( ( $this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { + return $lightColor; + } + return $darkColor; } private function toLuma($color) { diff --git a/tests/inputs/colors.less b/tests/inputs/colors.less index 588bfb69..eafc1700 100644 --- a/tests/inputs/colors.less +++ b/tests/inputs/colors.less @@ -98,6 +98,9 @@ fade { .contrast { color1: contrast(#000, red, blue); color2: contrast(#fff, red, blue); + color3: contrast(#333, #000, #fff); // Default threshold + color4: contrast(#333, #000, #fff, 0.1); // Threshold weighted towards dark colour + color5: contrast(#333, #000, #fff, 10%); // Threshold weighted as percentage } .luma { diff --git a/tests/outputs/colors.css b/tests/outputs/colors.css index 0826a135..30bc287b 100644 --- a/tests/outputs/colors.css +++ b/tests/outputs/colors.css @@ -68,6 +68,9 @@ fade { .contrast { color1: #ff0000; color2: #0000ff; + color3: #ffffff; + color4: #000000; + color5: #000000; } .luma { color: 44.11161568%;