/**
  * @param HtmlColor|string|null $color
  * @param float $opacity
  *
  * @return $this
  * @throws \Exception
  */
 public function setColor($color, $opacity = 1.0)
 {
     if (null === $color) {
         $this->color = null;
         $this->opacity = 0.0;
     } elseif ($color instanceof HtmlColor) {
         $this->color = $color;
         $this->opacity = (double) $opacity;
     } elseif (is_string($color)) {
         if (preg_match(HtmlColor::PATTERN_HEX, $color, $matches)) {
             $this->color = HtmlColor::byHex($color);
             $this->opacity = (double) $opacity;
         } elseif (preg_match(HtmlColor::PATTERN_RGB, $color, $matches)) {
             $this->color = HtmlColor::rgb((int) $matches[1], (int) $matches[2], (int) $matches[3]);
             $this->opacity = (double) $opacity;
         } elseif (preg_match(HtmlColor::PATTERN_RGBA, $color, $matches)) {
             $this->color = HtmlColor::rgb((int) $matches[1], (int) $matches[2], (int) $matches[3]);
             $this->opacity = (double) $matches[4];
         } else {
             $this->color = HtmlColor::byName($color);
             $this->opacity = (double) $opacity;
         }
     } else {
         throw new InvalidArgumentException('invalid color: ' . get_class($color));
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  */
 public function testByName()
 {
     $this->assertSame('black', HtmlColor::byName('black')->__toString());
     $this->assertSame('green', HtmlColor::byName('green')->__toString());
 }