Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


How to "mix" two Colors the right way?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Mekuri   Members   -  Reputation: 300

Like
0Likes
Like

Posted 19 February 2013 - 03:00 PM

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 :-)

 


Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri


Sponsor:

#2 Seabolt   Members   -  Reputation: 633

Like
3Likes
Like

Posted 19 February 2013 - 03:22 PM

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.


Perception is when one imagination clashes with another

#3 Servant of the Lord   Crossbones+   -  Reputation: 21147

Like
1Likes
Like

Posted 19 February 2013 - 03:38 PM

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.

So:

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

	TeamColorRGB
	ActualRGB = TeamColorRGB * Shadowing

	//Or more accurately:
	//TeamColorHSL.saturation = Shadowing;
}
else
{
	//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.


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Mekuri   Members   -  Reputation: 300

Like
0Likes
Like

Posted 19 February 2013 - 03:47 PM

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 :-)


Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri


#5 osmanb   Crossbones+   -  Reputation: 1628

Like
1Likes
Like

Posted 19 February 2013 - 06:27 PM

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.



#6 Mekuri   Members   -  Reputation: 300

Like
0Likes
Like

Posted 20 February 2013 - 03:41 PM

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.

Solution

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

 

EDIT:

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, 20 February 2013 - 04:11 PM.

Check out the game I am making here - http://www.youtube.com/user/NasarethMekuri





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS