Skip to content

Commit

Permalink
Merge branch 'color-parsing' into qa
Browse files Browse the repository at this point in the history
  • Loading branch information
bsweeney committed Feb 12, 2024
2 parents 57384e2 + 0d23b2a commit e8fcbe4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
16 changes: 9 additions & 7 deletions src/Svg/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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]);
}
}
}
Expand All @@ -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])) {
Expand Down
34 changes: 17 additions & 17 deletions tests/Svg/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

Expand Down

0 comments on commit e8fcbe4

Please sign in to comment.