/**
  * @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 testByHex()
 {
     $this->assertSame('#000000', HtmlColor::byHex('#000')->__toString());
     $this->assertSame('#112233', HtmlColor::byHex('#123')->__toString());
     $this->assertSame('#123abc', HtmlColor::byHex('#123abc')->__toString());
     $this->assertSame('#123abc', HtmlColor::byHex('#123ABC')->__toString());
     $this->setExpectedException(\Exception::class);
     HtmlColor::byHex('no hex string');
 }