Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: Kirl on January 30, 2011

Title: looping through all possible colors?
Post by: Kirl on January 30, 2011
So I've got three color values (r, g, b) that each range from 0 to 255 and I want to step through them so that every pssible combination is made at least once.

Any bright, loopy ideas on how to achieve this?
Title: Re: looping through all possible colors?
Post by: Xetick on January 30, 2011
3 nested for loops would be the way to go
Title: Re: looping through all possible colors?
Post by: Jim on January 30, 2011
Might be a significant saving if you did
Code: [Select]
for (a=0; a < (1<<24); a++)
{
r = a&0xff0000;
g =a&0xff00;
b = a&0xff;
}
Jim
Title: Re: looping through all possible colors?
Post by: rain_storm on January 30, 2011
Might be a significant saving if you did
Code: [Select]
for (a=0; a < (1<<24); a++)
{
r = a&0xff0000;
g =a&0xff00;
b = a&0xff;
}
Jim
Ever the optimist (optimizer?)
Title: Re: looping through all possible colors?
Post by: Xetick on January 30, 2011
Might be a significant saving if you did
Code: [Select]
for (a=0; a < (1<<24); a++)
{
r = a&0xff0000;
g =a&0xff00;
b = a&0xff;
}

I would call premature optimization on this one. First of the code wont work as expected since it doesn't shift down the values. This should be more correct
Code: [Select]
for (a=0; a < (1<<24); a++)
{
r = (a&0xff0000)>>16;
g = (a&0xff00)>>8;
b = a&0xff;
}
But since its doing 3 ands and now 2 shifts per color its probably actually slower then the more clearer 3 for loops

But as always, dont assume, if this is your bottleneck then test whats faster
Title: Re: looping through all possible colors?
Post by: Jim on January 30, 2011
It depends if you need the colour components in the lower 8 bits or if you want colour values you can stick down straight on to the screen, then there'd be no shifting or masking.

Jim
Title: Re: looping through all possible colors?
Post by: Kirl on February 03, 2011
Thanks for the help! I wanted to try a more random solution and I ended up using kind of a clumsy aproach. I now change r,g and b each with a random speed, which seems to work alright in smoothly shifting through the colors in a randomish manner, Cleaner solutions always welcome.

Code: [Select]
if(r+rv >= 240) rv = -1 + Math.random()*-step;
else if(r+rv <= 20) rv = 1 + Math.random()*step;

if(g+gv >= 240) gv = -1 + Math.random()*-step;
if(g+gv <= 20) gv = 1 + Math.random()*step;

if(b+bv >= 240) bv = -1 + Math.random()*-step;
else if(b+bv <= 20) bv = 1 + Math.random()*step;

r += rv;
g += gv;
b += bv;
Title: Re: looping through all possible colors?
Post by: hellfire on February 03, 2011
So the question was how to step *smoothly* through all possible colours  ;)
You can probably get better control over your colours by working in HSL (http://en.wikipedia.org/wiki/HSL_and_HSV).
Title: Re: looping through all possible colors?
Post by: elizdaavis on March 06, 2011
HSL(hue, saturation, and lightness) and  HSV(hue, saturation, and value) will help you solve the problem..