0
\$\begingroup\$

With the Microsoft XNA Framework I can change the color of a tinted texture by changing it's integer values like so:

Int x = 255;
if(true) { x--; tint = new Color(x, 255, 255); }
Draw(texture, Vector2.Zero, tint);

If x were a float however the appearance of the sprite wouldn't change until the value x became zero. With integer the texture fades over time with the event change.

Why is this? :S

\$\endgroup\$
2
  • \$\begingroup\$ because color constructor either takes 3 floats ,or 3 bytes. It doesn't work for both. \$\endgroup\$ Commented Dec 16, 2013 at 0:15
  • \$\begingroup\$ The reason is that the constructor which takes floats assumes that the floats are within [0, 1] range. So basically instead of giving floats a value between [0, 255], give them a value between [0, 1]. \$\endgroup\$ Commented Dec 16, 2013 at 1:17

1 Answer 1

4
\$\begingroup\$

It's because of how overload resolution works in C#. Of the available constructors with three arguments, there are:

Color(int, int, int)
Color(float, float, float)

If you pass all integers, you get the integer version. But if you pass a float for any of the parameters, you get the float version (and the integers will be implicitly converted to floats).

The integer version takes values in the range of 0 to 255. The float version takes values in the range of 0.0f to 1.0f for each channel (and converts them internally to cover the integer range).

(See also this blog post)

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.