Author Topic: looping through all possible colors?  (Read 4227 times)

0 Members and 1 Guest are viewing this topic.

Offline Kirl

  • Senior Member
  • Pentium
  • ********
  • Posts: 1217
  • Karma: 230
    • View Profile
    • Homepage
looping through all possible colors?
« 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?
www.kirl.nl
Challenge Trophies Won:

Offline Xetick

  • Atari ST
  • ***
  • Posts: 132
  • Karma: 80
    • View Profile
    • Plane9
Re: looping through all possible colors?
« Reply #1 on: January 30, 2011 »
3 nested for loops would be the way to go
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: looping through all possible colors?
« Reply #2 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
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: looping through all possible colors?
« Reply #3 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?)

Challenge Trophies Won:

Offline Xetick

  • Atari ST
  • ***
  • Posts: 132
  • Karma: 80
    • View Profile
    • Plane9
Re: looping through all possible colors?
« Reply #4 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
« Last Edit: January 30, 2011 by Xetick »
Plane9 - Home of the Plane9 3d screensaver/music visualizer
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: looping through all possible colors?
« Reply #5 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
Challenge Trophies Won:

Offline Kirl

  • Senior Member
  • Pentium
  • ********
  • Posts: 1217
  • Karma: 230
    • View Profile
    • Homepage
Re: looping through all possible colors?
« Reply #6 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;
www.kirl.nl
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: looping through all possible colors?
« Reply #7 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.
Challenge Trophies Won:

elizdaavis

  • Guest
Re: looping through all possible colors?
« Reply #8 on: March 06, 2011 »
HSL(hue, saturation, and lightness) and  HSV(hue, saturation, and value) will help you solve the problem..