Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update color parsing logic #105

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Svg/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ function _tagEnd($parser, $name)
break;
}

if (!$this->inDefs && $tag) {
if ((!$this->inDefs && $tag) || $tag instanceof StyleTag) {
$tag->handleEnd();
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/Svg/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function fillStyles($styles)
$value = $this->color;
}
}
if ($value !== null && $value[3] !== 1 && array_key_exists("{$from}-opacity", $style_map) === true) {
if (is_array($value) && $value[3] !== 1.0 && array_key_exists("{$from}-opacity", $style_map) === true) {
$styles["{$from}-opacity"] = $value[3];
}
break;
Expand Down Expand Up @@ -202,6 +202,10 @@ static function parseColor($color)
return "currentcolor";
}

if ($color === "transparent") {
return [0.0, 0.0, 0.0, 0.0];
}

// SVG color name
if (isset(self::$colorNames[$color])) {
return self::parseHexColor(self::$colorNames[$color]);
Expand All @@ -217,7 +221,7 @@ static function parseColor($color)
return self::getQuad($color);
}

// RGB color
// HSL color
if (strpos($color, "hsl") !== false) {
$quad = self::getQuad($color, true);

Expand Down Expand Up @@ -294,7 +298,8 @@ static function parseColor($color)
return null;
}

return trim(substr($color, $i + 1, $j - $i - 1));
//FIXME: gradients not supported?
return null; // trim(substr($color, $i + 1, $j - $i - 1));
}

return null;
Expand All @@ -311,7 +316,7 @@ static function getQuad($color, $percent = false) {

$quad = preg_split("/\\s*[,\\/]\\s*/", trim(substr($color, $i + 1, $j - $i - 1)));
if (!isset($quad[3])) {
$quad[3] = 1;
$quad[3] = "1";
}

if (count($quad) != 3 && count($quad) != 4) {
Expand All @@ -325,11 +330,13 @@ static function getQuad($color, $percent = false) {
if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
$quad[$c] = floatval($quad[$c]) / 100;
} else {
$quad[$c] = $quad[$c] / 255;
$quad[$c] = floatval($quad[$c]) / 255;
}
} else {
if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
$quad[$c] = round(floatval($quad[$c]) * 2.55);
$quad[$c] = floatval($quad[$c]) * 2.55;
} else {
$quad[$c] = floatval($quad[$c]);
}
}
}
Expand All @@ -339,7 +346,7 @@ static function getQuad($color, $percent = false) {

static function parseHexColor($hex)
{
$c = array(0, 0, 0, 1);
$c = array(0.0, 0.0, 0.0, 1.0);

// #FFFFFF
if (isset($hex[6])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Svg/Tag/AbstractTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function handle($attributes)
{
$this->attributes = $attributes;

if (!$this->getDocument()->inDefs) {
if (!$this->getDocument()->inDefs || $this instanceof StyleTag) {
$this->before($attributes);
$this->start($attributes);
}
}

public function handleEnd()
{
if (!$this->getDocument()->inDefs) {
if (!$this->getDocument()->inDefs || $this instanceof StyleTag) {
$this->end();
$this->after();
}
Expand Down
33 changes: 17 additions & 16 deletions tests/Svg/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ public function test_parseColor()
{
$this->assertEquals("none", Style::parseColor("none"));
$this->assertEquals("currentcolor", Style::parseColor("currentcolor"));
$this->assertEquals(array(255, 0, 0, 1), Style::parseColor("RED"));
$this->assertEquals(array(0, 0, 255, 1), Style::parseColor("blue"));
$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("black"));
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("white"));
$this->assertEquals(array(0.0, 0.0, 0.0, 0.0), Style::parseColor("transparent"));
$this->assertEquals(array(255.0, 0.0, 0.0, 1.0), Style::parseColor("RED"));
$this->assertEquals(array(0.0, 0.0, 255.0, 1.0), Style::parseColor("blue"));
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("black"));
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("white"));

$this->assertEquals(null, Style::parseColor("foo"));

$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("#000000"));
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("#ffffff"));
$this->assertEquals(array(0, 0, 0, .5), Style::parseColor("#00000080"));
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("#000000"));
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("#ffffff"));
$this->assertEquals(array(0.0, 0.0, 0.0, .5), Style::parseColor("#00000080"));

$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("rgb(0,0,0)"));
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("rgb(255,255,255)"));
$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("rgb(0, 0, 0)"));
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("rgb(255, 255, 255)"));
$this->assertEquals(array(255, 255, 255, .5), Style::parseColor("rgb(255, 255, 255, .5)"));
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("rgb(0,0,0)"));
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("rgb(255,255,255)"));
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("rgb(0, 0, 0)"));
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("rgb(255, 255, 255)"));
$this->assertEquals(array(255.0, 255.0, 255.0, .5), Style::parseColor("rgb(255, 255, 255, .5)"));

$this->assertEquals(array(255, 0, 0, 1), Style::parseColor("hsl(0, 100%, 50%)"));
$this->assertEquals(array(255, 0, 0, .5), Style::parseColor("hsl(0, 100%, 50%, .5)"));
$this->assertEquals(array(255.0, 0.0, 0.0, 1.0), Style::parseColor("hsl(0, 100%, 50%)"));
$this->assertEquals(array(255.0, 0.0, 0.0, .5), Style::parseColor("hsl(0, 100%, 50%, .5)"));
}

public function test_fromAttributes()
Expand All @@ -51,8 +52,8 @@ public function test_fromAttributes()

$style->fromAttributes($attributes);

$this->assertEquals(array(0, 0, 255, 1), $style->color);
$this->assertEquals(array(255, 255, 255, 1), $style->fill);
$this->assertEquals(array(0.0, 0.0, 255.0, 1.0), $style->color);
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), $style->fill);
$this->assertEquals("none", $style->stroke);
}

Expand Down
Loading