You are here

protected function ToolkitImagemagickTest::colorsAreClose in ImageMagick 8.2

Same name and namespace in other branches
  1. 8 tests/src/Functional/ToolkitImagemagickTest.php \Drupal\Tests\imagemagick\Functional\ToolkitImagemagickTest::colorsAreClose()

Function to compare 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[] $actual: The actual RGBA array.

int[] $expected: The expected RGBA array.

int $tolerance: The acceptable difference between the colors.

string $file: The image file being tested.

string $op: The image operation being tested.

Return value

bool TRUE if the colors differences are within tolerance, FALSE otherwise.

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

File

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

Class

ToolkitImagemagickTest
Tests that core image manipulations work properly through Imagemagick.

Namespace

Drupal\Tests\imagemagick\Functional

Code

protected function colorsAreClose(array $actual, array $expected, $tolerance, $file, $op) {

  // Fully transparent colors are equal, regardless of RGB.
  if ($actual[3] == 127 && $expected[3] == 127) {
    return TRUE;
  }
  $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);
  return TRUE;
}