• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

How to "mix" two Colors the right way?

5 posts in this topic

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

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

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

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  
Followers 0