Latest Tweets:
Here’s a copy of a post I placed back on 31MAY08 for my “final project” at Full Sail University…
So I’ve been trying to figure out how to get my shaders to implement gamma corrections and saturation modification. This may not seem like a big deal but truth be told, if you’re using a pretty crappy monitor, these features can make or break a game.
After toiling on the internet finding equations on the Wikipedia and other miscellaneous websites, I derived my gamma correction formula which is as follows…
OUT = IN ^ (1/GAMMA)
Basically, you would take the components of your color vector (IN) and increase it exponentially by 1/GAMMA. GAMMA would be a floating point number between 0.5 and 2.2 ideally though you can modify these values to your heart’s content. I realize that there are ways to handle this through the OS or rendering API (most of them anyways) but I wanted to find a way to allow a player to modify these values and play a game in windowed mode without impacting their desktop as they play.
Saturation modification took a bit less time to figure out but was still a bit tricky as there are actually steps you must go through first as opposed to just running it through a single step, here is the procedure for that…
First you will need to determine what I call the “De-saturation constant” (yeah, probably a misnomer, but whatever). Basically, if you have a color like red (255, 0, 0) or blue (0, 0, 255), you will need to figure out what they look like completely de-saturated. This number can be found by taking the minimum value and adding to it the difference of the maximum and minimum value and divide by two. This number will then be applied to every color field. Here is the formula and an example:
FORMULA:
DSC = INmin + ( (INmax – INmin) / 2 )
EXAMPLE:For the arbitrary color (192, 64, 32)
DSC = 32 + ( (192-32) / 2)
= 112
The fully “desaturated” version of the color will be (112, 112, 112).
Now that we know what the color looks like if it were going to be placed in an episode of “I Love Lucy”, we can use that as our “base”. The BASE is useless without the DELTA however. The DELTA is the difference between the normal color and the fully “desaturated” version of that normal color. Here’s the example:
FORMULA:
DELTA = (NORMAL – DSCVERSION);
EXAMPLE:
DELTA = (192, 64, 32) – (112, 112, 112)
= (192-112, 64-112, 32-112)
= (80, -48, -80)
If you do a quick check on your work, you’ll realize that adding the DELTA to the “desaturated” color will return us back to normal; this is how we get around from “colorless” to normal and even into over-saturation. We basically would scale our DELTA vector in order to properly display what we would like. Want to take out color? Make your SATURATION scale value 0.0f. Want to make it have just half the color to compensate for a funky monitor? Set it to 0.5 for half color. Want to keep it as is? Leave it at 1.0f (which I’m considering leaving a check in the shader to bypass the entire process if it is) and be happy. Here’s the implementation of the final step.
FORMULA:
OUT = DSCVERSION + (DELTA * SATURATION)
Remember that SATURATION is a scale value on a vector of floats.
EXAMPLES
OUT = (112, 112, 112) + ( (80, -48, -80) * 1.0 )
= (112 + 80, 112 – 48, 112 – 80)
= (192, 64, 32)
= ORIGINAL VALUE!!!
OUT = (112, 112, 112) + ( (80, -48, -80) * 0.5 )
= (112 + 40, 112 – 24, 112 – 40)
= (152, 88, 72)
= Color’s are dulled by 50%
OUT = (112, 112, 112) + ( (80, -48, -80) * 2.0 )
= (112 + 160, 112 – 96, 112 – 160)
= (255, 16, 0)
= Color’s are exaggerated by 200% and
will are clamped to their max/min values.
Well, I hope you guys find this info useful, feel free to get back to me anytime (Michael.Vittiglio@ReclusiveDesigns.com) though I have to admit that my time is limited due to my tasks regarding the “Lost Marbles” project.
Be well,
~Mike();