/** * Return @darkColor if @color is > 43% luma otherwise return @lightColor, see notes * * @param ILess_Node_Color $color * @return string */ public function contrast(ILess_Node $color, ILess_Node_Color $darkColor = null, ILess_Node_Color $lightColor = null, ILess_Node_Dimension $thresholdPercentage = null) { // ping pong back // filter: contrast(3.2); // should be kept as is, so check for color if (!$color instanceof ILess_Node_Color) { if ($color instanceof ILess_Node_Dimension || !ILess_Node::methodExists($color, 'toColor')) { return; } $color = $color->toColor(); } if (!$lightColor) { $lightColor = $this->rgba(255, 255, 255, 1); } if (!$darkColor) { $darkColor = $this->rgba(0, 0, 0, 1); } //Figure out which is actually light and dark! if ($darkColor->getLuma(true) > $lightColor->getLuma(true)) { $t = $lightColor; $lightColor = $darkColor; $darkColor = $t; } if (!$thresholdPercentage) { $thresholdPercentage = 0.43; } else { $thresholdPercentage = $this->number($thresholdPercentage); } if ($color->getLuma(true) < $thresholdPercentage) { return $lightColor; } else { return $darkColor; } }
/** * @see ILess_Node::compile */ public function compile(ILess_Environment $env, $arguments = null, $important = null) { $rules = array(); $match = false; $isOneFound = false; $args = array(); foreach ($this->arguments as $a) { $args[] = array('name' => $a['name'], 'value' => $a['value']->compile($env)); } foreach ($env->frames as $frame) { $mixins = $frame->find($this->selector, $env); if (!$mixins) { continue; } $isOneFound = true; for ($m = 0; $m < count($mixins); $m++) { $mixin = $mixins[$m]; $isRecursive = false; foreach ($env->frames as $recurFrame) { if (!$mixin instanceof ILess_Node_MixinDefinition) { if (isset($recurFrame->originalRulesetId) && $mixin->rulesetId === $recurFrame->originalRulesetId || $mixin === $recurFrame) { $isRecursive = true; break; } } } if ($isRecursive) { continue; } if ($mixin->matchArgs($args, $env)) { if (!ILess_Node::methodExists($mixin, 'matchCondition') || $mixin->matchCondition($args, $env)) { try { if (!$mixin instanceof ILess_Node_MixinDefinition) { $mixin = new ILess_Node_MixinDefinition('', array(), $mixin->rules, null, false); $mixin->originalRulesetId = $mixins[$m]->originalRulesetId ? $mixins[$m]->originalRulesetId : $mixin->originalRulesetId; } $rules = array_merge($rules, $mixin->compile($env, $args, $this->important)->rules); } catch (Exception $e) { throw new ILess_Exception_Compiler($e->getMessage(), $this->index, $this->currentFileInfo, $e); } } $match = true; } } if ($match) { if (!$this->currentFileInfo || !$this->currentFileInfo->reference) { foreach ($rules as $rule) { if ($rule instanceof ILess_Node_MarkableAsReferencedInterface) { $rule->markReferenced(); } } } return $rules; } } if ($isOneFound) { $message = array(); if ($args) { foreach ($args as $a) { $argValue = ''; if ($a['name']) { $argValue .= $a['name'] . ':'; } if (ILess_Node::methodExists($a['value'], 'toCSS')) { $argValue .= $a['value']->toCSS($env); } else { $argValue .= '???'; } $message[] = $argValue; } } throw new ILess_Exception_Compiler(sprintf('No matching definition was found for `%s(%s)`', trim($this->selector->toCSS($env)), join(',', $message)), $this->index, $this->currentFileInfo); } else { throw new ILess_Exception_Compiler(sprintf('%s is undefined.', trim($this->selector->toCSS($env))), $this->index, $this->currentFileInfo); } }