Page 1 of 1

math question

Posted: 10.09.2004, 20:18
by Rassilon
OK Ive been trying to figure this out and Ive made it more difficult than it needs to be...Im trying to scale between two RGB values bepending on the value of say C

RGB1 = [ 0.85 0.62 0.72 ]

RGB2 = [ 0.63 0.44 0.90 ]

Now what I want to do is when scale value C = 255 then we reach RGB1 If its in the middle then we could average RGB1 and RGB2. When C = 0 then we reach RGB2...so how could I do this properly?

Posted: 10.09.2004, 20:29
by ajtribick
Suppose we have the value of the first colour's red component, call it 'a', and the second colour's red component, call it 'b'.

I think the component of the final colour would then be:

(c*a + (255-c)*b)/255

You then do this for the green component and blue component.

Posted: 10.09.2004, 22:56
by Rassilon
chaos syndrome wrote:Suppose we have the value of the first colour's red component, call it 'a', and the second colour's red component, call it 'b'.

I think the component of the final colour would then be:

(c*a + (255-c)*b)/255

You then do this for the green component and blue component.


Thanks that worked a hell of a lot better than this:

int blendRGB( int rgb1, int rgb2, int blend) {
float lo_p = ((255 - (float)blend)+1) / 256;
float hi_p = ((float)blend +1) / 256;
rgb1 *= lo_p;
rgb2 *= hi_p;
return ((rgb1 * rgb2) / 2);
}

I now use this:

#define BLENDRGB(a, b, c) (c*a + (255-c)*b)/255