Generating a Random Hex Color Code with PHP

Generating a random hex color code with PHP

Get a random number from 0 to 255, then convert it to hex:

function random_color_part() {
return str_pad( dechex( mt_rand( 0, 255 ) ), 2, '0', STR_PAD_LEFT);
}

function random_color() {
return random_color_part() . random_color_part() . random_color_part();
}

echo random_color();

How to generate a random DARK hex color code with PHP?

Light colors use high values. Lowering max argument for rand() should create darker colors, like this:

function random_color_part() {
return str_pad( dechex( mt_rand( 0, 127 ) ), 2, '0', STR_PAD_LEFT);
}

But you will have to experiment with an exact threshold to get right colors.

You can use this snippet to check what happens with colors when you lower max color value (max).

function hex(i) {  var hex = i.toString(16);  if (hex.length == 1) {    hex = '0' + hex;  }  return hex;}
var max = 150;
for (var i = 0; i < max; i += 10) { var td = ''; for (var j = 0; j < max; j += 10) { for (var k = 0; k < max; k += 10) { var color = '#' + hex(i) + hex(j) + hex(k); td += '<td style="background-color:' + color + ';"> </td>'; } } $('table').append('<tr>' + td + '</tr>');}
table {  border-collapse: collapse;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table></table>

PHP random colors for every row

You can userand() function to get random numbers in specific range.
Take a look here

Example:

$color = "#" . rand(10,100);

you can replace $color wherever you want.

If you want hex colors use this function

function random_color(){
mt_srand((double)microtime()*1000000);
$c = '';
while(strlen($c)<6){
$c .= sprintf("%02X", mt_rand(0, 255));
}
return $c;
}

How to auto-generate random hex color code in SQL?

I think this works

SELECT '#' +  CONVERT(VARCHAR(MAX), CRYPT_GEN_RANDOM(3), 2) AS Color

php dechex with dark colors only

Here how to get dark color for both Hex and RGB

$hexMin = 0;
$hexMax = 9;
$rgbMin = 0;
$rgbMax = 153; // Hex 99 = 153 Decimal
$hex = '#' . mt_rand($hexMin,$hexMax) . mt_rand($hexMin, $hexMax) . mt_rand($hexMin, $hexMax) . mt_rand($hexMin,$hexMax) . mt_rand($hexMin, $hexMax) . mt_rand($hexMin, $hexMax);
$rgb = 'rgb(' . mt_rand($rgbMin,$rgbMax). ',' . mt_rand($rgbMin,$rgbMax). ',' . mt_rand($rgbMin,$rgbMax). ')';

Generate random colors

Your original code doesn't work as you'd expect - if a low number is generated you might get #1ffff (1 being the low red value) - which is invalid. It is much more stable to use this:

echo "rgb(".$r.",".$g.",".$b.")";

Since rgb(123,45,67) is perfectly valid colour specification.

Along similar lines, you can generate random numbers for hsl:

echo "hsl(".rand(0,359).",100%,50%)";

This will generate fully saturated, normal lightness colours of any hue. However, note that only recent browers support HSL so you may be better off coerting to RGB if browser support is a concern.

Generate Hex color code randomly

There is no PHP built-in function

$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)];

Then echo out the $color value anywhere you need it.

PHP get random colors which should compatible with text color white?

You can use this code to generate random dark colors. You can also change the darkness threshold by changing 127 in the function parameter.

function random_color_part($threshold = 127)
{
$dt = '';
for ($o = 1; $o <= 3; $o++) {
$dt .= str_pad(dechex(mt_rand(0, $threshold)), 2, '0', STR_PAD_LEFT);
}
return '#' . $dt;
}


Related Topics



Leave a reply



Submit