function ColorTest::testRgbToHex

Tests Color::rgbToHex().

File

drupal/core/modules/system/lib/Drupal/system/Tests/Common/ColorTest.php, line 78
Definition of Drupal\system\Tests\Common\ColorTest.

Class

ColorTest
Tests color conversion functions.

Namespace

Drupal\system\Tests\Common

Code

function testRgbToHex() {
  $tests = array(
    '#000000' => array(
      'red' => 0,
      'green' => 0,
      'blue' => 0,
    ),
    '#ffffff' => array(
      'red' => 255,
      'green' => 255,
      'blue' => 255,
    ),
    '#777777' => array(
      'red' => 119,
      'green' => 119,
      'blue' => 119,
    ),
    '#010203' => array(
      'red' => 1,
      'green' => 2,
      'blue' => 3,
    ),
  );

  // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
  foreach ($tests as $expected => $rgb) {
    $this
      ->assertIdentical(Color::rgbToHex($rgb), $expected);
  }

  // Input using indexed RGB array (e.g.: array(10, 10, 10)).
  foreach ($tests as $expected => $rgb) {
    $rgb = array_values($rgb);
    $this
      ->assertIdentical(Color::rgbToHex($rgb), $expected);
  }

  // Input using CSS RGB string notation (e.g.: 10, 10, 10).
  foreach ($tests as $expected => $rgb) {
    $rgb = implode(', ', $rgb);
    $this
      ->assertIdentical(Color::rgbToHex($rgb), $expected);
  }
}