You are here

public function ImageEffectsTestBase::assertColorsAreClose in Image Effects 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/ImageEffectsTestBase.php \Drupal\Tests\image_effects\Functional\ImageEffectsTestBase::assertColorsAreClose()
  2. 8.2 tests/src/Functional/ImageEffectsTestBase.php \Drupal\Tests\image_effects\Functional\ImageEffectsTestBase::assertColorsAreClose()

Assert two colors are close by RGBA within a tolerance.

Very basic, just compares the sum of the squared differences for each of the R, G, B, A components of two colors against a 'tolerance' value.

Parameters

int[] $actual: The actual RGBA array.

int[] $expected: The expected RGBA array.

int $tolerance: The acceptable difference between the colors.

5 calls to ImageEffectsTestBase::assertColorsAreClose()
AutoOrientTest::testAutoOrientAllTags in tests/src/Functional/Effect/AutoOrientTest.php
Auto Orientation effect test, all EXIF orientation tags.
ContrastTest::testContrastEffect in tests/src/Functional/Effect/ContrastTest.php
Contrast effect test.
ImageEffectsTestBase::assertColorsAreEqual in tests/src/Functional/ImageEffectsTestBase.php
Assert two colors are equal by RGBA.
SetTransparentColorTest::testSetTransparentColorEffect in tests/src/Functional/Effect/SetTransparentColorTest.php
Set transparent color effect test.
WatermarkTest::testWatermarkEffect in tests/src/Functional/Effect/WatermarkTest.php
Watermark effect test.

File

tests/src/Functional/ImageEffectsTestBase.php, line 285

Class

ImageEffectsTestBase
Base test class for image_effects tests.

Namespace

Drupal\Tests\image_effects\Functional

Code

public function assertColorsAreClose(array $actual, array $expected, $tolerance) {

  // Fully transparent colors are equal, regardless of RGB.
  if ($actual[3] == 127 && $expected[3] == 127) {
    return;
  }
  $distance = pow($actual[0] - $expected[0], 2) + pow($actual[1] - $expected[1], 2) + pow($actual[2] - $expected[2], 2) + pow($actual[3] - $expected[3], 2);
  $this
    ->assertLessThanOrEqual($tolerance, $distance, "Actual: {" . implode(',', $actual) . "}, Expected: {" . implode(',', $expected) . "}, Distance: " . $distance . ", Tolerance: " . $tolerance);
}