...

View Full Version : Rotating colors function. Base $X math



MattyUK
03-08-2010, 03:00 AM
I could use some starting pointers for a function I am about to start.

I need to feed RGB values to a graph class. I will need the colors to be different enough so I have settled on a range of numbers with enough visual difference between the resulting colors. I plan to write a function that every time it is run simply returns the next combination of RGB values from the ranges given. This way it will cycle through all possible colors each time it is called within the script.

Lets say I hard code this range:

array(205,155,105);
Then it'd return a single array with the values below for each time it is ran:

array(205,205,205);//1st time function ran AAA
array(205,205,155);//2nd time function ran AAB
array(205,205,105);//3rd time function ran AAC
array(205,155,205);//ABA
array(205,155,155);//5th time function ran ABB
array(205,155,105);//ABC
array(205,105,205);//ACA
array(205,105,155);//ACB
array(205,105,105);//ACC
array(155,205,205);//10th time function ran BAA
array(155,205,155);//BAB
array(155,205,105);//BAC
array(155,155,205);//BBA
array(155,155,155);//BBB
array(155,155,105);//11th time function ran BBC
//and so on

Until all possible combinations have been exhausted, at which point it starts again at the beginning.

I need the function to support any number of ranges (may be more than 3).

So I was thinking of these approaches.
I could use a global variable or session variable to store a count of the last combination returned. Session isn't necessary but might be nice to continue across the page loads.
I could count the hard coded ranges array then if I could somehow put a variable into that math base (say base 3 for the example) I could simply increment it and use that as a pointer to which array variable to output. But I'm not familiar with PHP's math functions.

So in conclusion, I'm not really sure where to start. Can anyone help with some pointers please. I get the feeling it could be a lot easier than I anticipate once I'm latched onto the right approach.

Matt

MattyUK
03-09-2010, 02:40 AM
Well I couldn't find anyway to have a int variable increment in a different math base so I had to come up with the following for the function.


function nextRGBColor($taColors=array(205,155,105),$bMode=true)
{
//Author: Matthew Newton
//Function to return rotating RGB values from a fixed range supplied.
//take the tainted array and ensure we only deal with int values
$aColors=array_map('intval',$taColors);
//what range are we dealing with?
$iRange=count($aColors);//Range has X values

static $iRc=0;
static $iGc=0;
static $iBc=0;//Set the counts at zero but static so they don't lose values each time the function is called.
if($bMode===false)
{//if it was set to false then we don't want to use previous values so we reset the static values to 0.
$iRc=0;
$iGc=0;
$iBc=0;
}//from: if($bMode===false)

//If we hit the max range value then we increment the next and start cycling again
if($iBc>=$iRange){$iBc=0;$iGc++;}
if($iGc>=$iRange){$iGc=0;$iRc++;$iBc=0;}
if($iRc>=$iRange){$iGc=0;$iRc=0;$iBc=0;}
//Retrieve the combination
$iR=$aColors[$iRc];
$iG=$aColors[$iGc];
$iB=$aColors[$iBc];

$iBc++;//Increment it, it will cascade the others as it goes.

return array($iR,$iG,$iB);
}//from: function nextRGBColor($taColors)

$aColors=array(205,155,105);
for($i=1; $i<=40;$i++)
{
print_r(nextRGBColor($aColors));echo '<br/>';
}
echo '<hr/>';
$aColors=array(205,155,105,55);
print_r(nextRGBColor($aColors,false));echo '<br/>';
for($i=1; $i<=100;$i++)
{
print_r(nextRGBColor($aColors));echo '<br/>';
}
exit;

Hope it helps someone.

Best Wishes
Matt

JAY6390
03-09-2010, 03:20 AM
what about base_convert() (http://www.php.net/base_convert)?

MattyUK
03-09-2010, 12:16 PM
Thanks Jay. I did consider it. I think it only converts between bases. Doesn't seem to set a variable to increment in a different base.

Let's say I want base 3 (0 1 2) and have:
$var=122;
so I increment:
$var++;
It should read (if in base 3):
200
but I figured it would read:
123
Base convert would take decimal 123 into 11120. Hence the issue.

Please let me know how to use base_convert if I've got it turned around or just plain wrong.

Thanks.

Matt

PS:
Perhaps I should try converting 123 base 3 into base 3. See if it corrects the invalid base 3 number (123) into a correct base 3 number (200).
I might give that a shot after all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum