コード例 #1
0
ファイル: Color.php プロジェクト: poef/ariadne
 /**
  * Operations have to be done per-channel, if not,
  * channels will spill onto each other. Once we have
  * our result, in the form of an integer triplet,
  * we create a new color node to hold the result.
  *
  * @param ILess_Environment $env
  * @param string $op
  * @param ILess_Node $other
  * @return ILess_Node_Color
  * @throws InvalidArgumentException
  */
 public function operate(ILess_Environment $env, $op, ILess_Node $other)
 {
     $result = array();
     if (!$other instanceof ILess_Node_Color) {
         if (!self::methodExists($other, 'toColor')) {
             throw new InvalidArgumentException('The other node must implement toColor() method to operate');
         }
         $other = $other->toColor();
         if (!$other instanceof ILess_Node_Color) {
             throw new InvalidArgumentException('The toColor() method must return an instance of ILess_Node_Color');
         }
     }
     $t = $this->getRGB();
     $o = $other->getRGB();
     for ($c = 0; $c < 3; $c++) {
         $result[$c] = ILess_Math::operate($op, $t[$c], $o[$c]);
         if ($result[$c] > 255) {
             $result[$c] = 255;
         } elseif ($result < 0) {
             $result[$c] = 0;
         }
     }
     return new ILess_Node_Color($result, $this->color->getAlpha() + $other->color->getAlpha());
 }
コード例 #2
0
ファイル: Dimension.php プロジェクト: poef/ariadne
 /**
  * Operates with the dimension. In an operation between two dimensions,
  * we default to the first Dimension's unit,
  * so `1px + 2` will yield `3px`.
  *
  * @param ILess_Environment $env
  * @param string $op
  * @param ILess_Node_Dimension $other
  * @return ILess_Node_Dimension
  * @throws ILess_CompilerException
  */
 public function operate(ILess_Environment $env, $op, ILess_Node_Dimension $other)
 {
     $value = ILess_Math::operate($op, $this->value, $other->value);
     $unit = clone $this->unit;
     if ($op === '+' || $op === '-') {
         if (!count($unit->numerator) && !count($unit->denominator)) {
             $unit->numerator = $other->unit->numerator;
             $unit->denominator = $other->unit->denominator;
         } elseif (!count($other->unit->numerator) && !count($other->unit->denominator)) {
             // do nothing
         } else {
             $other = $other->convertTo($this->unit->usedUnits());
             if ($env->strictUnits && $other->unit->toString() !== $unit->toString()) {
                 throw new ILess_Exception_Compiler(sprintf('Incompatible units. Change the units or use the unit function. Bad units: \'%s\' and \'%s\'.', $unit->toString(), $other->unit->toString()));
             }
             $value = ILess_Math::operate($op, $this->value, $other->value);
         }
     } elseif ($op === '*') {
         $unit->numerator = array_merge($unit->numerator, $other->unit->numerator);
         $unit->denominator = array_merge($unit->denominator, $other->unit->denominator);
         sort($unit->numerator);
         sort($unit->denominator);
         $unit->cancel();
     } elseif ($op === '/') {
         $unit->numerator = array_merge($unit->numerator, $other->unit->denominator);
         $unit->denominator = array_merge($unit->denominator, $other->unit->numerator);
         sort($unit->numerator);
         sort($unit->denominator);
         $unit->cancel();
     }
     return new ILess_Node_Dimension($value, $unit);
 }