Jump to content
  • Advertisement
Sign in to follow this  

How to "mix" two Colors the right way?

This topic is 2071 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've recently finished a little character generator for my game where the player is able to customize the character by changing hair color, shirt color and pants color. I do this by having the sprite being white (or close to) and then simply tint the texture with a color, where the player can modify the RGB values. This works fine for my character generator, since there is no lighting involved there.

The problem is when you start the game. While in game, I do some tile based lighting, and the way I do that visually is by tinting all objects with the light level. For example if the player is standing next to a torch, he/she will be fully lit, thus I tint him/her with  a color, where all the RGB values equals 255 (White). The darker it gets, the lower the light level.

My problem is, that I now have two colors that I use to tint the player (The color the player chose in the generator, and the light level), and I know that it is somehow possible to mix these colors.

I've tried/considered the following:

- I've tried using Color.Lerp, but I am unable to get some decent results.

- I've considered simple adding the RGB values from each color with each other, but that'll get me nowhere, since in most cases I'll end up with a pure white color, or if it is dark, the "generator color" will be shown.

- Another solution I've thought of, is rendering the sprite with the player colors (if possible) to a render target and then use that as the sprite. The problem with this is, that my sprite is divided into several parts, to accommodate coloring the individual parts, and I would have to write code separately for using render targets (instead of my current sprite sheets) for each tint-able body part.


So to sum it up:

- How would I combine two colors, one color representing a player chosen tint, the other representing a light level and get results where the player chosen color gets darker or lighter based on the light level?


If I've been unclear about how the coloring of body parts work, I have a video I made earlier today showing what I mean. If you don't want to see the whole thing, go to around 0:30 and see from there:




Thanks for reading :-)


Share this post

Link to post
Share on other sites

Generally, we light using multiplication. If your light values are clamped to be in 0-1 range, then it would be:


pixelColor = playerColor.rgb * lightColor.rgb;


lerp works fine for doing a linear interpolation between colors, but not a blend.

Share this post

Link to post
Share on other sites

Someone wrote about this on their journal a few months ago, but I lost the link. sad.png
I have my notes though:

"R=B && G=0"

"check for R==B&&G==0 and multiply by the target color"
"or change the G channel to represent saturation and only check R==B"

Image a character in a RTS, where the character changes colors of clothing or flags or whatever, based on your team color.
Make the character's normal colors the normal colors... but make the "team colors" be hot pink magenta.
But then, to shade the team colors, make it darker and darker colors of that pink/magenta. Just always make the Red channel and the Blue channel be equal, and the green channel be 0, for team colors. This way, your team color can still be shaded on the sprite.

So for example:
255, 255, 0 would be the brightest team color pixel.
254, 254, 0 would be one less.
253, 253, 0 one less again.

But something like 254, 255, 0 would NOT be replaced with the team color, because red and blue aren't equal.
Also, 255, 255, 1 would NOT be replaced with the team color, because green isn't zero.


ActualRGB = (...); 
If Red == Blue AND Green = 0
	Shadowing = Red; //Or blue

	ActualRGB = TeamColorRGB * Shadowing

	//Or more accurately:
	//TeamColorHSL.saturation = Shadowing;
	//Keep the original pixel of the sprite.
	ActualRGB = (Red, Green, Blue); 

There are many other ways to mix colors. You can convert from RGB to HSL (Hue Saturation Luminance), then you can just up the Luminance, then convert back to RGB.
Adding two values would definitely increase to full 255 too quickly. You might try simply averaging the two colors... ((a + b) / 2). I also like using Seabolt's 'convert to 0.0 - 1.0 floats and multiply' method. I post some blending algorithms here a few years ago.

Share this post

Link to post
Share on other sites

Wow, that was some fast answers :-)


I think I will try both your suggestion Seabolt about converting to 0.0 - 1.0, and Servant Of The Lords suggestion of taking the average of the two colors. Also I'll keep a link to your blending algorithms, they might come in handy, thanks a lot :)


I will report back here with the solution(s) when I'm done. Hopefully I'll have the time to look at it tomorrow evening :)


Thanks a lot for two fast and awesome responses :-)

Share this post

Link to post
Share on other sites

Photoshop's Overlay blend mode is also a great way to get more interesting color combination, particularly if you're working with a limited color range and your inputs aren't necessarily "light" and "albedo". Check out http://inlandstudios.com/en/?p=851 for a breakdown of the math.

Share this post

Link to post
Share on other sites

Hey again.


So I tried out a few solutions as you guys suggested, and ended up with a solution (Skip ahead to solution if you don't want to read about my experiences).

First off I tried averaging the values  ((a + b) / 2), but the results weren't great. At higher light levels it actually seemed to work out, but when I went into the darker areas, the colors would not be dimmed enough.

I then tried to convert the values to a float between 0.0 - 1.0 by doing like this:

float r = ((_hairColor.R / 255) * (_lightColor.R / 255)) * 255. I did this for the B and G value as well. This gave me two different results. This would cause the hair to be black, unless the area was very light, which would turn the hair red (in the case the hair should be blonde/yellow). If I then added an f after all the 255 in the code (to make sure it was treated as floats) the hair reacted similar to when I took the average values.


So I looked back here and noticed this:


I also like using Seabolt's 'convert to 0.0 - 1.0 floats and multiply' method.


So I quickly went back and ended up with the following code:



float scale = lightLevel / 255f;
Color modifiedHairColor = Color.Multiply(_hairColor, scale);
Color modifiedChestColor = Color.Multiply(_chestColor, scale);
Color modfifiedLegColor = Color.Multiply(_legColor, scale);



I've seen the multiply method before, but never really realized how to use it, until now smile.png

So that was the end result, and it seems to be working out. I've tested in very dark areas and very lit areas, and it works just as I wanted it to.


Thanks a lot for the awesome and fast feedback, it is really appreciated.  biggrin.png


Also Osmanb I've bookmarked the link you provided. I am currently taking math classes so I might want to look it over in the weekend, thanks! smile.png



So just after I posted this I realized that a little change was needed for this to work properly. When using the multiply method with a scale, it seems it also applies to the Alpha value of the color. This is simply fixed my setting the alpha value to 255 (or any other value needed). Just figured I'd add that. smile.png

Edited by Mekuri

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!