#region LGPL License
/*************************************************************************
Crazy Eddie's GUI System (http://crayzedsgui.sourceforge.net)
Copyright (C)2004 Paul D Turner (crayzed@users.sourceforge.net)
C# Port developed by Chris McGuirk (leedgitar@latenitegames.com)
Compatible with the Axiom 3D Engine (http://axiomengine.sf.net)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*************************************************************************/
#endregion LGPL License
using System;
using System.Globalization;
namespace CrayzEdsGui.Base{
/// <summary>
/// Handy structure for use in dealing with colors internally.
/// </summary>
public struct Color {
#region Fields
/// <summary>
/// Alpha component [0.0, 1.0];
/// </summary>
public float a;
/// <summary>
/// Red component [0.0, 1.0];
/// </summary>
public float r;
/// <summary>
/// Green component [0.0, 1.0];
/// </summary>
public float g;
/// <summary>
/// Blue component [0.0, 1.0];
/// </summary>
public float b;
#endregion Fields
#region Constructors
/// <summary>
/// Constructor.
/// </summary>
/// <remarks>Alpha defaults to 1.0f.</remarks>
/// <param name="r">Red component.</param>
/// <param name="g">Green component.</param>
/// <param name="b">Blue component.</param>
public Color(float r, float g, float b)
: this(1.0f, r, g, b) {}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="a">Alpha component.</param>
/// <param name="r">Red component.</param>
/// <param name="g">Green component.</param>
/// <param name="b">Blue component.</param>
public Color(float a, float r, float g, float b) {
this.a = a;
this.r = r;
this.g = g;
this.b = b;
}
/// <summary>
/// Constructor for creating a color from a hex (HTML) value.
/// </summary>
/// <param name="hexValue">Color value in the format 0xAARRGGBB.</param>
public Color(uint hexValue) {
a = ((hexValue & 0xFF000000) >> 24) / 255.0f;
r = ((hexValue & 0x00FF0000) >> 16) / 255.0f;
g = ((hexValue & 0x0000FF00) >> 8) / 255.0f;
b = (hexValue & 0x000000FF) / 255.0f;
}
#endregion Constructors
#region Methods
/// <summary>
/// Returns this color as a 32-bit 0xAARRGGBB integer.
/// </summary>
/// <returns>A 32-bit 0xAARRGGBB representation of this color object.</returns>
public int ToARGB() {
int result = 0;
result += ((int)(a * 255.0f)) << 24;
result += ((int)(r * 255.0f)) << 16;
result += ((int)(g * 255.0f)) << 8;
result += ((int)(b * 255.0f));
return result;
}
/// <summary>
/// Returns a string representation of the color in 0xAARRGGBB format.
/// </summary>
/// <returns>A string 0xAARRGGBB representation of this color object.</returns>
public override string ToString()
{
return string.Format( "{0:x}", ToARGB() );
}
/// <summary>
/// Parses the string representation of a color and returns the corresponding
/// Color object.
/// </summary>
/// <param name="data">String representation of the Color "AARRGGBB".</param>
/// <returns>A Color object with the correspoding color value.</returns>
public static Color Parse( string data )
{
return new Color( uint.Parse( data, NumberStyles.HexNumber ) );
}
#endregion Methods
#region Operators
public override bool Equals(object obj) {
if(!(obj is Color))
return false;
return ((Color)obj) == this;
}
public override int GetHashCode() {
return a.GetHashCode() ^ r.GetHashCode() ^ g.GetHashCode() ^ b.GetHashCode();
}
/// <summary>
/// Compare 2 colors.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator == (Color left, Color right) {
// TODO: Float tolerance
return left.a == right.a &&
left.r == right.r &&
left.g == right.g &&
left.b == right.b;
}
/// <summary>
/// Compare 2 colors.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator != (Color left, Color right) {
// TODO: Float tolerance
return left.a != right.a ||
left.r != right.r ||
left.g != right.g ||
left.b != right.b;
}
#endregion Operators
}
}
|