0

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)

B.Esen
  • 43
  • 7
  • 1
    What do you actually mean by "flat color"? And why can't you simply create a random color value and prevent reusage by means of a simple array of already used colors you check against? So _what is your specific question here_? – arkascha Sep 21 '16 at 07:46
  • 1
    What you have tried so far to achieve this ? – Sulthan Allaudeen Sep 21 '16 at 07:47
  • @arkascha Yeah I can create a random color but It also gives me some bad colors.You will understand if you know what does Flat Colors mean.So I guess I have to filter my random colors but how?. Maybe I couldn't explain clearly because of my english.. – B.Esen Sep 21 '16 at 08:15
  • @Sulthan Allaudeen http://stackoverflow.com/questions/7263140/random-color-generation-using-php => I've tried that code which one has 20 voted on answers. It's cool but It gives me some bad colors too. I just want to generate Flat Colors, – B.Esen Sep 21 '16 at 08:15
  • 1
    Sounds like you yourself don't really know how to define what a "falt color" actually is. So that should be the first question you have to answer yourself. Afterwards writing some lines of code should not be that hard... – arkascha Sep 21 '16 at 08:17
  • I am also not understanding what you exactly mean by "flat" and "bad" color, so I am voting for a close because of lack of clarity. – Matthias W. Sep 21 '16 at 08:43

1 Answers1

1

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

Alexandru Cosoi
  • 1,000
  • 5
  • 9