Hello forums !!
I am getting problem in generating unique hexcolors ie #XXXXXX
Can anybody provide me the help regarding generation of unique hexcolors ??
Thanks to all of You in advance..
how to generate unique hexcolors ??
What do you mean exactly by unique ? Do you mean random or has the generated color to be different from any previously generated color ?
"has the generated color to be different from any previously generated color"
this is the one..
thanks for the reply
Not sure what you want, but to generate a random color:
$color = sprintf('#%02X%02X%02X', rand(0, 255), rand(0, 255), rand(0, 255));
has the generated color to be different from any previously generated color
Well, that's easy enough. Start with black, increment for each new color until you reach white. Commit suicide if you need more colors after that :p
Just to add some lines to NogDog's code...
*** WARMING : you MUST put some code in the Is_Color_Unique() function, otherwise the script will never end/time out ! You have been warned !
function Is_Color_Unique($strColor) {
$bolIs_Unique = false;
// some code to check if color is unique
// Will set $bolIs_Unique to true only if the color is unique
return $bolIs_Unique;
}
function Generate_Color() {
return sprintf('#%02X%02X%02X', rand(0, 255), rand(0, 255), rand(0, 255));
}
$strColor = Generate_Color();
while(!Is_Color_Unique($strColor)) {
$strColor = Generate_Color();
}