Check text contrast with PHP
Last updated November 30, 2026
Learning outcome
Build a small command-line checker for a theme’s text and background colors. You’ll verify a known-good pair, change just the surface color, and catch a failure you can reproduce. At the end, you’ll test the same foreground against two dark surfaces and decide whether it works for both.
Prerequisites
PHP 8 on the command line, a text editor, and a directory where you can create a file. You can follow along with the colors below without a Drupal installation.
Tested environment and versions
The complete exercise was run with PHP 8.5.10 CLI and a POSIX-compatible shell. It uses opaque six-digit sRGB colors.
1. Set up a practice folder
Imagine reviewing a palette change that swaps the background of a small panel. You want to keep the label color and find out whether both surface choices are readable. Keep one foreground fixed for each appearance and vary the background. That way you can tell which change affected the ratio.
In a terminal, check that PHP is available and move into a new practice folder.
php -v
mkdir palette-contrast-lab
cd palette-contrast-lab
Check that the version line identifies PHP 8 CLI. If the folder already exists, use a new folder name. Keep this terminal in the practice folder for the rest of the lesson.
2. Build the checker and verify a known pair
Create palette-check.php in the practice folder and paste the complete script below. The first argument selects light or dark appearance. The second selects which of two background swatches the surface uses. The third, optional argument lets you try a different text color while keeping the selected background.
The calculator uses relative luminance. It converts each sRGB channel to a linear value, weights the channels, and puts the lighter color’s luminance in the numerator. That same calculation works for dark text on a light tint and light text on a dark tint. For this exercise, use the WCAG minimum of 4.5:1 for normal text. W3C contrast guidance
<?php
/**
* @file
* Checks text contrast for a pair of opaque sRGB colors.
*/
declare(strict_types=1);
/**
* Calculates the relative luminance of a color.
*
* @param string $hex
* A validated six-digit sRGB color with a leading hash.
*
* @return float
* The relative luminance, from 0 for black to 1 for white.
*/
function palette_check_relative_luminance(string $hex): float {
$channels = str_split(substr($hex, 1), 2);
$linearize = static function (string $channel): float {
$value = hexdec($channel) / 255;
return $value <= 0.04045
? $value / 12.92
: (($value + 0.055) / 1.055) ** 2.4;
};
[$red, $green, $blue] = array_map($linearize, $channels);
return 0.2126 * $red + 0.7152 * $green + 0.0722 * $blue;
}
$palettes = [
'light' => [
'ink' => '#0b1739',
'surface' => [
'harmony_1' => '#ecedff',
'harmony_2' => '#dee0f9',
],
],
'dark' => [
'ink' => '#eaf1ff',
'surface' => [
'harmony_1' => '#232047',
'harmony_2' => '#1a173d',
],
],
];
$mode = $argv[1] ?? '';
$source = $argv[2] ?? '';
if ($argc > 4 || !isset($palettes[$mode]['surface'][$source])) {
fwrite(STDERR, "Usage: php palette-check.php light|dark harmony_1|harmony_2 [#RRGGBB]\n");
exit(2);
}
$ink = strtolower($argv[3] ?? $palettes[$mode]['ink']);
if (!preg_match('/^#[0-9a-f]{6}$/D', $ink)) {
fwrite(STDERR, "The text color must be a six-digit hex value such as #eaf1ff.\n");
exit(2);
}
$surface = $palettes[$mode]['surface'][$source];
$ink_luminance = palette_check_relative_luminance($ink);
$surface_luminance = palette_check_relative_luminance($surface);
$lighter = max($ink_luminance, $surface_luminance);
$darker = min($ink_luminance, $surface_luminance);
$ratio = ($lighter + 0.05) / ($darker + 0.05);
$passes = $ratio >= 4.5;
printf(
"%s %s %s on %s %.2f:1 %s\n",
$mode,
$source,
$ink,
$surface,
$ratio,
$passes ? 'PASS' : 'FAIL',
);
exit($passes ? 0 : 1);
Check the file’s syntax, then run the first pair.
php -l palette-check.php
php palette-check.php light harmony_1
You should see this result after the syntax check passes.
light harmony_1 #0b1739 on #ecedff 15.16:1 PASS
Read the two hex values in order. The first is the ink the script chose for light mode. The second is the assigned surface. If either hex differs, compare the $palettes array in your file with the example before continuing.
3. Change just the surface
Keep the light ink fixed and switch its background to harmony_2. Repeat the comparison in dark mode.
php palette-check.php light harmony_2
php palette-check.php dark harmony_1
php palette-check.php dark harmony_2
Compare your output with these checkpoints.
| Appearance | Surface | Text | Background | Expected result |
|---|---|---|---|---|
| Light | harmony_1 |
#0b1739 |
#ecedff |
15.16:1 PASS |
| Light | harmony_2 |
#0b1739 |
#dee0f9 |
13.49:1 PASS |
| Dark | harmony_1 |
#eaf1ff |
#232047 |
13.53:1 PASS |
| Dark | harmony_2 |
#eaf1ff |
#1a173d |
15.01:1 PASS |
The same assignment change moves the ratios in opposite directions. The new light tint is closer in luminance to the dark text, so contrast drops. The new dark tint moves farther from the light text, so contrast rises. Compare both appearances before deciding that a surface change improves contrast.
4. Make the check fail and fix it
Keep dark mode on harmony_2. Pass a muted slate as the text color. Quote the hex value so the shell passes it as an argument.
php palette-check.php dark harmony_2 '#44506b'
echo $?
dark harmony_2 #44506b on #1a173d 2.11:1 FAIL
1
Run echo $? immediately after the checker. Status 1 signals that the pair fell below the threshold. The foreground changed to #44506b and the background stayed #1a173d, so you can isolate the failing change.
Restore the original light ink and check the repaired pair.
php palette-check.php dark harmony_2 '#eaf1ff'
echo $?
Expect 15.01:1 PASS and status 0. The script displays two decimal places and compares the full ratio against 4.5:1.
Troubleshoot a different result
| What you see | What to check |
|---|---|
The shell cannot find php |
Make the PHP command-line executable available, then rerun php -v. |
PHP cannot open palette-check.php |
Run pwd and check that the saved file is in that folder, with the same name. |
| Status 2 or a usage message | Use light or dark followed by harmony_1 or harmony_2. Quote the optional six-digit hex value, including its #. |
| The ratio is unexpected | Read the two colors printed on the line. Check the ink and surface values before comparing the ratio. |
Try a foreground of your own
Try using #79849a as the label color on both dark surfaces. Predict which background will return the lower ratio, using the comparison in step 3.
php palette-check.php dark harmony_1 '#79849a'
php palette-check.php dark harmony_2 '#79849a'
You should get 4.08:1 FAIL on harmony_1 and 4.52:1 PASS on harmony_2. The candidate would work on the second surface for this normal-text check. A label that moves between both surfaces needs a color that passes on both.
Apply the check to a theme
You now have a way to check the specific text and background pair that a component renders. In your own theme, record which palette role supplied the background and check the final text color against it. Run the check in each appearance affected by the change.
This example checks one pair of opaque colors at a time. A translucent background or gradient requires checking the colors visible behind the text. Focus indicators and other controls have their own checks. W3C non-text contrast guidance