2
\$\begingroup\$

I'm trying to make a shader that changes the color of a sprite the way the XNA spriteBatch.draw() color parameter changes color. I don't know exactly what it is called (the closest word I can think of is 'tinting' but that's not right I don't think) and it's kind of hard to explain the way it looks.

Say you set the parameter to 'Red' -- white pixels in the original texture would become completely red, while black pixels would remain black. Grey pixels would look dark red, blue pixels might look purple, green pixels might look brownish.

If you set the parameter to 'Black' the texture would become completely black.

Does anyone know the math behind this, or what this technique is called? It's probably very simple and I'm just not seeing it.

Thanks!

\$\endgroup\$
4
\$\begingroup\$

It's probably multiplying the colours. That's pretty typical for a parameter like this.

Each colour channel is interpreted as a value from 0 to 1, then multiplied with the corresponding colour channel of the tint colour. So:

_______________ times red (1, 0, 0)  =
white (1, 1, 1)                      = red (1, 0, 0)
grey (0.5, 0.5, 0.5)                 = dark red (0.5, 0, 0)
black (0, 0, 0)                      = black (0, 0, 0)

Under this scheme, pure blue (0, 0, 1) tinted with pure red (1, 0, 0) will turn black. But if you use intermediate blue & red colours (ones with some non-zero values in the other two channels) you may get a dark purple.

Here's an example of using "multiply" blending to tint colours, via this tutorial Multiplication tinting example

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.