How to generate random hex color codes in PHP

How to generate random hex color codes in PHP

PHP provides great support in creating different colors. In this article we will see how to generate random hex color codes in PHP.

Lets write some CSS to style the color boxes nicely:

<style type="text/css">
    .box {
        margin: 7px;
        padding: 7px;
        border-radius: 7px;
        height: 100px;
        width: 100px;
        text-align: center;
        display: inline-flex;
        align-items: center;
        color: #fff;
        font-size: 19px;
        font-family: sans-serif;
        font-weight: 300;
        letter-spacing: 1px;
        text-shadow: 0 0 6px rgb(0 0 0 / 30%);
    }

    .box-2{
        height: 20px; 
        width: 200px;
    }
</style>

Example 1: Generate hex color codes using a combination of string with 2 characters

In the following snippet we have first used the mt_rand() function and passed the color range which is 0 to 255. This function takes random number from the provided range.

Than we have used dechex() function which converts decimal to hexadecimal.

After that we have used str_pad() function to pads a string to a new length. All these steps makes sures that we get a maximum number of uniqie hex color codes.

<?php
    // generate random numbers and take first 2 characters from string
    function random_color_part() {
        return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
    }
    
    // generate random hex color codes
    function random_color() {
        return random_color_part() . random_color_part() . random_color_part();
    }
    
    // display 100 colors
    for($i = 0; $i <= 100; $i++){
        $random_color = random_color();
        echo '<div class="box" style="background-color: #'.$random_color.'">#'.$random_color.'</div>';
    }
?>
generate random hex color codes in PHP - example-1

Example 2: Generate hex color codes using sting shuffle function

In the below example we have generated 1,000 hex color codes using the string shuffle function.

The str_suffle() function randomly shuffles all characters of a string and the substr() function returns a part of string. This is actually a very good approach in generating large list of colors

<?php
    // generate 1,000 colors using string shuffle function
    for($i = 0; $i <= 1000; $i++){
       $random_color = '#'.substr(str_shuffle('AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF00112233445566778899'), 0, 6);
       echo '<div class="box-2 box" style="background-color: '.$random_color.'">'.$random_color.'</div>';
    }
?>
generate random hex color codes in PHP - example-2

Example 3: Generate random hex color codes codes using array of characters

Generating color codes using arrays[] is also a great approach. In this example we have randomly taken the characters of an array and interate it using for() loop.

However, this is an expensive approach if you are planning to generate large list of hex color codes.

<?php
    // create a custom hex generator
    function randomHex() {
       $chars = 'ABCDEF0123456789';
       $color = '#';
       for ( $i = 0; $i < 6; $i++ ) {
          $color .= $chars[rand(0, strlen($chars) - 1)];
       }
       return $color;
    }

    for($i = 0; $i <= 1000; $i++){
       $random_color = randomHex();
       echo '<div class="box" style="background-color: '.$random_color.'">'.$random_color.'</div>';
    }
?>
generate random hex color codes in PHP - example-3

Example 4: Generate RGBA color codes with opacity

In this example we have generated RGBA color codes with opacity and used the rand() function to take random numbers between 0 and 255. Opacity means adding transparency in a color.

<?php
    // create rgba colors with opacity
    for($i = 0; $i <= 1000; $i++){
       $random_color = 'rgba('.rand(0,255).', '.rand(0,255).', '.rand(0,255).', 0.73)';
       echo '<div class="box-2 box" style="background-color: '.$random_color.'">'.$random_color.'</div>';
    }
?>
generate random hex color codes in PHP - example-4

Example 5: Generate light shade color codes

In this example we have generated light color shades using the mt_rand() function and defined hex ranges from #FFFF00 and #FF9999.

Defining the color range will make sure the function will generate only specific range of colors.

<?php
    // light shade colors
    for($i = 0; $i <= 1000; $i++){
       $random_color = sprintf('#%06X', mt_rand(0xFF9999, 0xFFFF00));
       echo '<div class="box-2 box" style="background-color: '.$random_color.'; color: #444;">'.$random_color.'</div>';
    }
?>
example 5

Related Posts