#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
namespace OpenTK.Graphics
{
///
/// Represents a color with 4 floating-point components (R, G, B, A).
///
[Serializable]
public struct Color4 : IEquatable
{
#region Fields
///
/// The red component of this Color4 structure.
///
public float R;
///
/// The green component of this Color4 structure.
///
public float G;
///
/// The blue component of this Color4 structure.
///
public float B;
///
/// The alpha component of this Color4 structure.
///
public float A;
#endregion
#region Constructors
///
/// Constructs a new Color4 structure from the specified components.
///
/// The red component of the new Color4 structure.
/// The green component of the new Color4 structure.
/// The blue component of the new Color4 structure.
/// The alpha component of the new Color4 structure.
public Color4(float r, float g, float b, float a)
{
R = r;
G = g;
B = b;
A = a;
}
///
/// Constructs a new Color4 structure from the specified System.Drawing.Color.
///
/// The System.Drawing.Color containing the component values.
public Color4(Color color)
{
R = color.R / (float)Byte.MaxValue;
G = color.G / (float)Byte.MaxValue;
B = color.B / (float)Byte.MaxValue;
A = color.A / (float)Byte.MaxValue;
}
#endregion
#region Operators
///
/// Compares the specified Color4 structures for equality.
///
/// The left-hand side of the comparison.
/// The right-hand side of the comparison.
/// True if left is equal to right; false otherwise.
public static bool operator ==(Color4 left, Color4 right)
{
return left.Equals(right);
}
///
/// Compares the specified Color4 structures for inequality.
///
/// The left-hand side of the comparison.
/// The right-hand side of the comparison.
/// True if left is not equal to right; false otherwise.
public static bool operator !=(Color4 left, Color4 right)
{
return !left.Equals(right);
}
///
/// Converts the specified System.Drawing.Color to a Color4 structure.
///
/// The System.Drawing.Color to convert.
/// A new Color4 structure containing the converted components.
public static implicit operator Color4(Color color)
{
return new Color4(color);
}
///
/// Converts the specified Color4 to a System.Drawing.Color structure.
///
/// The Color4 to convert.
/// A new System.Drawing.Color structure containing the converted components.
public static explicit operator Color(Color4 color)
{
return Color.FromArgb(
(int)(color.A * Byte.MaxValue),
(int)(color.R * Byte.MaxValue),
(int)(color.G * Byte.MaxValue),
(int)(color.B * Byte.MaxValue));
}
#endregion
#region IEquatable Members
///
/// Compares whether this Color4 structure is equal to the specified Color4.
///
/// The Color4 structure to compare to.
/// True if both Color4 structures contain the same components; false otherwise.
public bool Equals(Color4 other)
{
return
this.R == other.R &&
this.G == other.G &&
this.B == other.B &&
this.A == other.A;
}
#endregion
#region Overrides
///
/// Compares whether this Color4 structure is equal to the specified object.
///
/// An object to compare to.
/// True obj is a Color4 structure with the same components as this Color4; false otherwise.
public override bool Equals(object obj)
{
if (!(obj is Color4))
return false;
return Equals((Color4)obj);
}
///
/// Calculates the hash code for this Color4 structure.
///
/// A System.Int32 containing the hashcode of this Color4 structure.
public override int GetHashCode()
{
return ((Color)this).GetHashCode();
}
///
/// Creates a System.String that describes this Color4 structure.
///
/// A System.String that describes this Color4 structure.
public override string ToString()
{
return String.Format("{{R, G, B, A = {0}, {1}, {2}, {3}}}", R.ToString(), G.ToString(), B.ToString(), A.ToString());
}
#endregion
}
}