Skip to content
PHP Logo

How can you generate grayscale tones in a color table using PHP?

To generate grayscale tones in a color table using PHP, you can create a loop that iterates from 0 to 255 and sets the RGB values to the same value to create shades of gray. This can be achieved by assigning the same value to the red, green, and blue components in the color table.

php
<?php
echo "<table>";
for ($i = 0; $i <= 255; $i++) {
    $gray = $i;
    $color = sprintf("#%02x%02x%02x", $gray, $gray, $gray);
    echo "<tr><td style='background-color: $color; width: 50px; height: 50px;'></td></tr>";
}
echo "</table>";
?>