~cosme/ubuntu/precise/freeimage/freeimage-3.15.1

« back to all changes in this revision

Viewing changes to Wrapper/FreeImage.NET/cs/Library/Structs/FI16RGB555.cs

  • Committer: Stefano Rivera
  • Date: 2010-07-24 15:35:51 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: stefanor@ubuntu.com-20100724153551-6s3fth1653huk31a
Tags: upstream-3.13.1
ImportĀ upstreamĀ versionĀ 3.31.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ==========================================================
 
2
// FreeImage 3 .NET wrapper
 
3
// Original FreeImage 3 functions and .NET compatible derived functions
 
4
//
 
5
// Design and implementation by
 
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
 
7
// - Carsten Klein (cklein05@users.sourceforge.net)
 
8
//
 
9
// Contributors:
 
10
// - David Boland (davidboland@vodafone.ie)
 
11
//
 
12
// Main reference : MSDN Knowlede Base
 
13
//
 
14
// This file is part of FreeImage 3
 
15
//
 
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
 
24
// THIS DISCLAIMER.
 
25
//
 
26
// Use at your own risk!
 
27
// ==========================================================
 
28
 
 
29
// ==========================================================
 
30
// CVS
 
31
// $Revision: 1.3 $
 
32
// $Date: 2009/02/20 07:41:08 $
 
33
// $Id: FI16RGB555.cs,v 1.3 2009/02/20 07:41:08 cklein05 Exp $
 
34
// ==========================================================
 
35
 
 
36
using System;
 
37
using System.Drawing;
 
38
using System.Runtime.InteropServices;
 
39
 
 
40
namespace FreeImageAPI
 
41
{
 
42
        /// <summary>
 
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.
 
46
        /// </summary>
 
47
        /// <remarks>
 
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.
 
54
        /// </para>
 
55
        /// </remarks>
 
56
        /// <example>
 
57
        /// The following code example demonstrates the various conversions between the
 
58
        /// <b>FI16RGB555</b> structure and the <see cref="System.Drawing.Color"/> structure.
 
59
        /// <code>
 
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;
 
71
        /// </code>
 
72
        /// </example>
 
73
        [Serializable, StructLayout(LayoutKind.Sequential)]
 
74
        public struct FI16RGB555 : IComparable, IComparable<FI16RGB555>, IEquatable<FI16RGB555>
 
75
        {
 
76
                /// <summary>
 
77
                /// The value of the color.
 
78
                /// </summary>
 
79
                private ushort value;
 
80
 
 
81
                /// <summary>
 
82
                /// Initializes a new instance based on the specified <see cref="System.Drawing.Color"/>.
 
83
                /// </summary>
 
84
                /// <param name="color"><see cref="System.Drawing.Color"/> to initialize with.</param>
 
85
                public FI16RGB555(Color color)
 
86
                {
 
87
                        value = (ushort)(
 
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));
 
91
                }
 
92
 
 
93
                /// <summary>
 
94
                /// Tests whether two specified <see cref="FI16RGB555"/> structures are equivalent.
 
95
                /// </summary>
 
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>
 
98
                /// <returns>
 
99
                /// <b>true</b> if the two <see cref="FI16RGB555"/> structures are equal; otherwise, <b>false</b>.
 
100
                /// </returns>
 
101
                public static bool operator ==(FI16RGB555 left, FI16RGB555 right)
 
102
                {
 
103
                        return (left.value == right.value);
 
104
                }
 
105
 
 
106
                /// <summary>
 
107
                /// Tests whether two specified <see cref="FI16RGB555"/> structures are different.
 
108
                /// </summary>
 
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>
 
111
                /// <returns>
 
112
                /// <b>true</b> if the two <see cref="FI16RGB555"/> structures are different; otherwise, <b>false</b>.
 
113
                /// </returns>
 
114
                public static bool operator !=(FI16RGB555 left, FI16RGB555 right)
 
115
                {
 
116
                        return (!(left == right));
 
117
                }
 
118
 
 
119
                /// <summary>
 
120
                /// Converts the value of a <see cref="System.Drawing.Color"/> structure to a <see cref="FI16RGB555"/> structure.
 
121
                /// </summary>
 
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)
 
125
                {
 
126
                        return new FI16RGB555(value);
 
127
                }
 
128
 
 
129
                /// <summary>
 
130
                /// Converts the value of a <see cref="FI16RGB555"/> structure to a <see cref="System.Drawing.Color"/> structure.
 
131
                /// </summary>
 
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)
 
135
                {
 
136
                        return value.Color;
 
137
                }
 
138
 
 
139
                /// <summary>
 
140
                /// Gets or sets the <see cref="System.Drawing.Color"/> of the structure.
 
141
                /// </summary>
 
142
                public Color Color
 
143
                {
 
144
                        get
 
145
                        {
 
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);
 
150
                        }
 
151
                        set
 
152
                        {
 
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));
 
157
                        }
 
158
                }
 
159
 
 
160
                /// <summary>
 
161
                /// Gets or sets the red color component.
 
162
                /// </summary>
 
163
                public byte Red
 
164
                {
 
165
                        get
 
166
                        {
 
167
                                return (byte)(((value & FreeImage.FI16_555_RED_MASK) >> FreeImage.FI16_555_RED_SHIFT) * 255 / 31);
 
168
                        }
 
169
                        set
 
170
                        {
 
171
                                this.value = (ushort)((this.value & (~FreeImage.FI16_555_RED_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_RED_SHIFT));
 
172
                        }
 
173
                }
 
174
 
 
175
                /// <summary>
 
176
                /// Gets or sets the green color component.
 
177
                /// </summary>
 
178
                public byte Green
 
179
                {
 
180
                        get
 
181
                        {
 
182
                                return (byte)(((value & FreeImage.FI16_555_GREEN_MASK) >> FreeImage.FI16_555_GREEN_SHIFT) * 255 / 31);
 
183
                        }
 
184
                        set
 
185
                        {
 
186
                                this.value = (ushort)((this.value & (~FreeImage.FI16_555_GREEN_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_GREEN_SHIFT));
 
187
                        }
 
188
                }
 
189
 
 
190
                /// <summary>
 
191
                /// Gets or sets the blue color component.
 
192
                /// </summary>
 
193
                public byte Blue
 
194
                {
 
195
                        get
 
196
                        {
 
197
                                return (byte)(((value & FreeImage.FI16_555_BLUE_MASK) >> FreeImage.FI16_555_BLUE_SHIFT) * 255 / 31);
 
198
                        }
 
199
                        set
 
200
                        {
 
201
                                this.value = (ushort)((this.value & (~FreeImage.FI16_555_BLUE_MASK)) | (((value * 31) / 255) << FreeImage.FI16_555_BLUE_SHIFT));
 
202
                        }
 
203
                }
 
204
 
 
205
                /// <summary>
 
206
                /// Compares this instance with a specified <see cref="Object"/>.
 
207
                /// </summary>
 
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)
 
212
                {
 
213
                        if (obj == null)
 
214
                        {
 
215
                                return 1;
 
216
                        }
 
217
                        if (!(obj is FI16RGB555))
 
218
                        {
 
219
                                throw new ArgumentException("obj");
 
220
                        }
 
221
                        return CompareTo((FI16RGB555)obj);
 
222
                }
 
223
 
 
224
                /// <summary>
 
225
                /// Compares this instance with a specified <see cref="FI16RGB555"/> object.
 
226
                /// </summary>
 
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)
 
231
                {
 
232
                        return this.Color.ToArgb().CompareTo(other.Color.ToArgb());
 
233
                }
 
234
 
 
235
                /// <summary>
 
236
                /// Tests whether the specified object is a <see cref="FI16RGB555"/> structure
 
237
                /// and is equivalent to this <see cref="FI16RGB555"/> structure.
 
238
                /// </summary>
 
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)
 
243
                {
 
244
                        return base.Equals(obj);
 
245
                }
 
246
 
 
247
                /// <summary>
 
248
                /// Tests whether the specified <see cref="FI16RGB555"/> structure is equivalent to this <see cref="FI16RGB555"/> structure.
 
249
                /// </summary>
 
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)
 
254
                {
 
255
                        return (this == other);
 
256
                }
 
257
 
 
258
                /// <summary>
 
259
                /// Returns a hash code for this <see cref="FI16RGB555"/> structure.
 
260
                /// </summary>
 
261
                /// <returns>An integer value that specifies the hash code for this <see cref="FI16RGB555"/>.</returns>
 
262
                public override int GetHashCode()
 
263
                {
 
264
                        return base.GetHashCode();
 
265
                }
 
266
 
 
267
                /// <summary>
 
268
                /// Converts the numeric value of the <see cref="FI16RGB555"/> object
 
269
                /// to its equivalent string representation.
 
270
                /// </summary>
 
271
                /// <returns>The string representation of the value of this instance.</returns>
 
272
                public override string ToString()
 
273
                {
 
274
                        return FreeImage.ColorToString(Color);
 
275
                }
 
276
        }
 
277
}
 
 
b'\\ No newline at end of file'