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\$
  • \$\begingroup\$ because color constructor either takes 3 floats ,or 3 bytes. It doesn't work for both. \$\endgroup\$ – concept3d Dec 16 '13 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\$ – user9790 Dec 16 '13 at 1:17
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\$

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.