/
ColorMatrix.cs
413 lines (371 loc) · 13 KB
/
ColorMatrix.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif
namespace System.Drawing.Imaging
{
/// <summary>
/// Defines a 5 x 5 matrix that contains the homogeneous coordinates for the RGBA space.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public sealed class ColorMatrix
{
private float _matrix00;
private float _matrix01;
private float _matrix02;
private float _matrix03;
private float _matrix04;
private float _matrix10;
private float _matrix11;
private float _matrix12;
private float _matrix13;
private float _matrix14;
private float _matrix20;
private float _matrix21;
private float _matrix22;
private float _matrix23;
private float _matrix24;
private float _matrix30;
private float _matrix31;
private float _matrix32;
private float _matrix33;
private float _matrix34;
private float _matrix40;
private float _matrix41;
private float _matrix42;
private float _matrix43;
private float _matrix44;
/// <summary>
/// Initializes a new instance of the <see cref='ColorMatrix'/> class.
/// </summary>
public ColorMatrix()
{
/*
* Setup identity matrix by default
*/
_matrix00 = 1.0f;
//matrix01 = 0.0f;
//matrix02 = 0.0f;
//matrix03 = 0.0f;
//matrix04 = 0.0f;
//matrix10 = 0.0f;
_matrix11 = 1.0f;
//matrix12 = 0.0f;
//matrix13 = 0.0f;
//matrix14 = 0.0f;
//matrix20 = 0.0f;
//matrix21 = 0.0f;
_matrix22 = 1.0f;
// matrix23 = 0.0f;
// matrix24 = 0.0f;
// matrix30 = 0.0f;
//matrix31 = 0.0f;
// matrix32 = 0.0f;
_matrix33 = 1.0f;
// matrix34 = 0.0f;
// matrix40 = 0.0f;
// matrix41 = 0.0f;
// matrix42 = 0.0f;
// matrix43 = 0.0f;
_matrix44 = 1.0f;
}
/// <summary>
/// Represents the element at the 0th row and 0th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix00
{
get { return _matrix00; }
set { _matrix00 = value; }
}
/// <summary>
/// Represents the element at the 0th row and 1st column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix01
{
get { return _matrix01; }
set { _matrix01 = value; }
}
/// <summary>
/// Represents the element at the 0th row and 2nd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix02
{
get { return _matrix02; }
set { _matrix02 = value; }
}
/// <summary>
/// Represents the element at the 0th row and 3rd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix03
{
get { return _matrix03; }
set { _matrix03 = value; }
}
/// <summary>
/// Represents the element at the 0th row and 4th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix04
{
get { return _matrix04; }
set { _matrix04 = value; }
}
/// <summary>
/// Represents the element at the 1st row and 0th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix10
{
get { return _matrix10; }
set { _matrix10 = value; }
}
/// <summary>
/// Represents the element at the 1st row and 1st column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix11
{
get { return _matrix11; }
set { _matrix11 = value; }
}
/// <summary>
/// Represents the element at the 1st row and 2nd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix12
{
get { return _matrix12; }
set { _matrix12 = value; }
}
/// <summary>
/// Represents the element at the 1st row and 3rd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix13
{
get { return _matrix13; }
set { _matrix13 = value; }
}
/// <summary>
/// Represents the element at the 1st row and 4th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix14
{
get { return _matrix14; }
set { _matrix14 = value; }
}
/// <summary>
/// Represents the element at the 2nd row and 0th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix20
{
get { return _matrix20; }
set { _matrix20 = value; }
}
/// <summary>
/// Represents the element at the 2nd row and 1st column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix21
{
get { return _matrix21; }
set { _matrix21 = value; }
}
/// <summary>
/// Represents the element at the 2nd row and 2nd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix22
{
get { return _matrix22; }
set { _matrix22 = value; }
}
/// <summary>
/// Represents the element at the 2nd row and 3rd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix23
{
get { return _matrix23; }
set { _matrix23 = value; }
}
/// <summary>
/// Represents the element at the 2nd row and 4th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix24
{
get { return _matrix24; }
set { _matrix24 = value; }
}
/// <summary>
/// Represents the element at the 3rd row and 0th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix30
{
get { return _matrix30; }
set { _matrix30 = value; }
}
/// <summary>
/// Represents the element at the 3rd row and 1st column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix31
{
get { return _matrix31; }
set { _matrix31 = value; }
}
/// <summary>
/// Represents the element at the 3rd row and 2nd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix32
{
get { return _matrix32; }
set { _matrix32 = value; }
}
/// <summary>
/// Represents the element at the 3rd row and 3rd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix33
{
get { return _matrix33; }
set { _matrix33 = value; }
}
/// <summary>
/// Represents the element at the 3rd row and 4th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix34
{
get { return _matrix34; }
set { _matrix34 = value; }
}
/// <summary>
/// Represents the element at the 4th row and 0th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix40
{
get { return _matrix40; }
set { _matrix40 = value; }
}
/// <summary>
/// Represents the element at the 4th row and 1st column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix41
{
get { return _matrix41; }
set { _matrix41 = value; }
}
/// <summary>
/// Represents the element at the 4th row and 2nd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix42
{
get { return _matrix42; }
set { _matrix42 = value; }
}
/// <summary>
/// Represents the element at the 4th row and 3rd column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix43
{
get { return _matrix43; }
set { _matrix43 = value; }
}
/// <summary>
/// Represents the element at the 4th row and 4th column of this <see cref='ColorMatrix'/>.
/// </summary>
public float Matrix44
{
get { return _matrix44; }
set { _matrix44 = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref='ColorMatrix'/> class with the elements in the specified matrix.
/// </summary>
[CLSCompliant(false)]
public ColorMatrix(float[][] newColorMatrix)
{
SetMatrix(newColorMatrix);
}
internal void SetMatrix(float[][] newColorMatrix)
{
_matrix00 = newColorMatrix[0][0];
_matrix01 = newColorMatrix[0][1];
_matrix02 = newColorMatrix[0][2];
_matrix03 = newColorMatrix[0][3];
_matrix04 = newColorMatrix[0][4];
_matrix10 = newColorMatrix[1][0];
_matrix11 = newColorMatrix[1][1];
_matrix12 = newColorMatrix[1][2];
_matrix13 = newColorMatrix[1][3];
_matrix14 = newColorMatrix[1][4];
_matrix20 = newColorMatrix[2][0];
_matrix21 = newColorMatrix[2][1];
_matrix22 = newColorMatrix[2][2];
_matrix23 = newColorMatrix[2][3];
_matrix24 = newColorMatrix[2][4];
_matrix30 = newColorMatrix[3][0];
_matrix31 = newColorMatrix[3][1];
_matrix32 = newColorMatrix[3][2];
_matrix33 = newColorMatrix[3][3];
_matrix34 = newColorMatrix[3][4];
_matrix40 = newColorMatrix[4][0];
_matrix41 = newColorMatrix[4][1];
_matrix42 = newColorMatrix[4][2];
_matrix43 = newColorMatrix[4][3];
_matrix44 = newColorMatrix[4][4];
}
internal float[][] GetMatrix()
{
float[][] returnMatrix = new float[5][];
for (int i = 0; i < 5; i++)
returnMatrix[i] = new float[5];
returnMatrix[0][0] = _matrix00;
returnMatrix[0][1] = _matrix01;
returnMatrix[0][2] = _matrix02;
returnMatrix[0][3] = _matrix03;
returnMatrix[0][4] = _matrix04;
returnMatrix[1][0] = _matrix10;
returnMatrix[1][1] = _matrix11;
returnMatrix[1][2] = _matrix12;
returnMatrix[1][3] = _matrix13;
returnMatrix[1][4] = _matrix14;
returnMatrix[2][0] = _matrix20;
returnMatrix[2][1] = _matrix21;
returnMatrix[2][2] = _matrix22;
returnMatrix[2][3] = _matrix23;
returnMatrix[2][4] = _matrix24;
returnMatrix[3][0] = _matrix30;
returnMatrix[3][1] = _matrix31;
returnMatrix[3][2] = _matrix32;
returnMatrix[3][3] = _matrix33;
returnMatrix[3][4] = _matrix34;
returnMatrix[4][0] = _matrix40;
returnMatrix[4][1] = _matrix41;
returnMatrix[4][2] = _matrix42;
returnMatrix[4][3] = _matrix43;
returnMatrix[4][4] = _matrix44;
return returnMatrix;
}
/// <summary>
/// Gets or sets the value of the specified element of this <see cref='ColorMatrix'/>.
/// </summary>
public float this[int row, int column]
{
get
{
return GetMatrix()[row][column];
}
set
{
float[][] tempMatrix = GetMatrix();
tempMatrix[row][column] = value;
SetMatrix(tempMatrix);
}
}
internal ref float GetPinnableReference() => ref _matrix00;
#if NET7_0_OR_GREATER
[CustomMarshaller(typeof(ColorMatrix), MarshalMode.ManagedToUnmanagedIn, typeof(PinningMarshaller))]
internal static unsafe class PinningMarshaller
{
public static ref float GetPinnableReference(ColorMatrix managed) => ref (managed is null ? ref Unsafe.NullRef<float>() : ref managed.GetPinnableReference());
// All usages in our currently supported scenarios will always go through GetPinnableReference
public static float* ConvertToUnmanaged(ColorMatrix managed) => throw new UnreachableException();
}
#endif
}
}