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 if (method_exists($this, 'expectException')) {
30 $this->expectException('InvalidArgumentException');
33 $this->setExpectedException('InvalidArgumentException');
36 $this->assertSame($expected, Color::hexToRgb($value));
40 * Data provider for testHexToRgb().
45 * An array of arrays containing:
46 * - The hex color value.
47 * - The rgb color array value.
48 * - (optional) Boolean indicating invalid status. Defaults to FALSE.
50 public function providerTestHexToRgb() {
52 // Any invalid arguments should throw an exception.
53 foreach (['', '-1', '1', '12', '12345', '1234567', '123456789', '123456789a', 'foo'] as $value) {
54 $invalid[] = [$value, '', TRUE];
56 // Duplicate all invalid value tests with additional '#' prefix.
57 // The '#' prefix inherently turns the data type into a string.
58 foreach ($invalid as $value) {
59 $invalid[] = ['#' . $value[0], '', TRUE];
61 // Add invalid data types (hex value must be a string).
63 1, 12, 1234, 12345, 123456, 1234567, 12345678, 123456789, 123456789,
64 -1, PHP_INT_MAX, PHP_INT_MAX + 1, -PHP_INT_MAX, 0x0, 0x010,
66 $invalid[] = [$value, '', TRUE];
68 // And some valid values.
70 // Shorthands without alpha.
71 ['hex' => '#000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
72 ['hex' => '#fff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
73 ['hex' => '#abc', 'rgb' => ['red' => 170, 'green' => 187, 'blue' => 204]],
74 ['hex' => 'cba', 'rgb' => ['red' => 204, 'green' => 187, 'blue' => 170]],
75 // Full without alpha.
76 ['hex' => '#000000', 'rgb' => ['red' => 0, 'green' => 0, 'blue' => 0]],
77 ['hex' => '#ffffff', 'rgb' => ['red' => 255, 'green' => 255, 'blue' => 255]],
78 ['hex' => '#010203', 'rgb' => ['red' => 1, 'green' => 2, 'blue' => 3]],
80 return array_merge($invalid, $valid);
84 * Tests Color::rgbToHex().
86 * @param string $value
87 * The rgb color value.
88 * @param string $expected
89 * The expected hex color value.
91 * @dataProvider providerTestRbgToHex
93 public function testRgbToHex($value, $expected) {
94 $this->assertSame($expected, Color::rgbToHex($value));
98 * Data provider for testRgbToHex().
100 * @see testRgbToHex()
103 * An array of arrays containing:
104 * - The rgb color array value.
105 * - The hex color value.
107 public function providerTestRbgToHex() {
108 // Input using named RGB array (e.g., as returned by Color::hexToRgb()).
110 [['red' => 0, 'green' => 0, 'blue' => 0], '#000000'],
111 [['red' => 255, 'green' => 255, 'blue' => 255], '#ffffff'],
112 [['red' => 119, 'green' => 119, 'blue' => 119], '#777777'],
113 [['red' => 1, 'green' => 2, 'blue' => 3], '#010203'],
115 // Input using indexed RGB array (e.g.: array(10, 10, 10)).
116 foreach ($tests as $test) {
117 $tests[] = [array_values($test[0]), $test[1]];
119 // Input using CSS RGB string notation (e.g.: 10, 10, 10).
120 foreach ($tests as $test) {
121 $tests[] = [implode(', ', $test[0]), $test[1]];
127 * Data provider for testNormalizeHexLength().
129 * @see testNormalizeHexLength()
132 * An array of arrays containing:
133 * - The hex color value.
134 * - The 6 character length hex color value.
136 public function providerTestNormalizeHexLength() {
142 ['#000000', '#000000'],
143 ['ffffff', '#ffffff'],
144 ['#010203', '#010203'],
151 * Tests Color::normalizeHexLength().
153 * @param string $value
154 * The input hex color value.
155 * @param string $expected
156 * The expected normalized hex color value.
158 * @dataProvider providerTestNormalizeHexLength
160 public function testNormalizeHexLength($value, $expected) {
161 $this->assertSame($expected, Color::normalizeHexLength($value));