Home Generate a Random Color Value With PHP

    Generate a Random Color Value With PHP


    Hexadecimal (also base 16, or hex) is a positional numeral system with a radix, or base, of 16. It uses sixteen distinct symbols: 0–9 to represent values zero to nine, and case-insensitive A, B, C, D, E, F to represent values ten to fifteen. Since colors are represented by HEX values between 0x000000 and 0xffffff (where 0x simply indicate the number is in hexadecimal), we simply select a random value between the two limits.

    The following functions are just examples of how you might generate a random value from white to black. Since 0xffffff translates to a numeric value of 16777215, you can use it as an alternative to the HEX limit (with the former requiring a conversion back to the HEX value).

    Only one function should be used. The last function isn’t overly efficient.

    If you wanted to generate a random color value with RGB values (for example, rgb(255,255,255)), use the following:

    Short URL for this post: http://shor.tt/1XI1