...

View Full Version : Generating Hex Codes



Raul Duke
02-09-2006, 04:52 AM
I have a function here that generates hex codes for the use of inputing them into html for colors. The function works great, my problem is that sometimes the code it generates ends up a 5 digit hex code, which firefox dosn't like. Any suggestions on how I can ensure that each hex value created is two digits?

function randColor(){

$r= rand(0,255);
$g= rand(0,255);
$b= rand(0,255);
$rhex = dechex($r);
$ghex = dechex($g);
$bhex = dechex($b);
$color = $rhex . $ghex . $bhex;
return $color;

}

degsy
02-09-2006, 03:30 PM
You could put a bit of validation in


<?php
function randColor(){

$r= rand(0,255);
$g= rand(0,255);
$b= rand(0,255);
$rhex = dechex($r);
$ghex = dechex($g);
$bhex = dechex($b);
$color = $rhex . $ghex . $bhex;

if(strlen($color) == 6){
return $color;
}
}
for($x=0; $x<10; $x++){
$col = randColor();
if($col != ""){
echo '<span style="color: #' . $col . '">' . $col . '</span>';
echo '<br>' . "\r\n";
}
}
?>

dumpfi
02-09-2006, 04:56 PM
function randColor()
{
return randomColorValue().randomColorValue().randomColorValue();
}
function randomColorValue()
{
return str_pad(dechex(rand(0, 255)), 2, '0', STR_PAD_LEFT);
}dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum