Skip to content

Commit 61dd282

Browse files
committed
TASK: Move isSimilar and isEqual from color interface to the test fixtures
Maybe they will be reintroduced later but for now we they are only needed for testing.
1 parent d0d749f commit 61dd282

File tree

5 files changed

+54
-56
lines changed

5 files changed

+54
-56
lines changed

Build/Travis/.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Classes/Domain/ValueObject/ColorInterface.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ public function asRgba(): RgbaColor;
1616
*/
1717
public function asHsla(): HslaColor;
1818

19-
/**
20-
* @param ColorInterface $color
21-
*
22-
* @return bool
23-
*/
24-
public function equals(self $color): bool;
25-
26-
/**
27-
* @param ColorInterface $color
28-
* @param float $maxDist
29-
* @return bool
30-
*/
31-
public function isSimilarTo(ColorInterface $color, float $maxDist = 2): bool;
32-
3319
/**
3420
* @return string
3521
*/

Classes/Domain/ValueObject/HslaColor.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,7 @@ public function asHsla(): HslaColor
137137
return $this;
138138
}
139139

140-
/**
141-
* @param ColorInterface $color
142-
* @param float $maxDist
143-
* @return bool
144-
*/
145-
public function isSimilarTo(ColorInterface $color, float $maxDist = 5): bool
146-
{
147-
$color = $color->asHsla();
148140

149-
$deltaH1 = abs($this->getHue() - $color->getHue());
150-
$deltH12 = 360 - $color->getHue() + $this->getHue();
151-
152-
return (
153-
min($deltaH1, $deltH12) < $maxDist
154-
&& abs($this->getSaturation() - $color->getSaturation()) < $maxDist
155-
&& abs($this->getLightness() - $color->getLightness()) < $maxDist
156-
&& abs($this->getAlpha() - $color->getAlpha()) < ($maxDist / 100)
157-
);
158-
}
159141

160142
/**
161143
* @param float $delta 0..100

Classes/Domain/ValueObject/RgbaColor.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,6 @@ public function asRgba(): RgbaColor
9797
return $this;
9898
}
9999

100-
/**
101-
* @param ColorInterface $color
102-
* @param float $maxDist
103-
* @return bool
104-
*/
105-
public function isSimilarTo(ColorInterface $color, float $maxDist = 5): bool
106-
{
107-
$color = $color->asRgba();
108-
return (
109-
abs($this->getRed() - $color->getRed()) < $maxDist
110-
&& abs($this->getGreen() - $color->getGreen()) < $maxDist
111-
&& abs($this->getBlue() - $color->getBlue()) < $maxDist
112-
&& abs($this->getAlpha() - $color->getAlpha()) < $maxDist
113-
);
114-
}
115-
116100
/**
117101
* @return HslaColor
118102
* @see http://en.wikipedia.org/wiki/HSL_color_space.

Tests/Unit/AbstractColorTest.php

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ public function hlsSpectrumDataProvider():array
8383
*/
8484
protected function assertSimilarColor(ColorInterface $expected, ColorInterface $color, string $message = null) {
8585
$this->addToAssertionCount(1);
86-
if ($color->isSimilarTo($expected) == false) {
87-
if ($this->expectecColor instanceof RgbaColor) {
86+
if ($expected instanceof RgbaColor) {
87+
if (!$this->isSimilarRgba($expected, $color)) {
8888
$this->fail( $message ?? $color->getRgbaString() . ' is not similar to ' . $expected->getRgbaString());
89-
} elseif ($this->expectecColor instanceof HslaColor) {
89+
}
90+
} elseif ($expected instanceof HslaColor) {
91+
if (!$this->isSimilarHsla($expected, $color)) {
9092
$this->fail( $message ?? $color->getHslaString() . ' is not similar to ' . $expected->getHslaString());
9193
}
9294
}
@@ -101,11 +103,55 @@ protected function assertSameColor(ColorInterface $expected, ColorInterface $col
101103
if (get_class($expected) != get_class($color)) {
102104
$this->fail( $message ?? get_class($expected) . ' is not the same as ' . get_class($color));
103105
} elseif ($color->equals($expected) == false) {
104-
if ($this->expectecColor instanceof RgbaColor) {
105-
$this->fail( $message ?? $color->getRgbaString() . ' is not similar to ' . $expected->getRgbaString());
106-
} elseif ($this->expectecColor instanceof HslaColor) {
107-
$this->fail( $message ?? $color->getHslaString() . ' is not similar to ' . $expected->getHslaString());
106+
if ($expected instanceof RgbaColor) {
107+
if (!$this->isSimilarRgba($expected, $color, 0)) {
108+
$this->fail( $message ?? $color->getRgbaString() . ' is not equal to ' . $expected->getRgbaString());
109+
}
110+
} elseif ($expected instanceof HslaColor) {
111+
if (!$this->isSimilarHsla($expected, $color, 0)) {
112+
$this->fail( $message ?? $color->getHslaString() . ' is not equal to ' . $expected->getHslaString());
113+
}
108114
}
109115
}
110116
}
117+
118+
/**
119+
* @param ColorInterface $a
120+
* @param ColorInterface $a
121+
* @param float $maxDist
122+
* @return bool
123+
*/
124+
protected function isSimilarHsla(ColorInterface $a, ColorInterface $b, float $maxDist = 5): bool
125+
{
126+
$a = $a->asHsla();
127+
$b = $b->asHsla();
128+
129+
$deltaH1 = abs($b->getHue() - $a->getHue());
130+
$deltH12 = 360 - $a->getHue() + $b->getHue();
131+
132+
return (
133+
min($deltaH1, $deltH12) < $maxDist
134+
&& abs($b->getSaturation() - $a->getSaturation()) < $maxDist
135+
&& abs($b->getLightness() - $a->getLightness()) < $maxDist
136+
&& abs($b->getAlpha() - $a->getAlpha()) < ($maxDist / 100)
137+
);
138+
}
139+
140+
/**
141+
* @param ColorInterface $a
142+
* @param ColorInterface $a
143+
* @param float $maxDist
144+
* @return bool
145+
*/
146+
public function isSimilarRgba(ColorInterface $a, ColorInterface $b, float $maxDist = 5): bool
147+
{
148+
$a = $a->asRgba();
149+
$b = $b->asRgba();
150+
return (
151+
abs($b->getRed() - $a->getRed()) < $maxDist
152+
&& abs($b->getGreen() - $a->getGreen()) < $maxDist
153+
&& abs($b->getBlue() - $a->getBlue()) < $maxDist
154+
&& abs($b->getAlpha() - $a->getAlpha()) < $maxDist
155+
);
156+
}
111157
}

0 commit comments

Comments
 (0)