Thomas
Thomas

Reputation: 8978

How can I loop through colors?

I would like to change smoothly the background color of a view over time.

Choosing 2 colors c1 and c2, it is easy in the rgb space to make a color function of the time c(t) = t.c1 + (1-t).c2, which will be a gradient between the two

However the rgb space is 3D and I am looking for a way to enumerate all colors smoothly. I know there are billions of colors possible but coding RGB on 4x4x4 bits would be ok for my project.

My question is independent of the choice of the programming language

I have tried things like (pseudo code / Python)

for red in xrange(16):
  for green in xrange(16):
     for blue in xrange(16):
       color = rgb(red, green, blue)

however this is not smooth at all, it is like a "step function" if you see what I mean (color will go from plain green to no green at all and a little red for instance)

Any idea how to do that ?

Upvotes: 0

Views: 2361

Answers (1)

Tcanarchy
Tcanarchy

Reputation: 770

In that case I think you're better of with the HSV values of a color. You can use the same tactic and change only one of these HSV values at a time, to make the color shift more fluently.

Check this out (HSV)

EDIT:

After looking a bit further, I found this argument why NOT to use HSV. This might be a good solution to your problem:

Solution

Upvotes: 1

Related Questions