@ 2022-06-12T04:12:16Z <?php
function sanitize_hex_color( $color, $maybe_alpha = false ) {
if ( '' === $color ) {
return '';
}
$allowed_lengths = $maybe_alpha ? array( 4, 5, 7, 9 ) : array( 4, 7 );
$correct_length = in_array( strlen( $color ), $allowed_lengths, true );
if ( $correct_length && preg_replace( '|^#([^A-Fa-f0-9]+)$|', '', $color ) === $color ) {
return $color;
}
}
/**
* Test Suite
*/
$happy_bc_tests = array(
'$maybe_alpha = false, 3 digit' => array(
'color' => '#123',
'expected' => '#123',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 letter' => array(
'color' => '#abc',
'expected' => '#abc',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 mixed' => array(
'color' => '#0ab',
'expected' => '#0ab',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 digit' => array(
'color' => '#123456',
'expected' => '#123456',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 letter' => array(
'color' => '#abcdef',
'expected' => '#abcdef',
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 mixed' => array(
'color' => '#abc123',
'expected' => '#abc123',
'maybe_alpha' => false,
),
);
$unhappy_bc_tests = array(
'empty string' => array(
'color' => '',
'expected' => '',
'maybe_alpha' => false,
),
'no hash' => array(
'color' => '123',
'expected' => null,
'maybe_alpha' => false,
),
'not a-f' => array(
'color' => '#hjg',
'expected' => null,
'maybe_alpha' => false,
),
'not A-F' => array(
'color' => '#HJG',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 digit with 1 alpha' => array(
'color' => '#123f',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 letter with 1 alpha' => array(
'color' => '#abcf',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 3 mixed with 1 alpha' => array(
'color' => '#0abf',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 digit with 2 alpha' => array(
'color' => '#123456ff',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 letter with 2 alpha' => array(
'color' => '#abcdefff',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = false, 6 mixed with 2 alpha' => array(
'color' => '#abc123ff',
'expected' => null,
'maybe_alpha' => false,
),
);
$happy_alpha_tests = array(
// Happy.
'$maybe_alpha = true, 3 digit' => array(
'color' => '#123',
'expected' => '#123',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter' => array(
'color' => '#abc',
'expected' => '#abc',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed' => array(
'color' => '#0ab',
'expected' => '#0ab',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit' => array(
'color' => '#123456',
'expected' => '#123456',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter' => array(
'color' => '#abcdef',
'expected' => '#abcdef',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed' => array(
'color' => '#abc123',
'expected' => '#abc123',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 digit with 1 alpha' => array(
'color' => '#123f',
'expected' => '#123f',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter with 1 alpha' => array(
'color' => '#abcf',
'expected' => '#abcf',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed with 1 alpha' => array(
'color' => '#0abf',
'expected' => '#0abf',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit with 2 alpha' => array(
'color' => '#123456ff',
'expected' => '#123456ff',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter with 2 alpha' => array(
'color' => '#abcdefff',
'expected' => '#abcdefff',
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed with 2 alpha' => array(
'color' => '#abc123ff',
'expected' => '#abc123ff',
'maybe_alpha' => true,
),
);
$unhappy_alpha_tests = array(
'empty string' => array(
'color' => '',
'expected' => '',
'maybe_alpha' => false,
),
'no hash' => array(
'color' => '123',
'expected' => null,
'maybe_alpha' => false,
),
'not a-f' => array(
'color' => '#hjg',
'expected' => null,
'maybe_alpha' => false,
),
'not A-F' => array(
'color' => '#HJG',
'expected' => null,
'maybe_alpha' => false,
),
'$maybe_alpha = true, 3 digit with 2 alpha' => array(
'color' => '#123ff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 letter with 2 alpha' => array(
'color' => '#abcff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 3 mixed with 2 alpha' => array(
'color' => '#0abff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 digit with 1 alpha' => array(
'color' => '#123456f',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 letter with alpha' => array(
'color' => '#abcff',
'expected' => null,
'maybe_alpha' => true,
),
'$maybe_alpha = true, 6 mixed with alpha' => array(
'color' => '#0abff',
'expected' => null,
'maybe_alpha' => true,
),
);
function run_tests( $tests = array(), $testdox = false ) {
$markers = array(
$testdox ? '❌' : 'F',
$testdox ? '✅' : '.',
);
foreach ( $tests as $test => $data ) {
list( $color, $expected, $maybe_alpha ) = array_values( $data );
$actual = sanitize_hex_color( $color, $maybe_alpha );
echo $markers[ (int) ( $actual === $expected ) ] . ( $testdox ? " $test" : '' ) . ( $testdox ? PHP_EOL : '' );
}
}
echo "Happy BC tests\n";
run_tests( $happy_bc_tests, true );
echo "\nUnhappy BC tests\n";
run_tests( $unhappy_bc_tests, true );
echo "\nHappy alpha tests\n";
run_tests( $happy_alpha_tests, true );
echo "\nUnhappy alpha tests\n";
run_tests( $unhappy_alpha_tests, true );
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.4.12 0.017 0.003 20.53 8.4.11 0.010 0.010 22.49 8.4.10 0.010 0.010 22.63 8.4.9 0.008 0.013 18.74 8.4.8 0.012 0.011 17.97 8.4.7 0.010 0.009 18.22 8.4.6 0.009 0.007 18.88 8.4.5 0.010 0.011 18.73 8.4.4 0.016 0.003 19.80 8.4.3 0.010 0.010 22.09 8.4.2 0.011 0.007 20.54 8.4.1 0.000 0.009 23.89 8.3.25 0.010 0.008 18.90 8.3.24 0.013 0.007 16.64 8.3.23 0.010 0.011 16.81 8.3.22 0.007 0.009 17.22 8.3.21 0.010 0.009 16.69 8.3.20 0.004 0.005 16.61 8.3.19 0.011 0.008 17.20 8.3.18 0.006 0.003 16.86 8.3.17 0.009 0.009 18.88 8.3.16 0.007 0.011 20.75 8.3.15 0.006 0.003 16.88 8.3.14 0.004 0.015 17.18 8.3.13 0.010 0.010 18.64 8.3.12 0.009 0.009 19.21 8.3.11 0.009 0.000 20.94 8.3.10 0.003 0.006 24.06 8.3.9 0.016 0.003 26.77 8.3.8 0.003 0.006 18.43 8.3.7 0.010 0.010 16.58 8.3.6 0.012 0.006 18.43 8.3.5 0.004 0.014 20.37 8.3.4 0.003 0.012 18.96 8.3.3 0.004 0.011 19.21 8.3.2 0.008 0.000 24.18 8.3.1 0.000 0.008 24.66 8.3.0 0.005 0.005 26.16 8.2.29 0.011 0.008 20.62 8.2.28 0.008 0.011 18.48 8.2.27 0.015 0.003 19.34 8.2.26 0.010 0.000 16.63 8.2.25 0.003 0.006 19.14 8.2.24 0.003 0.006 18.52 8.2.23 0.003 0.006 22.58 8.2.22 0.005 0.005 37.54 8.2.21 0.007 0.011 26.77 8.2.20 0.010 0.000 16.75 8.2.19 0.014 0.004 16.58 8.2.18 0.015 0.000 25.92 8.2.17 0.017 0.003 19.17 8.2.16 0.003 0.012 22.96 8.2.15 0.008 0.000 25.66 8.2.14 0.008 0.000 24.66 8.2.13 0.007 0.011 26.16 8.2.12 0.006 0.003 26.16 8.2.11 0.012 0.012 22.25 8.2.10 0.004 0.008 18.15 8.2.9 0.009 0.000 19.40 8.2.8 0.004 0.007 18.04 8.2.7 0.004 0.004 17.88 8.2.6 0.004 0.004 17.93 8.2.5 0.004 0.004 18.07 8.2.4 0.000 0.008 18.32 8.2.3 0.005 0.003 18.13 8.2.2 0.004 0.004 20.40 8.2.1 0.000 0.007 18.13 8.2.0 0.004 0.004 19.30 8.1.33 0.011 0.007 22.13 8.1.32 0.009 0.010 20.39 8.1.31 0.010 0.007 18.29 8.1.30 0.003 0.013 18.04 8.1.29 0.009 0.000 30.84 8.1.28 0.006 0.016 25.92 8.1.27 0.004 0.004 23.99 8.1.26 0.004 0.004 26.35 8.1.25 0.005 0.003 28.09 8.1.24 0.005 0.005 20.89 8.1.23 0.008 0.004 17.77 8.1.22 0.009 0.000 17.79 8.1.21 0.004 0.004 18.77 8.1.20 0.006 0.006 17.48 8.1.19 0.000 0.008 17.47 8.1.18 0.004 0.004 18.10 8.1.17 0.004 0.004 18.87 8.1.16 0.004 0.004 18.93 8.1.15 0.004 0.004 18.88 8.1.14 0.004 0.004 17.74 8.1.13 0.003 0.003 19.01 8.1.12 0.000 0.007 17.55 8.1.11 0.003 0.006 17.68 8.1.10 0.004 0.004 17.63 8.1.9 0.004 0.004 17.59 8.1.8 0.000 0.008 17.69 8.1.7 0.018 0.000 17.61 8.1.6 0.020 0.003 17.68 8.1.5 0.012 0.008 17.70 8.1.4 0.017 0.000 17.76 8.1.3 0.012 0.008 17.68 8.1.2 0.012 0.008 17.81 8.1.1 0.010 0.010 17.66 8.1.0 0.010 0.010 17.72 8.0.30 0.003 0.005 18.77 8.0.29 0.003 0.006 16.88 8.0.28 0.000 0.007 18.49 8.0.27 0.000 0.007 18.13 8.0.26 0.003 0.003 18.50 8.0.25 0.000 0.007 17.04 8.0.24 0.003 0.003 17.16 8.0.23 0.000 0.009 17.06 8.0.22 0.003 0.003 17.05 8.0.21 0.003 0.003 17.00 8.0.20 0.005 0.010 17.10 8.0.19 0.018 0.000 17.07 8.0.18 0.012 0.005 17.09 8.0.17 0.015 0.002 16.95 8.0.16 0.014 0.005 17.07 8.0.15 0.017 0.000 16.89 8.0.14 0.013 0.003 17.05 8.0.13 0.015 0.003 17.10 8.0.12 0.007 0.011 17.05 8.0.11 0.014 0.003 17.06 8.0.10 0.008 0.008 16.99 8.0.9 0.014 0.003 17.04 8.0.8 0.012 0.004 17.11 8.0.7 0.017 0.000 16.95 8.0.6 0.011 0.005 17.05 8.0.5 0.012 0.004 16.90 8.0.3 0.012 0.006 16.88 8.0.2 0.007 0.007 17.03 8.0.1 0.016 0.000 17.17 7.4.33 0.000 0.006 15.55 7.4.32 0.004 0.004 16.76 7.4.30 0.014 0.005 16.76 7.4.29 0.005 0.011 16.73 7.4.28 0.018 0.000 16.86 7.4.27 0.010 0.010 16.68 7.4.26 0.014 0.004 16.80 7.4.25 0.007 0.011 16.63 7.4.24 0.012 0.003 16.88 7.4.23 0.009 0.009 16.81 7.4.22 0.013 0.004 16.72 7.4.21 0.013 0.003 16.82 7.4.20 0.018 0.000 16.85 7.4.19 0.010 0.006 16.91 7.4.18 0.011 0.006 16.91 7.4.16 0.015 0.004 16.80 7.4.15 0.019 0.000 16.69 7.4.14 0.008 0.008 16.61 7.4.13 0.010 0.005 16.79 7.4.12 0.014 0.003 16.71 7.4.11 0.015 0.003 16.76 7.4.10 0.004 0.011 16.86 7.4.9 0.011 0.005 16.61 7.4.8 0.012 0.005 16.67 7.4.7 0.007 0.003 16.66 7.4.6 0.013 0.000 16.73 7.4.5 0.012 0.002 16.57 7.4.4 0.012 0.002 16.57 7.4.3 0.008 0.009 16.81 7.4.2 0.011 0.000 16.68 7.4.1 0.012 0.000 16.67 7.4.0 0.005 0.005 16.54
preferences:dark mode live preview ace vim emacs key bindings
29.64 ms | 403 KiB | 5 Q