@@ -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