1
// ==========================================================
2
// FreeImage 3 .NET wrapper
3
// Original FreeImage 3 functions and .NET compatible derived functions
5
// Design and implementation by
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
7
// - Carsten Klein (cklein05@users.sourceforge.net)
10
// - David Boland (davidboland@vodafone.ie)
12
// Main reference : MSDN Knowlede Base
14
// This file is part of FreeImage 3
16
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
17
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
18
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
19
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
20
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
21
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
22
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
23
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
26
// Use at your own risk!
27
// ==========================================================
29
// ==========================================================
32
// $Date: 2009/02/20 07:41:08 $
33
// $Id: FI16RGB555.cs,v 1.3 2009/02/20 07:41:08 cklein05 Exp $
34
// ==========================================================
38
using System.Runtime.InteropServices;
40
namespace FreeImageAPI
43
/// The <b>FI16RGB555</b> structure describes a color consisting of relative
44
/// intensities of red, green, blue and alpha value. Each single color
45
/// component consumes 5 bits and so, takes values in the range from 0 to 31.
48
/// <para>For easy integration of the underlying structure into the .NET framework,
49
/// the <b>FI16RGB555</b> structure implements implicit conversion operators to
50
/// convert the represented color to and from the <see cref="System.Drawing.Color"/>
51
/// type. This makes the <see cref="System.Drawing.Color"/> type a real replacement
52
/// for the <b>FI16RGB555</b> structure and my be used in all situations which require
53
/// an <b>FI16RGB555</b> type.
57
/// The following code example demonstrates the various conversions between the
58
/// <b>FI16RGB555</b> structure and the <see cref="System.Drawing.Color"/> structure.
60
/// FI16RGB555 fi16rgb;
61
/// // Initialize the structure using a native .NET Color structure.
62
/// fi16rgb = new FI16RGB555(Color.Indigo);
63
/// // Initialize the structure using the implicit operator.
64
/// fi16rgb = Color.DarkSeaGreen;
65
/// // Convert the FI16RGB555 instance into a native .NET Color
66
/// // using its implicit operator.
67
/// Color color = fi16rgb;
68
/// // Using the structure's Color property for converting it
69
/// // into a native .NET Color.
70
/// Color another = fi16rgb.Color;
73
[Serializable, StructLayout(LayoutKind.Sequential)]
74
public struct FI16RGB555 : IComparable, IComparable<FI16RGB555>, IEquatable<FI16RGB555>
77
/// The value of the color.
82
/// Initializes a new instance based on the specified <see cref="System.Drawing.Color"/>.
84
/// <param name="color"><see cref="System.Drawing.Color"/> to initialize with.</param>
85
public FI16RGB555(Color color)
88
(((color.R * 31) / 255) << FreeImage.FI16_555_RED_SHIFT) +
89
(((color.G * 31) / 255) << FreeImage.FI16_555_GREEN_SHIFT) +
90
(((color.B * 31) / 255) << FreeImage.FI16_555_BLUE_SHIFT));
94
/// Tests whether two specified <see cref="FI16RGB555"/> structures are equivalent.
96
/// <param name="left">The <see cref="FI16RGB555"/> that is to the left of the equality operator.</param>
97
/// <param name="right">The <see cref="FI16RGB555"/> that is to the right of the equality operator.</param>
99
/// <b>true</b> if the two <see cref="FI16RGB555"/> structures are equal; otherwise, <b>false</b>.
101
public static bool operator ==(FI16RGB555 left, FI16RGB555 right)
103
return (left.value == right.value);
107
/// Tests whether two specified <see cref="FI16RGB555"/> structures are different.
109
/// <param name="left">The <see cref="FI16RGB555"/> that is to the left of the inequality operator.</param>
110
/// <param name="right">The <see cref="FI16RGB555"/> that is to the right of the inequality operator.</param>
112
/// <b>true</b> if the two <see cref="FI16RGB555"/> structures are different; otherwise, <b>false</b>.
114
public static bool operator !=(FI16RGB555 left, FI16RGB555 right)
116
return (!(left == right));
120
/// Converts the value of a <see cref="System.Drawing.Color"/> structure to a <see cref="FI16RGB555"/> structure.
122
/// <param name="value">A <see cref="System.Drawing.Color"/> structure.</param>
123
/// <returns>A new instance of <see cref="FI16RGB555"/> initialized to <paramref name="value"/>.</returns>
124
public static implicit operator FI16RGB555(Color value)
126
return new FI16RGB555(value);
130
/// Converts the value of a <see cref="FI16RGB555"/> structure to a <see cref="System.Drawing.Color"/> structure.
132
/// <param name="value">A <see cref="FI16RGB555"/> structure.</param>
133
/// <returns>A new instance of <see cref="System.Drawing.Color"/> initialized to <paramref name="value"/>.</returns>
134
public static implicit operator Color(FI16RGB555 value)
140
/// Gets or sets the <see cref="System.Drawing.Color"/> of the structure.
146
return Color.FromArgb(
147
((value & FreeImage.FI16_555_RED_MASK) >> FreeImage.FI16_555_RED_SHIFT) * 255 / 31,
148
((value & FreeImage.FI16_555_GREEN_MASK) >> FreeImage.FI16_555_GREEN_SHIFT) * 255 / 31,
149
((value & FreeImage.FI16_555_BLUE_MASK) >> FreeImage.FI16_555_BLUE_SHIFT) * 255 / 31);
153
this.value = (ushort)(
154
(((value.R * 31) / 255) << FreeImage.FI16_555_RED_SHIFT) +
155
(((value.G * 31) / 255) << FreeImage.FI16_555_GREEN_SHIFT) +
156
(((value.B * 31) / 255) << FreeImage.FI16_555_BLUE_SHIFT));
161
/// Gets or sets the red color component.
167
return (byte)(((value & FreeImage.FI16_555_RED_MASK) >> FreeImage.FI16_555_RED_SHIFT) * 255 / 31);
171
this.value = (ushort)((this.value & (~FreeImage.FI16_555_RED_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_RED_SHIFT));
176
/// Gets or sets the green color component.
182
return (byte)(((value & FreeImage.FI16_555_GREEN_MASK) >> FreeImage.FI16_555_GREEN_SHIFT) * 255 / 31);
186
this.value = (ushort)((this.value & (~FreeImage.FI16_555_GREEN_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_GREEN_SHIFT));
191
/// Gets or sets the blue color component.
197
return (byte)(((value & FreeImage.FI16_555_BLUE_MASK) >> FreeImage.FI16_555_BLUE_SHIFT) * 255 / 31);
201
this.value = (ushort)((this.value & (~FreeImage.FI16_555_BLUE_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_BLUE_SHIFT));
206
/// Compares this instance with a specified <see cref="Object"/>.
208
/// <param name="obj">An object to compare with this instance.</param>
209
/// <returns>A 32-bit signed integer indicating the lexical relationship between the two comparands.</returns>
210
/// <exception cref="ArgumentException"><paramref name="obj"/> is not a <see cref="FI16RGB555"/>.</exception>
211
public int CompareTo(object obj)
217
if (!(obj is FI16RGB555))
219
throw new ArgumentException("obj");
221
return CompareTo((FI16RGB555)obj);
225
/// Compares this instance with a specified <see cref="FI16RGB555"/> object.
227
/// <param name="other">A <see cref="FI16RGB555"/> to compare.</param>
228
/// <returns>A signed number indicating the relative values of this instance
229
/// and <paramref name="other"/>.</returns>
230
public int CompareTo(FI16RGB555 other)
232
return this.Color.ToArgb().CompareTo(other.Color.ToArgb());
236
/// Tests whether the specified object is a <see cref="FI16RGB555"/> structure
237
/// and is equivalent to this <see cref="FI16RGB555"/> structure.
239
/// <param name="obj">The object to test.</param>
240
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FI16RGB555"/> structure
241
/// equivalent to this <see cref="FI16RGB555"/> structure; otherwise, <b>false</b>.</returns>
242
public override bool Equals(object obj)
244
return base.Equals(obj);
248
/// Tests whether the specified <see cref="FI16RGB555"/> structure is equivalent to this <see cref="FI16RGB555"/> structure.
250
/// <param name="other">A <see cref="FI16RGB555"/> structure to compare to this instance.</param>
251
/// <returns><b>true</b> if <paramref name="obj"/> is a <see cref="FI16RGB555"/> structure
252
/// equivalent to this <see cref="FI16RGB555"/> structure; otherwise, <b>false</b>.</returns>
253
public bool Equals(FI16RGB555 other)
255
return (this == other);
259
/// Returns a hash code for this <see cref="FI16RGB555"/> structure.
261
/// <returns>An integer value that specifies the hash code for this <see cref="FI16RGB555"/>.</returns>
262
public override int GetHashCode()
264
return base.GetHashCode();
268
/// Converts the numeric value of the <see cref="FI16RGB555"/> object
269
/// to its equivalent string representation.
271
/// <returns>The string representation of the value of this instance.</returns>
272
public override string ToString()
274
return FreeImage.ColorToString(Color);
b'\\ No newline at end of file'