From 0d23b2abeebf518d3f1b562b02c3a5031f390856 Mon Sep 17 00:00:00 2001 From: Brian Sweeney Date: Mon, 12 Feb 2024 10:15:55 -0500 Subject: [PATCH] Return color quad as decimal array fixes #109 --- src/Svg/Style.php | 16 +++++++++------- tests/Svg/StyleTest.php | 34 +++++++++++++++++----------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/Svg/Style.php b/src/Svg/Style.php index 73d721c..9b4e1e1 100644 --- a/src/Svg/Style.php +++ b/src/Svg/Style.php @@ -162,7 +162,7 @@ protected function fillStyles($styles) $value = $this->color; } } - if (is_array($value) && $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; @@ -203,7 +203,7 @@ static function parseColor($color) } if ($color === "transparent") { - return [0,0,0,0]; + return [0.0, 0.0, 0.0, 0.0]; } // SVG color name @@ -221,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); @@ -316,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) { @@ -330,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]); } } } @@ -344,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])) { diff --git a/tests/Svg/StyleTest.php b/tests/Svg/StyleTest.php index cef731a..77e68da 100644 --- a/tests/Svg/StyleTest.php +++ b/tests/Svg/StyleTest.php @@ -18,26 +18,26 @@ public function test_parseColor() { $this->assertEquals("none", Style::parseColor("none")); $this->assertEquals("currentcolor", Style::parseColor("currentcolor")); - $this->assertEquals(array(0, 0, 0, 0), Style::parseColor("transparent")); - $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() @@ -52,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); }