function ColorTest::providerTestHexToRgb

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Component/Utility/ColorTest.php \Drupal\Tests\Component\Utility\ColorTest::providerTestHexToRgb()
  2. 8.9.x core/tests/Drupal/Tests/Component/Utility/ColorTest.php \Drupal\Tests\Component\Utility\ColorTest::providerTestHexToRgb()
  3. 10 core/tests/Drupal/Tests/Component/Utility/ColorTest.php \Drupal\Tests\Component\Utility\ColorTest::providerTestHexToRgb()

Data provider for testHexToRgb().

Return value

array An array of arrays containing:

  • The hex color value.
  • The rgb color array value.
  • (optional) Boolean indicating invalid status. Defaults to FALSE.

See also

testHexToRgb()

File

core/tests/Drupal/Tests/Component/Utility/ColorTest.php, line 101

Class

ColorTest
Tests Color utility class conversions.

Namespace

Drupal\Tests\Component\Utility

Code

public static function providerTestHexToRgb() {
    $invalid = [];
    // Any invalid arguments should throw an exception.
    foreach ([
        '',
        '-1',
        '1',
        '12',
        '12345',
        '1234567',
        '123456789',
        '123456789a',
        'foo',
    ] as $value) {
        $invalid[] = [
            $value,
            '',
            TRUE,
        ];
    }
    // Duplicate all invalid value tests with additional '#' prefix.
    // The '#' prefix inherently turns the data type into a string.
    foreach ($invalid as $value) {
        $invalid[] = [
            '#' . $value[0],
            '',
            TRUE,
        ];
    }
    // Add invalid data types (hex value must be a string).
    foreach ([
        1,
        12,
        1234,
        12345,
        123456,
        1234567,
        12345678,
        123456789,
        123456789,
        -1,
        PHP_INT_MAX,
        PHP_INT_MAX + 1,
        -PHP_INT_MAX,
        0x0,
        0x10,
    ] as $value) {
        $invalid[] = [
            $value,
            '',
            TRUE,
        ];
    }
    // And some valid values.
    $valid = [
        // Shorthands without alpha.
[
            'value' => '#000',
            'expected' => [
                'red' => 0,
                'green' => 0,
                'blue' => 0,
            ],
        ],
        [
            'value' => '#fff',
            'expected' => [
                'red' => 255,
                'green' => 255,
                'blue' => 255,
            ],
        ],
        [
            'value' => '#abc',
            'expected' => [
                'red' => 170,
                'green' => 187,
                'blue' => 204,
            ],
        ],
        [
            'value' => 'cba',
            'expected' => [
                'red' => 204,
                'green' => 187,
                'blue' => 170,
            ],
        ],
        // Full without alpha.
[
            'value' => '#000000',
            'expected' => [
                'red' => 0,
                'green' => 0,
                'blue' => 0,
            ],
        ],
        [
            'value' => '#ffffff',
            'expected' => [
                'red' => 255,
                'green' => 255,
                'blue' => 255,
            ],
        ],
        [
            'value' => '#010203',
            'expected' => [
                'red' => 1,
                'green' => 2,
                'blue' => 3,
            ],
        ],
    ];
    return array_merge($invalid, $valid);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.