diff --git a/src/Svg/Style.php b/src/Svg/Style.php index 1f07154..5234d47 100644 --- a/src/Svg/Style.php +++ b/src/Svg/Style.php @@ -169,7 +169,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; @@ -220,7 +220,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 @@ -238,7 +238,7 @@ static function parseColor($color) return self::getQuad($color); } - // RGB color + // HSL color if (strpos($color, "hsl") !== false) { $quad = self::getQuad($color, true); @@ -333,7 +333,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) { @@ -347,11 +347,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]); } } } @@ -361,7 +363,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); }