Example #1
0
 public function color($n)
 {
     if ($n instanceof Less_Tree_Quoted) {
         $colorCandidate = $n->value;
         $returnColor = Less_Tree_Color::fromKeyword($colorCandidate);
         if ($returnColor) {
             return $returnColor;
         }
         if (preg_match('/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/', $colorCandidate)) {
             return new Less_Tree_Color(substr($colorCandidate, 1));
         }
         throw new Less_Exception_Compiler("argument must be a color keyword or 3/6 digit hex e.g. #FFF");
     } else {
         throw new Less_Exception_Compiler("argument must be a string");
     }
 }
 /**
  * @param Less_Tree_Color $color1
  */
 public function mix($color1 = null, $color2 = null, $weight = null)
 {
     if (!$color1 instanceof Less_Tree_Color) {
         throw new Less_Exception_Compiler('The first argument to mix must be a color' . ($color1 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : ''));
     }
     if (!$color2 instanceof Less_Tree_Color) {
         throw new Less_Exception_Compiler('The second argument to mix must be a color' . ($color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : ''));
     }
     if (!$weight) {
         $weight = new Less_Tree_Dimension('50', '%');
     }
     if (!$weight instanceof Less_Tree_Dimension) {
         throw new Less_Exception_Compiler('The third argument to contrast must be a percentage' . ($weight instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : ''));
     }
     $p = $weight->value / 100.0;
     $w = $p * 2 - 1;
     $hsl1 = $color1->toHSL();
     $hsl2 = $color2->toHSL();
     $a = $hsl1['a'] - $hsl2['a'];
     $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2;
     $w2 = 1 - $w1;
     $rgb = array($color1->rgb[0] * $w1 + $color2->rgb[0] * $w2, $color1->rgb[1] * $w1 + $color2->rgb[1] * $w2, $color1->rgb[2] * $w1 + $color2->rgb[2] * $w2);
     $alpha = $color1->alpha * $p + $color2->alpha * (1 - $p);
     return new Less_Tree_Color($rgb, $alpha);
 }