tags:

views:

1028

answers:

5

Given two colors and n steps, how can one calculate n colors including the two given colors that create a fade effect?

If possible pseudo-code is preferred but this will probably be implemented in Java.

Thanks!

+10  A: 

divide each colour into its RGB components and then calculate the individual steps required.

oldRed = 120;
newRed = 200;
steps = 10;
redStepAmount = (newRed - oldRed) / steps;

currentRed = oldRed;
for (i = 0; i < steps; i++) {
   currentRed += redStepAmount;
}

Obviously extend that for green and blue.

nickf
The code as shown will never reach newRed, both because of integer truncation and an off-by-one error. Better to restructure as currentRed = oldRed + ((i * (newRed - oldRed)) / (steps - 1).
Mark Ransom
yeah i was thinking of that as I was writing it - but there's a few ways around that, and I thought i'd leave an exercise for the implementer :-)
nickf
+1  A: 

The quesiton is what transformation do you want to occur? If you transpose into the HSV colourspace and given

FF0000 and 00FF00

It will transition from red through yellow to green.

However, if you define "black" or some other shade as being the mid-point of the blend, you have to shade to that colour first ff0000->000000->00ff00 or via white : ff0000 -> ffffff -> 00ff00.

Transforming via HSV however can be fun because you have to use a bit of trig to map the circular map into the vector components.

Kent Fredric
+1  A: 

The easiest thing to do is linear interpolation between the color components (see nickf's response). Just be aware that the eye is highly nonlinear, so it won't necessarily look you're making even steps. Some color spaces attempt to address this (CIE maybe?), so you might want to transform into another color space first, interpolate, then transform back to RGB or whatever you're using.

Drew Hall
The gamma that is applied to color values will help counteract the nonlinearity, so transforming to another color space is probably overkill.
Mark Ransom
+8  A: 

There are two good related questions you should also review:

Please note that you're often better off doing this in the HSV color space rather than RGB - it generates more pleasing colors to the human eye (lower chance of clashing or negative optical properties).

Good luck!

Adam Davis
+1  A: 

If you want a blend that looks anything like most color picker GUI widgets, you really want to translate to HSL or HSV. From there, you're probably fine with linear interpolation in each dimension.

Trying to do any interpolations directly in RGB colorspace is a bad idea. It's way too nonlinear (and no, gamma correction won't help in this case).

Tom
I beg to differ. There are plenty of operations that work better in other color spaces, but interpolation between two known endpoints isn't one of them.
Mark Ransom
i think that for many situations a basic linear RGB transform is satisfactory, eg: my textbox is red and I want it to fade nicely to green when it has been filled correctly. it all happens in less than a second and is sufficient.
nickf
@nickf - True, if it's a temporal effect, you have a lot more leeway than a spatial effect.@mark - What's the problem with interpolating in HSV? I've had quite a bit of success with it.
Tom