// Project: XnaGraphicEngine, File: ColorHelper.cs
// Namespace: XnaGraphicEngine.Helpers, Class: ColorHelper
// Path: C:\code\XnaGraphicEngine\Helpers, Author: Abi
// Code lines: 182, Size of file: 5,41 KB
// Creation date: 02.10.2006 01:33
// Last modified: 27.10.2006 02:40
// Generated with Commenter by abi.exDream.com
#region Using directives
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Text;
#endregion
namespace XnaGraphicEngine.Helpers
{
///
/// Color helper, just to convert colors to different formats and providing
/// more helper methods missing in the Color class.
///
public class ColorHelper
{
#region Constants
///
/// Empty color, used to mark unused color values.
///
public static readonly Color Empty = new Color(0, 0, 0, 0);
///
/// Half alpha color helper. Just white with 50% alpha.
///
public static readonly Color
HalfAlpha = new Color(255, 255, 255, 128);
#endregion
#region Convert Color to Vector4
///
/// Convert Color to Vector4
///
/// Color
/// Return Vector4
public static Vector4 ConvertColorToVector4(Color color)
{
return new Vector4(
color.R / 255.0f,
color.G / 255.0f,
color.B / 255.0f,
color.A / 255.0f);
} // ConvertColorToVector4(color)
#endregion
#region Stay in range helper
///
/// Stay in range, val will be set to min if less or to max when bigger.
///
private static float StayInRange(float val, float min, float max)
{
if (val < min)
return min;
if (val > max)
return max;
return val;
} // StayInRange(val, min, max)
#endregion
#region Multiply colors
///
/// Multiply colors
///
/// Color 1
/// Color 2
/// Return color
public static Color MultiplyColors(Color color1, Color color2)
{
// Quick check if any of the colors is white,
// multiplying won't do anything then.
if (color1 == Color.White)
return color2;
if (color2 == Color.White)
return color1;
// Get values from color1
float redValue1 = color1.R / 255.0f;
float greenValue1 = color1.G / 255.0f;
float blueValue1 = color1.B / 255.0f;
float alphaValue1 = color1.A / 255.0f;
// Get values from color2
float redValue2 = color2.R / 255.0f;
float greenValue2 = color2.G / 255.0f;
float blueValue2 = color2.B / 255.0f;
float alphaValue2 = color2.A / 255.0f;
// Multiply everything using our floats
return new Color(
(byte)(StayInRange(redValue1 * redValue2, 0, 1) * 255.0f),
(byte)(StayInRange(greenValue1 * greenValue2, 0, 1) * 255.0f),
(byte)(StayInRange(blueValue1 * blueValue2, 0, 1) * 255.0f),
(byte)(StayInRange(alphaValue1 * alphaValue2, 0, 1) * 255.0f));
} // MultiplyColors(color1, color2)
#endregion
#region Same color check
///
/// Same color. Helper method for LoadLevel because for some reason
/// the color compare does not work and causes a lot of errors.
///
/// Color
/// Check color
/// Bool
public static bool SameColor(Color color, Color checkColor)
{
return color.R == checkColor.R &&
color.G == checkColor.G &&
color.B == checkColor.B;
} // SameColor(color, checkColor)
#endregion
#region Interpolate color
///
/// Interpolate color. Used to fade the hud colors from green to red.
///
public static Color InterpolateColor(Color col1, Color col2, float percent)
{
return new Color(
(byte)((float)col1.R * (1.0f - percent) + (float)col2.R * percent),
(byte)((float)col1.G * (1.0f - percent) + (float)col2.G * percent),
(byte)((float)col1.B * (1.0f - percent) + (float)col2.B * percent),
(byte)((float)col1.A * (1.0f - percent) + (float)col2.A * percent));
} // InterpolateColor(col1, col2, percent)
#endregion
#region ApplyAlphaToColor
///
/// Apply alpha to color
///
/// Color
/// New alpha
/// Color
public static Color ApplyAlphaToColor(Color col, float newAlpha)
{
if (newAlpha < 0)
newAlpha = 0;
if (newAlpha > 1)
newAlpha = 1;
return new Color(
(byte)(col.R),
(byte)(col.G),
(byte)(col.B),
(byte)(newAlpha * 255.0f));
} // ApplyAlphaToColor(col, newAlpha)
///
/// Mix alpha to color
///
/// Color
/// New alpha
/// Color
public static Color MixAlphaToColor(Color col, float newAlpha)
{
if (newAlpha < 0)
newAlpha = 0;
if (newAlpha > 1)
newAlpha = 1;
return new Color(
(byte)(col.R * newAlpha),
(byte)(col.G * newAlpha),
(byte)(col.B * newAlpha),
(byte)(newAlpha * 255.0f));
} // MixAlphaToColor(col, newAlpha)
#endregion
#region FromArgb
///
/// Helper method to support MDX style Color.FromArgb methods.
///
/// Red
/// Green
/// Blue
/// Color
public static Color FromArgb(byte r, byte g, byte b)
{
return new Color(r, g, b);
} // FromArgb(r, g, b)
///
/// Helper method to support MDX style Color.FromArgb methods.
///
/// Red
/// Green
/// Blue
/// Color
public static Color FromArgb(byte a, byte r, byte g, byte b)
{
return new Color(r, g, b, a);
} // FromArgb(r, g, b)
///
/// Helper method to support MDX style Color.FromArgb methods.
///
/// Red
/// Green
/// Blue
/// Color
public static Color FromArgb(byte a, Color col)
{
return new Color(col.R, col.G, col.B, a);
} // FromArgb(r, g, b)
///
/// From argb
///
/// Packed argb color
/// Color
public static Color FromArgb(uint packedArgbColor)
{
return new Color(
(byte)(packedArgbColor << 24),
(byte)(packedArgbColor << 16),
(byte)(packedArgbColor << 8),
(byte)(packedArgbColor));
} // FromArgb(packedArgbColor)
#endregion
} // class ColorHelper
} // namespace XnaGraphicEngine.Helpers