Zufällige Hex-Farbe

Avatar of Chris Coyier
Chris Coyier am

Technik #1

<?php 
    
    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
    
?>

Geben Sie dann den Wert von $color aus, wo immer Sie ihn benötigen. Zum Beispiel

<body style="background: <?php echo $color; ?>;">

Technik #2

<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>

Es gibt auch eine JavaScript-Version.