-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLinearColor.cs
More file actions
221 lines (187 loc) · 6.76 KB
/
Copy pathLinearColor.cs
File metadata and controls
221 lines (187 loc) · 6.76 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
namespace WindEditor
{
/// <summary>
/// RGBA Color that uses 32-bit floats to represent each component.
/// </summary>
[Serializable] [StructLayout(LayoutKind.Sequential)]
public struct WLinearColor
{
/// <summary> Red component of the <see cref="WLinearColor"/>. </summary>
public float R;
/// <summary> Green component of the <see cref="WLinearColor"/>. </summary>
public float G;
/// <summary> Blue component of the <see cref="WLinearColor"/>. </summary>
public float B;
/// <summary> Alpha component of the <see cref="WLinearColor"/>. Defaults to 1f. </summary>
public float A;
/// <summary>
/// Construct new <see cref="WLinearColor"/> with the given R, G, B, and A components.
/// </summary>
/// <param name="r">Red Component.</param>
/// <param name="g">Green Component.</param>
/// <param name="b">Blue Component.</param>
/// <param name="a">Alpha Component.</param>
public WLinearColor(float r, float g, float b, float a)
{
R = r;
G = g;
B = b;
A = a;
}
/// <summary>
/// Construct a <see cref="WLinearColor"/> with the given R, G, B, and an implicit A component set to 1f.
/// </summary>
/// <param name="r">Red Component.</param>
/// <param name="g">Green Component.</param>
/// <param name="b">Blue Component.</param>
public WLinearColor(float r, float g, float b)
{
R = r;
G = g;
B = b;
A = 1f;
}
public float this[int index]
{
get
{
switch (index)
{
case 0: return R;
case 1: return G;
case 2: return B;
case 3: return A;
default: throw new ArgumentOutOfRangeException("index", "Invalid WLinearColor Index!");
}
}
set
{
switch (index)
{
case 0: R = value; break;
case 1: G = value; break;
case 2: B = value; break;
case 3: A = value; break;
default: throw new ArgumentOutOfRangeException("index", "Invalid WLinearColor Index!");
}
}
}
public static bool operator ==(WLinearColor lhs, WLinearColor rhs)
{
return lhs.R == rhs.R && lhs.G == rhs.G && lhs.B == rhs.B && lhs.A == rhs.A;
}
public static bool operator !=(WLinearColor lhs, WLinearColor rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
if (!(obj is WLinearColor))
return false;
WLinearColor other = (WLinearColor)obj;
return this == other;
}
public override int GetHashCode()
{
return ((byte)(R * 255) << 24 | (byte)(R * 255) << 16 | (byte)(G * 255) << 8 | (byte)(A * 255));
}
public static WLinearColor FromHexString(string hexString)
{
if (string.IsNullOrEmpty(hexString))
throw new ArgumentException("Empty/Null hex string!", "hexString");
if (hexString.StartsWith("0x"))
hexString = hexString.Substring(2);
uint WLinearColorVal;
bool bSuccess = uint.TryParse(hexString, NumberStyles.HexNumber, CultureInfo.CurrentCulture, out WLinearColorVal);
if (!bSuccess)
throw new ArgumentException("Not a valid hex number!", "hexString");
WLinearColor outputWLinearColor = new WLinearColor();
byte[] bytes = BitConverter.GetBytes(WLinearColorVal); // This is in ABGR
for (int i = 0; i < 4; i++)
outputWLinearColor[3-i] = bytes[i] / 255f;
// This code is untested, comment this out once you've tested it.
return outputWLinearColor;
}
public override string ToString()
{
return string.Format("R: {0} G: {1} B: {2} A: {3}", (byte)(R*255f), (byte)(G*255f), (byte)(B*255f), (byte)(A*255f));
}
public string ToHexString()
{
StringBuilder sb = new StringBuilder(10);
sb.Append("0x");
sb.Append(R.ToString("X2"));
sb.Append(G.ToString("X2"));
sb.Append(B.ToString("X2"));
sb.Append(A.ToString("X2"));
return sb.ToString();
}
public static WLinearColor Lerp(WLinearColor a, WLinearColor b, float t)
{
t = WMath.Clamp(t, 0, 1);
return new WLinearColor((1 - t) * a.R + t * b.R, (1 - t) * a.G + t * b.G, (1 - t) * a.B + t * b.B, (1 - t) * a.A + t * b.A);
}
#region Static Preset WLinearColors
public static WLinearColor White
{
get { return new WLinearColor(1f, 1f, 1f, 1f); }
}
public static WLinearColor Black
{
get { return new WLinearColor(0f, 0f, 0f, 1f); }
}
public static WLinearColor Red
{
get { return new WLinearColor(1f, 0f, 0f, 1f); }
}
public static WLinearColor Green
{
get { return new WLinearColor(0f, 1f, 0f, 1f); }
}
public static WLinearColor Blue
{
get { return new WLinearColor(0f, 0f, 1f, 1f); }
}
public static WLinearColor Orange
{
get { return new WLinearColor(1f, 0.647f, 0f, 1f); }
}
public static WLinearColor Coral
{
get { return new WLinearColor(1f, 0.5f, 0.314f, 1f); }
}
public static WLinearColor Yellow
{
get { return new WLinearColor(1f, 1f, 0f, 1f); }
}
public static WLinearColor Purple
{
get { return new WLinearColor(0.542f, 0.169f, 0.886f, 1f); }
}
public static WLinearColor Seagreen
{
get { return new WLinearColor(0.18f, 0.545f, 341f, 1f); }
}
public static WLinearColor Pink
{
get { return new WLinearColor(1f, 0.412f, 0.71f, 1f); }
}
public static WLinearColor DarkGrey
{
get { return new WLinearColor(0.4f, 0.4f, 0.4f, 1f); }
}
public static WLinearColor Grey
{
get { return new WLinearColor(0.7f, 0.7f, 0.7f, 1f); }
}
public static WLinearColor Transparent
{
get { return new WLinearColor(0f, 0f, 0f, 0f); }
}
#endregion
}
}