/** * Creates the variable. Detects the type. * * @param string $name The name of the variable * @param mixed $value The value of the variable * @return ILess_Variable */ public static function create($name, $value) { $important = false; // name is marked as !name if (strpos($name, '!') === 0) { $important = true; $name = substr($name, 1); } // Color if (ILess_Color::isNamedColor($value) || strtolower($value) === 'transparent' || strpos($value, '#') === 0) { $value = new ILess_Node_Color(new ILess_Color($value)); } elseif (preg_match(self::RGBA_COLOR_REGEXP, $value, $matches)) { // RGB(A) colors $value = new ILess_Node_Color(new ILess_Color(array($matches[1], $matches[2], $matches[3]), isset($matches[4]) ? $matches[4] : 1)); } elseif (preg_match(self::QUOTED_REGEXP, $value, $matches)) { $value = new ILess_Node_Quoted($matches[0], $matches[0][0] == '"' ? $matches[1] : $matches[2]); } elseif (strpos($value, 'http://') === 0 || strpos($value, 'https://') === 0) { $value = new ILess_Node_Anonymous($value); } elseif (preg_match(self::DIMENSION_REGEXP, $value, $matches)) { $value = new ILess_Node_Dimension($matches[1], isset($matches[2]) ? $matches[2] : null); } else { $value = new ILess_Node_Anonymous($value); } return new ILess_Variable($name, $value, $important); }
/** * @covers toString */ public function testToString() { $color = new ILess_Color('#ffeeaa'); $this->assertEquals('#ffeeaa', $color->toString()); // the format remains the same $color = new ILess_Color('#fea'); $this->assertEquals('#ffeeaa', $color->toString()); $color = new ILess_Color('#ff0000'); $this->assertEquals('#ff0000', $color->toString()); $this->assertEquals(255, $color->getRed()); $this->assertEquals(0, $color->getGreen()); $this->assertEquals(0, $color->getBlue()); $color = new ILess_Color('#f60000'); $this->assertEquals('#f60000', $color->toString()); $this->assertEquals(246, $color->getRed()); $this->assertEquals(0, $color->getGreen()); $this->assertEquals(0, $color->getBlue()); }
/** * 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()); }
/** * A catch-all word, such as: `black border-collapse` * * @return ILess_Node_Color|ILess_Node_Keyword */ protected function parseEntitiesKeyword() { if ($k = $this->matchReg('/\\G[_A-Za-z-][_A-Za-z0-9-]*/')) { // detected named color and "transparent" keyword if ($color = ILess_Color::fromKeyword($k)) { return new ILess_Node_Color($color); } else { return new ILess_Node_Keyword($k); } } }
/** * Return a color 10% points *darker* * * @param ILess_Node $color * @param ILess_Node * @return string * @throws InvalidArgumentException If the node is invalid */ public function darken(ILess_Node $color, ILess_Node_Dimension $percentage = null) { // this is a keyword if ($color instanceof ILess_Node_Keyword && ILess_Color::isNamedColor($color->value)) { $color = new ILess_Node_Color(ILess_Color::color($color->value)); } if (!$color instanceof ILess_Node_Color) { throw new InvalidArgumentException('Cannot darken the color. Invalid color given.'); } $percentage = $percentage ? $percentage->value / 100 : 10; $lightness = $this->clamp($color->getLightness(true) - $percentage); return $this->hsla($color->getHue(true), $color->getSaturation(true), $lightness, $color->getAlpha(true)); }
/** * Creates new color from the keyword * * @param string $keyword * @return ILess_Color */ public static function fromKeyword($keyword) { $color = null; // is this named color? if (self::isNamedColor($keyword)) { $color = new ILess_Color(substr(ILess_Color::color($keyword), 1)); $color->keyword = $keyword; } elseif ($keyword === 'transparent') { $color = new ILess_Color(array(255, 255, 255), 0); $color->isTransparentKeyword = true; } return $color; }