Are there best practices for generating random colors in PHP to ensure readability on a website?

Generating random colors in PHP for a website can sometimes result in colors that are difficult to read or clash with the overall design. To ensure readability, it's best to generate random colors within a certain range of brightness and saturation. This can be achieved by limiting the hue to a specific range and adjusting the saturation and brightness levels accordingly.

function generateRandomColor() {
    $hue = mt_rand(0, 360); // Random hue value between 0 and 360
    $saturation = mt_rand(50, 100); // Random saturation value between 50 and 100
    $brightness = mt_rand(50, 100); // Random brightness value between 50 and 100

    return "hsl($hue, $saturation%, $brightness%)";
}

$randomColor = generateRandomColor();
echo "<div style='background-color: $randomColor; color: white; padding: 10px;'>Random Color</div>";