Skip to content

Commit d869430

Browse files
authored
Merge pull request #5 from PackageFactory/analysis-pegoka
Apply fixes from StyleCI
2 parents e753af6 + d315ade commit d869430

File tree

4 files changed

+77
-50
lines changed

4 files changed

+77
-50
lines changed

Classes/Domain/ValueObject/HslaColor.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function getAlpha(): float
8888

8989
/**
9090
* @return RgbaColor
91+
*
9192
* @see http://en.wikipedia.org/wiki/HSL_color_space.
9293
* @see https://gist.github.com/mjackson/5311256
9394
*/
@@ -100,15 +101,16 @@ public function asRgba(): RgbaColor
100101

101102
if ($s == 0) {
102103
$rgb = $l * 255;
104+
103105
return new RgbaColor($rgb, $rgb, $rgb, $this->alpha * 255);
104106
}
105107

106108
$q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s;
107109
$p = 2 * $l - $q;
108110

109-
$r = $this->hue2rgb($p, $q, $h + 1/3);
111+
$r = $this->hue2rgb($p, $q, $h + 1 / 3);
110112
$g = $this->hue2rgb($p, $q, $h);
111-
$b = $this->hue2rgb($p, $q, $h - 1/3);
113+
$b = $this->hue2rgb($p, $q, $h - 1 / 3);
112114

113115
return new RgbaColor($r * 255, $g * 255, $b * 255, $a * 255);
114116
}
@@ -117,28 +119,38 @@ public function asRgba(): RgbaColor
117119
* @param float $p
118120
* @param float $q
119121
* @param float $t
122+
*
120123
* @return float
121124
*/
122125
private function hue2rgb(float $p, float $q, float $t): float
123126
{
124-
if($t < 0) $t += 1;
125-
if($t > 1) $t -= 1;
126-
if($t < 1/6) return $p + ($q - $p) * 6 * $t;
127-
if($t < 1/2) return $q;
128-
if($t < 2/3) return $p + ($q - $p) * (2/3 - $t) * 6;
127+
if ($t < 0) {
128+
$t += 1;
129+
}
130+
if ($t > 1) {
131+
$t -= 1;
132+
}
133+
if ($t < 1 / 6) {
134+
return $p + ($q - $p) * 6 * $t;
135+
}
136+
if ($t < 1 / 2) {
137+
return $q;
138+
}
139+
if ($t < 2 / 3) {
140+
return $p + ($q - $p) * (2 / 3 - $t) * 6;
141+
}
142+
129143
return $p;
130144
}
131145

132146
/**
133147
* @return HslaColor
134148
*/
135-
public function asHsla(): HslaColor
149+
public function asHsla(): self
136150
{
137151
return $this;
138152
}
139153

140-
141-
142154
/**
143155
* @param float $delta 0..100
144156
*

Classes/Domain/ValueObject/RgbaColor.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ public function getAlpha(): float
9292
/**
9393
* @return RgbaColor
9494
*/
95-
public function asRgba(): RgbaColor
95+
public function asRgba(): self
9696
{
9797
return $this;
9898
}
9999

100100
/**
101101
* @return HslaColor
102+
*
102103
* @see http://en.wikipedia.org/wiki/HSL_color_space.
103104
* @see https://gist.github.com/mjackson/5311256
104105
*/
@@ -116,7 +117,6 @@ public function asHsla(): HslaColor
116117
if ($max == $min) {
117118
$h = $s = 0;
118119
} else {
119-
120120
$d = $max - $min;
121121
$s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min);
122122

@@ -139,7 +139,6 @@ public function asHsla(): HslaColor
139139
);
140140
}
141141

142-
143142
/**
144143
* @param float $delta
145144
*

Classes/Eel/ColorBuilder.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function hex(string $hex): ?ColorHelper
5555
{
5656
$hex = strtolower($hex);
5757
if (preg_match(self::PATTERN_HEX_SHORT, $hex, $matches)) {
58-
5958
$red = hexdec($matches['red'].$matches['red']);
6059
$green = hexdec($matches['green'].$matches['green']);
6160
$blue = hexdec($matches['blue'].$matches['blue']);
@@ -65,7 +64,6 @@ public function hex(string $hex): ?ColorHelper
6564
new RgbaColor($red, $green, $blue, $alpha)
6665
);
6766
} elseif (preg_match(self::PATTERN_HEX_LONG, $hex, $matches)) {
68-
6967
$red = hexdec($matches['red']);
7068
$green = hexdec($matches['green']);
7169
$blue = hexdec($matches['blue']);
@@ -75,6 +73,7 @@ public function hex(string $hex): ?ColorHelper
7573
new RgbaColor($red, $green, $blue, $alpha)
7674
);
7775
}
76+
7877
return null;
7978
}
8079

@@ -141,16 +140,26 @@ protected function parseNumber(string $value, int $max = 255, bool $circle = fal
141140
$number = (int) (
142141
substr($value, 0, -1)
143142
);
143+
144144
return $max * ($number / 100);
145145
} else {
146146
$value = (float) $value;
147147
if ($circle) {
148-
if ($value < 0) $value = $max + ($value % $max);
149-
if ($value > $max) $value = $value % $max;
148+
if ($value < 0) {
149+
$value = $max + ($value % $max);
150+
}
151+
if ($value > $max) {
152+
$value = $value % $max;
153+
}
150154
} else {
151-
if ($value < 0) $value = 0;
152-
if ($value > $max) $value = $max;
155+
if ($value < 0) {
156+
$value = 0;
157+
}
158+
if ($value > $max) {
159+
$value = $max;
160+
}
153161
}
162+
154163
return $value;
155164
}
156165
}

Tests/Unit/AbstractColorTest.php

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
2+
23
namespace PackageFactory\ColorHelper\Tests\Unit;
34

4-
use PHPUnit\Framework\TestCase;
55
use PackageFactory\ColorHelper\Domain\ValueObject\ColorInterface;
66
use PackageFactory\ColorHelper\Domain\ValueObject\HslaColor;
77
use PackageFactory\ColorHelper\Domain\ValueObject\RgbaColor;
8+
use PHPUnit\Framework\TestCase;
89
use Symfony\Component\Yaml\Yaml;
910

1011
abstract class AbstractColorTest extends TestCase
1112
{
1213
/**
13-
* Return a set of colors as hls rgb and hex
14+
* Return a set of colors as hls rgb and hex.
15+
*
1416
* @return array
1517
*/
1618
public function getColorFixtures(): array
@@ -34,62 +36,65 @@ function ($item) {
3436
}
3537

3638
/**
37-
* Create a full range of rgb values to test the full color spectrum
39+
* Create a full range of rgb values to test the full color spectrum.
40+
*
3841
* @return array
3942
*/
40-
public function rgbSpectrumDataProvider():array
43+
public function rgbSpectrumDataProvider(): array
4144
{
4245
$interval = 20;
4346
$testArgumentSets = [];
44-
for ($r = 0; $r < 256; $r+=$interval) {
45-
for ($g = 0; $g < 256; $g+=$interval) {
46-
for ($b = 0; $b < 256; $b+=$interval) {
47-
$testArgumentSets[] = [$r,$g,$b];
47+
for ($r = 0; $r < 256; $r += $interval) {
48+
for ($g = 0; $g < 256; $g += $interval) {
49+
for ($b = 0; $b < 256; $b += $interval) {
50+
$testArgumentSets[] = [$r, $g, $b];
4851
}
4952
}
5053
}
54+
5155
return $testArgumentSets;
5256
}
5357

5458
/**
55-
* Create a full range of hsl values to test the full color spectrum
59+
* Create a full range of hsl values to test the full color spectrum.
60+
*
5661
* @return array
5762
*/
58-
public function hlsSpectrumDataProvider():array
63+
public function hlsSpectrumDataProvider(): array
5964
{
6065
$interval = 20;
6166
$start = 5;
6267

6368
$testArgumentSets = [];
64-
for ($h = $start; $h < 360; $h+=$interval) {
65-
for ($l =$start; $l < 100; $l+=$interval) {
66-
for ($s = $start; $s < 100; $s+=$interval) {
69+
for ($h = $start; $h < 360; $h += $interval) {
70+
for ($l = $start; $l < 100; $l += $interval) {
71+
for ($s = $start; $s < 100; $s += $interval) {
6772
$testArgumentSets[] = [$h, $l, $s];
6873
}
6974
}
7075
}
7176

7277
// test the extremes
73-
$testArgumentSets[] = [0,0,0];
74-
$testArgumentSets[] = [0,0,100];
78+
$testArgumentSets[] = [0, 0, 0];
79+
$testArgumentSets[] = [0, 0, 100];
7580

7681
return $testArgumentSets;
7782
}
7883

79-
8084
/**
8185
* @param ColorInterface $expected
8286
* @param ColorInterface $color
8387
*/
84-
protected function assertSimilarColor(ColorInterface $expected, ColorInterface $color, string $message = null) {
88+
protected function assertSimilarColor(ColorInterface $expected, ColorInterface $color, string $message = null)
89+
{
8590
$this->addToAssertionCount(1);
8691
if ($expected instanceof RgbaColor) {
8792
if (!$this->isSimilarRgba($expected, $color)) {
88-
$this->fail( $message ?? $color->getRgbaString() . ' is not similar to ' . $expected->getRgbaString());
93+
$this->fail($message ?? $color->getRgbaString().' is not similar to '.$expected->getRgbaString());
8994
}
9095
} elseif ($expected instanceof HslaColor) {
9196
if (!$this->isSimilarHsla($expected, $color)) {
92-
$this->fail( $message ?? $color->getHslaString() . ' is not similar to ' . $expected->getHslaString());
97+
$this->fail($message ?? $color->getHslaString().' is not similar to '.$expected->getHslaString());
9398
}
9499
}
95100
}
@@ -98,18 +103,19 @@ protected function assertSimilarColor(ColorInterface $expected, ColorInterface $
98103
* @param ColorInterface $expected
99104
* @param ColorInterface $color
100105
*/
101-
protected function assertSameColor(ColorInterface $expected, ColorInterface $color, string $message = null) {
106+
protected function assertSameColor(ColorInterface $expected, ColorInterface $color, string $message = null)
107+
{
102108
$this->addToAssertionCount(1);
103109
if (get_class($expected) != get_class($color)) {
104-
$this->fail( $message ?? get_class($expected) . ' is not the same as ' . get_class($color));
110+
$this->fail($message ?? get_class($expected).' is not the same as '.get_class($color));
105111
} elseif ($color->equals($expected) == false) {
106112
if ($expected instanceof RgbaColor) {
107113
if (!$this->isSimilarRgba($expected, $color, 0)) {
108-
$this->fail( $message ?? $color->getRgbaString() . ' is not equal to ' . $expected->getRgbaString());
114+
$this->fail($message ?? $color->getRgbaString().' is not equal to '.$expected->getRgbaString());
109115
}
110116
} elseif ($expected instanceof HslaColor) {
111117
if (!$this->isSimilarHsla($expected, $color, 0)) {
112-
$this->fail( $message ?? $color->getHslaString() . ' is not equal to ' . $expected->getHslaString());
118+
$this->fail($message ?? $color->getHslaString().' is not equal to '.$expected->getHslaString());
113119
}
114120
}
115121
}
@@ -118,7 +124,8 @@ protected function assertSameColor(ColorInterface $expected, ColorInterface $col
118124
/**
119125
* @param ColorInterface $a
120126
* @param ColorInterface $a
121-
* @param float $maxDist
127+
* @param float $maxDist
128+
*
122129
* @return bool
123130
*/
124131
protected function isSimilarHsla(ColorInterface $a, ColorInterface $b, float $maxDist = 5): bool
@@ -129,29 +136,29 @@ protected function isSimilarHsla(ColorInterface $a, ColorInterface $b, float $ma
129136
$deltaH1 = abs($b->getHue() - $a->getHue());
130137
$deltH12 = 360 - $a->getHue() + $b->getHue();
131138

132-
return (
139+
return
133140
min($deltaH1, $deltH12) < $maxDist
134141
&& abs($b->getSaturation() - $a->getSaturation()) < $maxDist
135142
&& abs($b->getLightness() - $a->getLightness()) < $maxDist
136-
&& abs($b->getAlpha() - $a->getAlpha()) < ($maxDist / 100)
137-
);
143+
&& abs($b->getAlpha() - $a->getAlpha()) < ($maxDist / 100);
138144
}
139145

140146
/**
141147
* @param ColorInterface $a
142148
* @param ColorInterface $a
143-
* @param float $maxDist
149+
* @param float $maxDist
150+
*
144151
* @return bool
145152
*/
146153
public function isSimilarRgba(ColorInterface $a, ColorInterface $b, float $maxDist = 5): bool
147154
{
148155
$a = $a->asRgba();
149156
$b = $b->asRgba();
150-
return (
157+
158+
return
151159
abs($b->getRed() - $a->getRed()) < $maxDist
152160
&& abs($b->getGreen() - $a->getGreen()) < $maxDist
153161
&& abs($b->getBlue() - $a->getBlue()) < $maxDist
154-
&& abs($b->getAlpha() - $a->getAlpha()) < $maxDist
155-
);
162+
&& abs($b->getAlpha() - $a->getAlpha()) < $maxDist;
156163
}
157164
}

0 commit comments

Comments
 (0)