# Combining RGB values

This topic is 5420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have several ways of combining RGB values set up:
inline int BlendRGB(int a, int b, int c)			{ return (c*a + (255-c)*b)/255; }
inline int CombineRGB(int a, int b)					{ return ((a + b)*0.5); }
inline int OrRGB(int a, int b)						{ return (a ^ b); }
inline int AndRGB(int a, int b)						{ return (a & b); }


What Im trying to do is combine two textures into one buffer using perlin noise as an offset. Thi allows in interesting combination of textures except it leaves the color under saturated when I combine a greyscale base texture with a color value. What I want to know what is the best method to combine a color with a greyscale? Would I replace the grey value with an equivalent color value say: 128 128 128 blended with red is 128 0 0. If that is the case how to I do this without having to do if else statements. Instead I would like to use an operator such as & ^ | etc...Ive done this and the end results are generally an oversaturated image... I suppose my end question is are there better methods than using operators?

##### Share on other sites
Think of them as vectors. If gray is (128,128,128) on a scale of (0,0,0) to (255,255,255) and red is (128,0,0), blending them together must yield a value within the color space. Add them together and then normalize the result:

(128,128,128) + (128,0,0) => (256,128,128)N = sqrt(256^2 + 128^2 + 128^2) => 313.53Result = ((256,128,128) / N) * 255 => (208.21,104.10,104.10)

##### Share on other sites
I haven't worked with these enough to really know offhand their visual effects, but just glancing through DX's blending ops, one caught my attention that you might wanna try:
//derived from D3DTOP_ADDSMOOTH://(assuming 0.0 to 1.0 for component values, instead of 0 to 255)inline int AddSmooth(float a, float b){  return (a + b) - (a * b);  // OR (same thing)  return a + b * (1.0f - a);}

Oluseyi's sounds more obvious & promising, as always... [smile][/edit]

##### Share on other sites
Well neither one of those worked the way I wanted...

inline int CombineRGB2(float a, float b)			{ return (255 * ((a+b) / (sqrt(pow((a + b),2))))); }

Gave me all white...unless i need all three values in which case wont work either I need it channel by channel...

inline int AddSmoothRGB(int a, int b)				{ return a + b * (255 - a);}

Both returns you provided gave me RGB noise...

Ill play around some more and see what I come up with...

##### Share on other sites
You'd need to divide by 255 in that equation, too, to get it correct when working with 0-255 integers:
inline int AddSmoothRGB(int a, int b)				{ return a + (b * (255 - a)) / 255;}//Orinline int AddSmoothRGB(int a, int b)				{ return (a + b) - (a * b) / 255;}

##### Share on other sites
Thanks everyone for the point in the right direction...

This actually gave me the results I needed...I overloaded the sum and clamped it to not rise above 255.

return MIN(255,a * ((b+1)/192));

Which gave me this

Now I have to figure out a better method of producing seamless perlin noise. Any thoughts?

##### Share on other sites
I wanted to get a greyscale version of terrain textures and a single rgb value that best represented it. It looked pretty good but I had to use the D3D renderstate modulate2 or simialr which modulates the colour and texture and doubles it. I might be way off the subject here though?

• 17
• 10
• 21
• 16
• 9