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

03-08-2010, 04: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:

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,155);//5th time function ran ABB
array(155,205,205);//10th time function ran BAA
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.


03-09-2010, 03: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
//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 it was set to false then we don't want to use previous values so we reset the static values to 0.
}//from: if($bMode===false)

//If we hit the max range value then we increment the next and start cycling again
//Retrieve the combination

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

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

for($i=1; $i<=40;$i++)
print_r(nextRGBColor($aColors));echo '<br/>';
echo '<hr/>';
print_r(nextRGBColor($aColors,false));echo '<br/>';
for($i=1; $i<=100;$i++)
print_r(nextRGBColor($aColors));echo '<br/>';

Hope it helps someone.

Best Wishes

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

03-09-2010, 01: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:
so I increment:
It should read (if in base 3):
but I figured it would read:
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.



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.