// ========================================================== // FreeImage 3 .NET wrapper // Original FreeImage 3 functions and .NET compatible derived functions // // Design and implementation by // - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net) // - Carsten Klein (cklein05@users.sourceforge.net) // // Contributors: // - David Boland (davidboland@vodafone.ie) // // Main reference : MSDN Knowlede Base // // This file is part of FreeImage 3 // // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER // THIS DISCLAIMER. // // Use at your own risk! // ========================================================== // ========================================================== // CVS // $Revision: 1.4 $ // $Date: 2009/02/20 07:40:53 $ // $Id: RGBQUAD.cs,v 1.4 2009/02/20 07:40:53 cklein05 Exp $ // ========================================================== using System; using System.Drawing; using System.Runtime.InteropServices; namespace FreeImageAPI { /// /// The RGBQUAD structure describes a color consisting of relative /// intensities of red, green, blue and alpha value. Each single color /// component consumes 8 bits and so, takes values in the range from 0 to 255. /// /// /// /// The RGBQUAD structure provides access to an underlying Win32 RGBQUAD /// structure. To determine the alpha, red, green or blue component of a color, /// use the rgbReserved, rgbRed, rgbGreen or rgbBlue fields, respectively. /// /// For easy integration of the underlying structure into the .NET framework, /// the RGBQUAD structure implements implicit conversion operators to /// convert the represented color to and from the /// type. This makes the type a real replacement /// for the RGBQUAD structure and my be used in all situations which require /// an RGBQUAD type. /// /// /// Each color component rgbReserved, rgbRed, rgbGreen or rgbBlue of RGBQUAD /// is translated into it's corresponding color component A, R, G or B of /// by an one-to-one manner and vice versa. /// /// /// Conversion from System.Drawing.Color to RGBQUAD /// /// RGBQUAD.component = Color.component /// /// Conversion from RGBQUAD to System.Drawing.Color /// /// Color.component = RGBQUAD.component /// /// The same conversion is also applied when the /// property or the constructor /// is invoked. /// /// /// /// The following code example demonstrates the various conversions between the /// RGBQUAD structure and the structure. /// /// RGBQUAD rgbq; /// // Initialize the structure using a native .NET Color structure. /// rgbq = new RGBQUAD(Color.Indigo); /// // Initialize the structure using the implicit operator. /// rgbq = Color.DarkSeaGreen; /// // Convert the RGBQUAD instance into a native .NET Color /// // using its implicit operator. /// Color color = rgbq; /// // Using the structure's Color property for converting it /// // into a native .NET Color. /// Color another = rgbq.Color; /// /// [Serializable, StructLayout(LayoutKind.Explicit)] public struct RGBQUAD : IComparable, IComparable, IEquatable { /// /// The blue color component. /// [FieldOffset(0)] public byte rgbBlue; /// /// The green color component. /// [FieldOffset(1)] public byte rgbGreen; /// /// The red color component. /// [FieldOffset(2)] public byte rgbRed; /// /// The alpha color component. /// [FieldOffset(3)] public byte rgbReserved; /// /// The color's value. /// [FieldOffset(0)] public uint uintValue; /// /// Initializes a new instance based on the specified . /// /// to initialize with. public RGBQUAD(Color color) { uintValue = 0u; rgbBlue = color.B; rgbGreen = color.G; rgbRed = color.R; rgbReserved = color.A; } /// /// Tests whether two specified structures are equivalent. /// /// The that is to the left of the equality operator. /// The that is to the right of the equality operator. /// /// true if the two structures are equal; otherwise, false. /// public static bool operator ==(RGBQUAD left, RGBQUAD right) { return (left.uintValue == right.uintValue); } /// /// Tests whether two specified structures are different. /// /// The that is to the left of the inequality operator. /// The that is to the right of the inequality operator. /// /// true if the two structures are different; otherwise, false. /// public static bool operator !=(RGBQUAD left, RGBQUAD right) { return (left.uintValue != right.uintValue); } /// /// Converts the value of a structure to a structure. /// /// A structure. /// A new instance of initialized to . public static implicit operator RGBQUAD(Color value) { return new RGBQUAD(value); } /// /// Converts the value of a structure to a Color structure. /// /// A structure. /// A new instance of initialized to . public static implicit operator Color(RGBQUAD value) { return value.Color; } /// /// Converts the value of an structure to a structure. /// /// An structure. /// A new instance of initialized to . public static implicit operator RGBQUAD(uint value) { RGBQUAD result = new RGBQUAD(); result.uintValue = value; return result; } /// /// Converts the value of a structure to an structure. /// /// A structure. /// A new instance of initialized to . public static implicit operator uint(RGBQUAD value) { return value.uintValue; } /// /// Gets or sets the of the structure. /// public Color Color { get { return Color.FromArgb( rgbReserved, rgbRed, rgbGreen, rgbBlue); } set { rgbRed = value.R; rgbGreen = value.G; rgbBlue = value.B; rgbReserved = value.A; } } /// /// Converts an array of into an array of /// . /// /// The array to convert. /// An array of . public static RGBQUAD[] ToRGBQUAD(Color[] array) { if (array == null) return null; RGBQUAD[] result = new RGBQUAD[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = array[i]; } return result; } /// /// Converts an array of into an array of /// . /// /// The array to convert. /// An array of . public static Color[] ToColor(RGBQUAD[] array) { if (array == null) return null; Color[] result = new Color[array.Length]; for (int i = 0; i < array.Length; i++) { result[i] = array[i].Color; } return result; } /// /// Compares this instance with a specified . /// /// An object to compare with this instance. /// A 32-bit signed integer indicating the lexical relationship between the two comparands. /// is not a . public int CompareTo(object obj) { if (obj == null) { return 1; } if (!(obj is RGBQUAD)) { throw new ArgumentException("obj"); } return CompareTo((RGBQUAD)obj); } /// /// Compares this instance with a specified object. /// /// A to compare. /// A signed number indicating the relative values of this instance /// and . public int CompareTo(RGBQUAD other) { return this.Color.ToArgb().CompareTo(other.Color.ToArgb()); } /// /// Tests whether the specified object is a structure /// and is equivalent to this structure. /// /// The object to test. /// true if is a structure /// equivalent to this structure; otherwise, false. public override bool Equals(object obj) { return ((obj is RGBQUAD) && (this == ((RGBQUAD)obj))); } /// /// Tests whether the specified structure is equivalent to this structure. /// /// A structure to compare to this instance. /// true if is a structure /// equivalent to this structure; otherwise, false. public bool Equals(RGBQUAD other) { return (this == other); } /// /// Returns a hash code for this structure. /// /// An integer value that specifies the hash code for this . public override int GetHashCode() { return base.GetHashCode(); } /// /// Converts the numeric value of the object /// to its equivalent string representation. /// /// The string representation of the value of this instance. public override string ToString() { return FreeImage.ColorToString(Color); } } }