3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Color;
6 use PHPUnit\Framework\TestCase;
9 * Tests Color utility class conversions.
13 class ColorTest extends TestCase {
16 * Tests Color::hexToRgb().
18 * @param string $value
19 * The hex color value.
20 * @param string $expected
21 * The expected rgb color value.
22 * @param bool $invalid
23 * Whether this value is invalid and exception should be expected.
25 * @dataProvider providerTestHexToRgb
27 public function testHexToRgb($value, $expected, $invalid = FALSE) {
29 $this->setExpectedException('InvalidArgumentException');
31 $this->assertSame($expected, Color::hexToRgb($value));
35 * Data provider for testHexToRgb().
40 * An array of arrays containing:
41 * - The hex color value.
42 * - The rgb color array value.
43 * - (optional) Boolean indicating invalid status. Defaults to FALSE.
45 public function providerTestHexToRgb() {
47 // Any invalid arguments should throw an exception.
48 foreach (['', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo'] as $value) {
49 $invalid[] = [$value, '', TRUE];
51 // Duplicate all invalid value tests with additional '#' prefix.
52 // The '#' prefix inherently turns the data type into a string.
53 foreach ($invalid as $value) {
54 $invalid[] = ['#' . $value[0], '', TRUE];
56 // Add invalid data types (hex value must be a string).
58 1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
59 -1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010
61 $invalid[] = [$value, '', TRUE];
63 // And some valid values.
65 // Shorthands without alpha.
66 ['hex' => '#000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
67 ['hex' => '#fff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
68 ['hex' => '#abc', 'rgb' => ['red' => 170, 'green' => 187, 'blue' => 204]],
69 ['hex' => 'cba', 'rgb' => ['red' => 204, 'green' => 187, 'blue' => 170]],
70 // Full without alpha.
71 ['hex' => '#000000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
72 ['hex' => '#ffffff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
73 ['hex' => '#010203', 'rgb' => ['red' => 1, 'green' => 2, 'blue' => 3]],
75 return array_merge($invalid, $valid);
79 * Tests Color::rgbToHex().
81 * @param string $value
82 * The rgb color value.
83 * @param string $expected
84 * The expected hex color value.
86 * @dataProvider providerTestRbgToHex
88 public function testRgbToHex($value, $expected) {
89 $this->assertSame($expected, Color::rgbToHex($value));
93 * Data provider for testRgbToHex().
98 * An array of arrays containing:
99 * - The rgb color array value.
100 * - The hex color value.
102 public function providerTestRbgToHex() {
103 // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
105 [['red' => 0, 'green' => 0, 'blue' => 0], '#000000'],
106 [['red' => 255, 'green' => 255, 'blue' => 255], '#ffffff'],
107 [['red' => 119, 'green' => 119, 'blue' => 119], '#777777'],
108 [['red' => 1, 'green' => 2, 'blue' => 3], '#010203'],
110 // Input using indexed RGB array (e.g.: array(10, 10, 10)).
111 foreach ($tests as $test) {
112 $tests[] = [array_values($test[0]), $test[1]];
114 // Input using CSS RGB string notation (e.g.: 10, 10, 10).
115 foreach ($tests as $test) {
116 $tests[] = [implode(', ', $test[0]), $test[1]];