fna-workbench

fna-workbench Git Source Tree


Root/src/Graphics/PackedVector/Rgba64.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
#endregion
 
namespace Microsoft.Xna.Framework.Graphics.PackedVector
{
    /// <summary>
    /// Packed vector type containing unsigned normalized values ranging from 0 to 1.
    /// The x and z components use 5 bits, and the y component uses 6 bits.
    /// </summary>
    public struct Rgba64 : IPackedVector<ulong>, IEquatable<Rgba64>, IPackedVector
    {
        #region Public Properties
 
        /// <summary>
        /// Gets and sets the packed value.
        /// </summary>
        [CLSCompliant(false)]
        public ulong PackedValue
        {
            get
            {
                return packedValue;
            }
            set
            {
                packedValue = value;
            }
        }
 
        #endregion
 
        #region Private Variables
 
        private ulong packedValue;
 
        #endregion
 
        #region Public Constructors
 
        /// <summary>
        /// Creates a new instance of Rgba64.
        /// </summary>
        /// <param name="x">The x component</param>
        /// <param name="y">The y component</param>
        /// <param name="z">The z component</param>
        /// <param name="w">The w component</param>
        public Rgba64(float x, float y, float z, float w)
        {
            packedValue = Pack(x, y, z, w);
        }
 
        /// <summary>
        /// Creates a new instance of Rgba64.
        /// </summary>
        /// <param name="vector">
        /// Vector containing the components for the packed vector.
        /// </param>
        public Rgba64(Vector4 vector)
        {
            packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
        }
 
        #endregion
 
        #region Public Methods
 
        /// <summary>
        /// Gets the packed vector in Vector4 format.
        /// </summary>
        /// <returns>The packed vector in Vector4 format</returns>
        public Vector4 ToVector4()
        {
            return new Vector4(
                (float) (((packedValue >> 48) & 0xFFFF) / 65535.0f),
                (float) (((packedValue >> 32) & 0xFFFF) / 65535.0f),
                (float) (((packedValue >> 16) & 0xFFFF) / 65535.0f),
                (float) ((packedValue & 0xFFFF) / 65535.0f)
            );
        }
 
        #endregion
 
        #region IPackedVector Methods
 
        /// <summary>
        /// Sets the packed vector from a Vector4.
        /// </summary>
        /// <param name="vector">Vector containing the components.</param>
        void IPackedVector.PackFromVector4(Vector4 vector)
        {
            packedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
        }
 
        #endregion
 
        #region Public Static Operators and Override Methods
 
        /// <summary>
        /// Compares an object with the packed vector.
        /// </summary>
        /// <param name="obj">The object to compare.</param>
        /// <returns>True if the object is equal to the packed vector.</returns>
        public override bool Equals(object obj)
        {
            return (obj is Rgba64) && Equals((Rgba64) obj);
        }
 
        /// <summary>
        /// Compares another Rgba64 packed vector with the packed vector.
        /// </summary>
        /// <param name="other">The Rgba64 packed vector to compare.</param>
        /// <returns>True if the packed vectors are equal.</returns>
        public bool Equals(Rgba64 other)
        {
            return packedValue == other.packedValue;
        }
 
        /// <summary>
        /// Gets a string representation of the packed vector.
        /// </summary>
        /// <returns>A string representation of the packed vector.</returns>
        public override string ToString()
        {
            return ToVector4().ToString();
        }
 
        /// <summary>
        /// Gets a hash code of the packed vector.
        /// </summary>
        /// <returns>The hash code for the packed vector.</returns>
        public override int GetHashCode()
        {
            return packedValue.GetHashCode();
        }
 
        public static bool operator ==(Rgba64 lhs, Rgba64 rhs)
        {
            return lhs.packedValue == rhs.packedValue;
        }
 
        public static bool operator !=(Rgba64 lhs, Rgba64 rhs)
        {
            return lhs.packedValue != rhs.packedValue;
        }
 
        #endregion
 
        #region Private Static Pack Method
 
        private static ulong Pack(float x, float y, float z, float w)
        {
            return (ulong) (
                (((long) (MathHelper.Clamp(x, 0, 1) * 65535.0f) & 0xFFFF) << 48) |
                (((long) (MathHelper.Clamp(y, 0, 1) * 65535.0f) & 0xFFFF) << 32) |
                (((long) (MathHelper.Clamp(z, 0, 1) * 65535.0f) & 0xFFFF) << 16) |
                ((long) (MathHelper.Clamp(w, 0, 1) * 65535.0f) & 0xFFFF)
            );
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.20978s using 11 queries.