Sign in to follow this  

Combining RGB values

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.53

Result = ((256,128,128) / N) * 255 => (208.21,104.10,104.10)

Share this post


Link to post
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);
}


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

Share this post


Link to post
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 this post


Link to post
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;}

//Or

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

Share this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this