You are here

protected function ToolkitImagemagickTest::assertColorsAreClose in ImageMagick 8.3

Asserts equality of two colors 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[] $expected: The expected RGBA array.

int[] $actual: The actual RGBA array.

int $tolerance: The acceptable difference between the colors.

string $file: The image file being tested.

string $op: The image operation being tested.

1 call to ToolkitImagemagickTest::assertColorsAreClose()
ToolkitImagemagickTest::testManipulations in tests/src/Functional/ToolkitImagemagickTest.php
Test image toolkit operations.

File

tests/src/Functional/ToolkitImagemagickTest.php, line 672

Class

ToolkitImagemagickTest
Tests that core image manipulations work properly through Imagemagick.

Namespace

Drupal\Tests\imagemagick\Functional

Code

protected function assertColorsAreClose(array $expected, array $actual, int $tolerance, string $file, string $op) : void {

  // 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 . ", File: " . $file . ", Operation: " . $op);
}