/** * Creates the variable. Detects the type. * * @param string $name The name of the variable * @param mixed $value The value of the variable * * @return 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 (Color::isNamedColor($value) || strtolower($value) === 'transparent' || strpos($value, '#') === 0) { $value = new ColorNode(new Color($value)); } elseif (preg_match(self::RGBA_COLOR_REGEXP, $value, $matches)) { // RGB(A) colors $value = new ColorNode(new Color([$matches[1], $matches[2], $matches[3]], isset($matches[4]) ? $matches[4] : 1)); } elseif (preg_match(self::QUOTED_REGEXP, $value, $matches)) { $value = new QuotedNode($matches[0], $matches[0][0] == '"' ? $matches[1] : $matches[2]); } elseif (strpos($value, 'http://') === 0 || strpos($value, 'https://') === 0) { $value = new AnonymousNode($value); } elseif (preg_match(self::DIMENSION_REGEXP, $value, $matches)) { $value = new DimensionNode($matches[1], isset($matches[2]) ? $matches[2] : null); } else { $value = new AnonymousNode($value); } return new self($name, $value, $important); }
/** * @covers toString */ public function testToString() { $color = new Color('#ffeeaa'); $this->assertEquals('#ffeeaa', $color->toString()); // the format remains the same $color = new Color('#fea'); $this->assertEquals('#ffeeaa', $color->toString()); $color = new Color('#ff0000'); $this->assertEquals('#ff0000', $color->toString()); $this->assertEquals(255, $color->getRed()); $this->assertEquals(0, $color->getGreen()); $this->assertEquals(0, $color->getBlue()); $color = new Color('#f60000'); $this->assertEquals('#f60000', $color->toString()); $this->assertEquals(246, $color->getRed()); $this->assertEquals(0, $color->getGreen()); $this->assertEquals(0, $color->getBlue()); }
/** * Color blending * * @param callable $mode * @param Color $color1 * @param Color $color2 * @return ColorNode */ protected function colorBlend(callable $mode, Color $color1, Color $color2) { $ab = $color1->getAlpha(); // backdrop $as = $color2->getAlpha(); // source $r = []; // result $ar = $as + $ab * (1 - $as); $rgb1 = $color1->rgb; $rgb2 = $color2->rgb; for ($i = 0; $i < 3; $i++) { $cb = $rgb1[$i] / 255; $cs = $rgb2[$i] / 255; $cr = call_user_func($mode, $cb, $cs); if ($ar) { $cr = ($as * $cs + $ab * ($cb - $as * ($cb + $cs - $cr))) / $ar; } $r[$i] = $cr * 255; } return new ColorNode($r, $ar); }
/** * Creates new color from the keyword * * @param string $keyword * @return Color */ public static function fromKeyword($keyword) { $color = null; // is this named color? if (self::isNamedColor($keyword)) { $color = new Color(substr(Color::color($keyword), 1)); $color->keyword = $keyword; } elseif ($keyword === 'transparent') { $color = new Color([255, 255, 255], 0); $color->isTransparentKeyword = true; } return $color; }
/** * A catch-all word, such as: `black border-collapse`. * * @return ColorNode|KeywordNode|null */ protected function parseEntitiesKeyword() { $k = $this->input->char('%'); if (!$k) { $k = $this->input->re('/\\G[_A-Za-z-][_A-Za-z0-9-]*/'); } if ($k) { // detected named color and "transparent" keyword if ($color = Color::fromKeyword($k)) { return new ColorNode($color); } else { return new KeywordNode($k); } } }