B.Esen
B.Esen

Reputation: 43

Generating random flat colors with php

I want to use flat colors for every categories and It must be random and unique.

I've found that website. => http://flatcolors.net/random.php

This is exactly what i need !

How can i generate that with Php? and it would be awesome if It's generate like "Base Flat Color (example. Flat Green)" and with similar other different 25 colors etc. (example. Flat Green Another Tones)

Upvotes: 0

Views: 979

Answers (1)

Alexandru Cosoi
Alexandru Cosoi

Reputation: 1000


Hello, B.Esen

In RGB color model a color has 3 component that represent the amount of red, green, blue in that color. So in order to generate a random color all you have to do is generate 3 random number for 0 to 255, convert then to base 16 and concatenate the results into something like this #ff23ab where ff => 255 (the amount of red), 23(base 16) => 35 (the amount of green) and ab(base 16) is 171(the amount of blue). The base colors are found at the 'edges' of the spectrum, where r,g,b have values of 0 or 255, excluding the white and black where all of them are 0 or 255.

So you have the following base colors

  1. 0000ff -> blue
  2. 00ff00 -> green
  3. ff0000 -> red
  4. ffff00 -> yellow
  5. ff00ff -> magenta
  6. 00ffff -> cyan

I don't know what you refer to as "flat color", but the class below should provide you with a decent configurable color generator, that uses the theory above to generate colors without the gray variations (at least one of the generators is 0). Keep in mind that the code bellow is not "production ready", and is intended to have didactic purpose. Before you use it in production you need to make it foolproof (set boundaries, check division by zero, etc).

class ColorGenerator
{
    /**
     * Used to set the lower limit of RGB values.
     * The higher this value is the fewer gray tone will be generated 70+ to 100 recommended
     *
     * @var int
     */
    protected static $lowerLimit = 70;

    /**
     * Used to set the higher limit of RGB values.
     * The higher this value is the fewer gray tone will be generated 180+ to 255 recommended
     *
     * @var int
     */
    protected static $upperLimit = 255;

    /**
     * Distance between 2 selected values.
     * Colors like ff0000 and ff0001 are basically the same when it comes to human eye perception
     * increasing this value will result in more different color but will lower the color pool
     *
     * @var int
     */
    protected static $colorGap = 20;

    /**
     * Colors already generated
     *
     * @var array
     */
    protected static $generated = array();

    /**
     * @return string
     */
    public static function generate()
    {
        $failCount = 0;
        do {
        $redVector = rand(0, 1);
        $greenVector = rand(0, 1);
        $blueVector = rand(!($redVector || $greenVector), (int)(($redVector xor $greenVector) || !($redVector || $greenVector)));
        $quantiles = floor((self::$upperLimit - self::$lowerLimit) / self::$colorGap);

        $red = $redVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit);
        $green = $greenVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit);
        $blue = $blueVector * (rand(0, $quantiles) * self::$colorGap + self::$lowerLimit);
        $failCount++;
        } while (isset(self::$generated["$red,$green,$blue"]) && $failCount < 1000);

        return self::rgb($red, $green, $blue);
    }

    /**
     * @param int $red
     * @param int $green
     * @param int $blue
     * @return string
     */
    protected static function rgb($red, $green, $blue)
    {
        $red = base_convert($red, 10, 16);
        $red = str_pad($red, 2, '0', STR_PAD_LEFT);

        $green = base_convert($green, 10, 16);
        $green = str_pad($green, 2, '0', STR_PAD_LEFT);

        $blue = base_convert($blue, 10, 16);
        $blue = str_pad($blue, 2, '0', STR_PAD_LEFT);

        return '#' . $red . $green . $blue;
    }
}

Hope this helps. Happy coding

Alexandru Cosoi

Upvotes: 1

Related Questions