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.

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.

 // To reduce code, add the following #using statement to your Game class
 using FlatRedBall.Graphics;

 // Replace your Initialize method with the following:
 protected override void Initialize()
 {
     FlatRedBallServices.InitializeFlatRedBall(this, this.graphics);

     float xPosition = -3;
     float spacing = 2;

     Sprite regularSprite = SpriteManager.AddSprite("redball.bmp");
     regularSprite.X = xPosition;
     // uses default ColorOperation:
     // FlatRedBall.Graphics.ColorOperation.Texture

     xPosition += spacing;

     Sprite blueAdded = SpriteManager.AddSprite("redball.bmp");
     blueAdded.X = xPosition;
     blueAdded.Blue = 1; // between 0 and 1
     blueAdded.ColorOperation = ColorOperation.Add;

     xPosition += spacing;

     Sprite plainColor = SpriteManager.AddSprite("redball.bmp");
     plainColor.X = xPosition;
     plainColor.Green = .7f;
     plainColor.Red = .8f;
     // using the ColorOperation.Color rather than
     // ColorOperation.ColorTextureAlpha will make the
     // entire Sprite the specified color even where normally
     // transparent.
     plainColor.ColorOperation = ColorOperation.ColorTextureAlpha;

     xPosition += spacing;
     Sprite inverseColor = SpriteManager.AddSprite("redball.bmp");
     inverseColor.X = xPosition;
     inverseColor.ColorOperation = ColorOperation.InverseTexture;


     base.Initialize();
 }

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.

 // To reduce code, add the following #using statement to your Game class
 using FlatRedBall.Graphics;

 // Replace your Initialize method with the following:

 protected override void Initialize()
 {
     FlatRedBallServices.InitializeFlatRedBall(this, this.graphics);

     // Might as well use it since we have one created
     // for us already.
     Random random = FlatRedBallServices.Random;

     for (int i = 0; i < 80; i++)
     {
        Sprite sprite = SpriteManager.AddSprite("redball.bmp");
        sprite.ColorOperation = ColorOperation.Add;

        sprite.X = (float)random.NextDouble() * 30 - 15;
        sprite.Y = (float)random.NextDouble() * 20 - 10;

        sprite.RedRate = (float)random.NextDouble() * .7f;
        sprite.GreenRate = (float)random.NextDouble() * .7f;
        sprite.BlueRate = (float)random.NextDouble() * .7f;

        sprite.CustomBehavior += FluctuateColor;
     }
     base.Initialize();
 }

 // Add the following method at class scope
 void FluctuateColor(Sprite sprite)