In this article I will show you how to use Colors in XNA.
Colors are very complex structures which will be defined simply in this article but know that it is.Allright lets start.
Lets have a look at the structure of a color:
a new Instance of a Color:
a new Vector3 based Color:
a new Vector4 based Color:
a new 3-float based Color:
a new 3-int based Color:
a new 4-float based Color:
a new 4-int based Color:
Setting the Color for clearing the screen:
FromNonPremultiplied converts a non-premultipled alpha color to a color that contains premultiplied alpha.Why do we need premultiplied alpha? As Shawn Hargreaves explained in his blog: It is a superset of both conventional and additive blending. If you set alpha to zero while RGB is non zero, you get an additive blend. This can be handy for particle systems that want to smoothly transition from additive glowing sparks to dark pieces of soot as the particles age.And It plays nice with DXT Compression which only supports transparent pixels with an RGB of zero.Which means all the things about FromNonPremultiplied is : Transparency.While in traditional Alpha Blending RGB and alpha are independent in Premultiplied Alpha these two are linked.So its a new feature added on XNA 4.0.Lets see how they seem in code:
1st way to use it is to set it as a Vector4-based Color.
The other way is to set 4 integer values:
Color.Lerp linearly interpolates between two colors.Which means you can mix them in your code using 2 Colors and an amount of value:
Color.Multiply helps you multiply a color with a float-based scale variable:
Lets create a sample to use them all:
Add these codes in your Draw function and use them in order.
Color cl1 = new Color();
Color cl2 = new Color(new Vector3(100.0f,
150.0f, 50.0f)); //Vector3
Color cl3 = new Color(new Vector4(100.0f,
150.0f, 50.0f, 125.0f)); //Vector4
Color cl4 = new Color(0.0f,
15.0f, 50.0f); //3-float based
Color cl5 = new Color(10, 50,
25); //3-integer
based
Color cl6 = new Color(0.0f,
15.0f, 50.0f, 100.0f); //4-float based
Color cl7 = new Color(10, 20,
30, 40); //4-integer based
Color cl8 = Color.FromNonPremultiplied(new Vector4(100.0f, 0.0f,
15.0f, 100.0f)); //Vector4-based
Color cl9 = Color.FromNonPremultiplied(10, 20, 30, 100); //int based
Color cl10 = Color.Lerp(Color.Red,
Color.Black, 50.0f); //Lerp Function
Color cl11 = Color.Multiply(Color.Pink,
5.0f); //Multiply
Function
For example we have used:
GraphicsDevice.Clear(Color.FromNonPremultiplied(new Vector4(100.0f, 0.0f,
15.0f, 100.0f)));
And get an output like that:
Colors are not a very easy subject to write and there are much more complex topics such as Color Mapping(Image Processing),Lights and Chromatics-The Science of Color
I will be writing much more about the Colors which is a very important stuff for Graphics Programming.