Skip to content

Commit 535e674

Browse files
committed
Return color quad as decimal array
fixes #109
1 parent e39b7c0 commit 535e674

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/Svg/Style.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected function fillStyles($styles)
169169
$value = $this->color;
170170
}
171171
}
172-
if (is_array($value) && $value[3] !== 1 && array_key_exists("{$from}-opacity", $style_map) === true) {
172+
if (is_array($value) && $value[3] !== 1.0 && array_key_exists("{$from}-opacity", $style_map) === true) {
173173
$styles["{$from}-opacity"] = $value[3];
174174
}
175175
break;
@@ -220,7 +220,7 @@ static function parseColor($color)
220220
}
221221

222222
if ($color === "transparent") {
223-
return [0,0,0,0];
223+
return [0.0, 0.0, 0.0, 0.0];
224224
}
225225

226226
// SVG color name
@@ -238,7 +238,7 @@ static function parseColor($color)
238238
return self::getQuad($color);
239239
}
240240

241-
// RGB color
241+
// HSL color
242242
if (strpos($color, "hsl") !== false) {
243243
$quad = self::getQuad($color, true);
244244

@@ -333,7 +333,7 @@ static function getQuad($color, $percent = false) {
333333

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

339339
if (count($quad) != 3 && count($quad) != 4) {
@@ -347,11 +347,13 @@ static function getQuad($color, $percent = false) {
347347
if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
348348
$quad[$c] = floatval($quad[$c]) / 100;
349349
} else {
350-
$quad[$c] = $quad[$c] / 255;
350+
$quad[$c] = floatval($quad[$c]) / 255;
351351
}
352352
} else {
353353
if ($quad[$c][strlen($quad[$c]) - 1] === "%") {
354-
$quad[$c] = round(floatval($quad[$c]) * 2.55);
354+
$quad[$c] = floatval($quad[$c]) * 2.55;
355+
} else {
356+
$quad[$c] = floatval($quad[$c]);
355357
}
356358
}
357359
}
@@ -361,7 +363,7 @@ static function getQuad($color, $percent = false) {
361363

362364
static function parseHexColor($hex)
363365
{
364-
$c = array(0, 0, 0, 1);
366+
$c = array(0.0, 0.0, 0.0, 1.0);
365367

366368
// #FFFFFF
367369
if (isset($hex[6])) {

tests/Svg/StyleTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ public function test_parseColor()
1818
{
1919
$this->assertEquals("none", Style::parseColor("none"));
2020
$this->assertEquals("currentcolor", Style::parseColor("currentcolor"));
21-
$this->assertEquals(array(0, 0, 0, 0), Style::parseColor("transparent"));
22-
$this->assertEquals(array(255, 0, 0, 1), Style::parseColor("RED"));
23-
$this->assertEquals(array(0, 0, 255, 1), Style::parseColor("blue"));
24-
$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("black"));
25-
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("white"));
21+
$this->assertEquals(array(0.0, 0.0, 0.0, 0.0), Style::parseColor("transparent"));
22+
$this->assertEquals(array(255.0, 0.0, 0.0, 1.0), Style::parseColor("RED"));
23+
$this->assertEquals(array(0.0, 0.0, 255.0, 1.0), Style::parseColor("blue"));
24+
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("black"));
25+
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("white"));
2626

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

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

33-
$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("rgb(0,0,0)"));
34-
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("rgb(255,255,255)"));
35-
$this->assertEquals(array(0, 0, 0, 1), Style::parseColor("rgb(0, 0, 0)"));
36-
$this->assertEquals(array(255, 255, 255, 1), Style::parseColor("rgb(255, 255, 255)"));
37-
$this->assertEquals(array(255, 255, 255, .5), Style::parseColor("rgb(255, 255, 255, .5)"));
33+
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("rgb(0,0,0)"));
34+
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("rgb(255,255,255)"));
35+
$this->assertEquals(array(0.0, 0.0, 0.0, 1.0), Style::parseColor("rgb(0, 0, 0)"));
36+
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), Style::parseColor("rgb(255, 255, 255)"));
37+
$this->assertEquals(array(255.0, 255.0, 255.0, .5), Style::parseColor("rgb(255, 255, 255, .5)"));
3838

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

4343
public function test_fromAttributes()
@@ -52,8 +52,8 @@ public function test_fromAttributes()
5252

5353
$style->fromAttributes($attributes);
5454

55-
$this->assertEquals(array(0, 0, 255, 1), $style->color);
56-
$this->assertEquals(array(255, 255, 255, 1), $style->fill);
55+
$this->assertEquals(array(0.0, 0.0, 255.0, 1.0), $style->color);
56+
$this->assertEquals(array(255.0, 255.0, 255.0, 1.0), $style->fill);
5757
$this->assertEquals("none", $style->stroke);
5858
}
5959

0 commit comments

Comments
 (0)