FlatRedBall.Graphics.IColorable

Introduction

The IColorable interface defines properties for interacting with an object which can have its color dynamically modified and which can specify how it “blends” with objects behind it. Common objects which implement the IColorable interface include the Sprite and Text object.

Applying ColorOperations

ColorOperations can be used to change the color of a IColorable at runtime. ColorOperations require two pieces of information: the color specified by three values (Red, Green, and Blue) and the operation to perform with the specified color.

The default ColorOperation for most IColorables (like Sprites) is ColorOperation.None. Unless the ColorOperation is changed, the Red, Green, and Blue properties will have no effect on way the IColorable is rendered. The default ColorOperation for Texts is Modulate.

The Red, Green, and Blue properties provide access to the components of the color which is used to change the IColorable’s color. The ColorOperation property specifies how to modify the color.

Color values range between 0 and 1 and are all set to 0 by default.

The exception is the Text object. The Text object has its Red, Green, and Blue color values set to 1 so that the text appears as white. Furthermore, the ColorOperation defaults to ColorOperation.Modulate for the Text object.

The following code will create five Sprites, each with a different ColorOperation.

ColorOperations.png

For more information on specific ColorOperation values, see the ColorOperation page.

Color Rates

Color rates are values which change the Red, Green, and Blue values over time. The rates, just like all velocity values, indicate units changed per second. The following code creates 80 Sprites and randomly varies their colors using a SpriteCustomBehavior.

SpritesWithColorRates.png

Alpha and BlendOperation

The Alpha and BlendOperation properties are related just as the components of color and ColorOperation properties. The Alpha property is used to control the transparency of an IColorable.

Unlike the color components, modifying the Alpha value will affect the appearance of an IColorable with its default BlendOperation.

BlendOperations control how IColorables blend with objects behind them. The following code shows two rows of Sprites – the first with the default BlendOperation.Regular and the second with BlendOperation.Add;

AlphaAndBlendOperation.png

AlphaRate

The AlphaRate property modifies the Alpha property similar to the ColorRate properties. AlphaRate can be used to fade an IColorable in and out.

Most IColorables are created with an Alpha value of 1. This generally represents full opacity. AlphaRate can change this value over time. A positive AlphaRate value will increase the Alpha property over time, and a negative AlphaRate value will decrease the Alpha property over time. The formula (which is generally applied automatically for managed objects) is:

Therefore, a fully opaque object with an AlphaRate of -1 will disappear in 1 second. Increasing the absolute value of the AlphaRate makes the object disappear faster. That is, an AlphaRate of -2 (twice as large in absolute value terms) will make an object disappear after .5 seconds (half as long). The formula is:

Therefore, we can see that an AlphaRate value of -2 will cause an object to disappear in

IColorable Members

Did this article leave any questions unanswered? Post any question in our forums for a rapid response.