• Welcome to Dark Bit Factory & Gravity.
 

looping through all possible colors?

Started by Kirl, January 30, 2011

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Kirl

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?
www.kirl.nl
Challenge Trophies Won:

Xetick

3 nested for loops would be the way to go
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won:

Jim

Might be a significant saving if you did

for (a=0; a < (1<<24); a++)
{
r = a&0xff0000;
g =a&0xff00;
b = a&0xff;
}

Jim
Challenge Trophies Won:

rain_storm

Quote from: Jim on January 30, 2011
Might be a significant saving if you did

for (a=0; a < (1<<24); a++)
{
r = a&0xff0000;
g =a&0xff00;
b = a&0xff;
}

Jim
Ever the optimist (optimizer?)

Challenge Trophies Won:

Xetick

#4
Quote from: Jim on January 30, 2011
Might be a significant saving if you did

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

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
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won:

Jim

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
Challenge Trophies Won:

Kirl

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.


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;
www.kirl.nl
Challenge Trophies Won:

hellfire

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.
Challenge Trophies Won:

elizdaavis

HSL(hue, saturation, and lightness) and  HSV(hue, saturation, and value) will help you solve the problem..