-
Notifications
You must be signed in to change notification settings - Fork 1
/
AcuityEngine.Modulators.cs
324 lines (265 loc) · 10.4 KB
/
AcuityEngine.Modulators.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
/*
* MetaphysicsIndustries.Acuity
* Copyright (C) 2009-2021 Metaphysics Industries, Inc., Richard Sartor
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
*/
/*****************************************************************************
* *
* AcuityEngine.cs *
* *
* Methods for doing general calculations. *
* *
*****************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using MetaphysicsIndustries.Solus;
namespace MetaphysicsIndustries.Acuity
{
public partial class AcuityEngine
{
public static float Convert24gToFloat(float value)
{
value = Math.Max(0, Math.Min(0xFFFFFF, value));
float r = ((int)(value) & 0x000000FF) / 255.0f;
float g = (((int)(value) & 0x0000FF00) >> 8) / 255.0f;
float b = (((int)(value) & 0x00FF0000) >> 16) / 255.0f;
return (r + g + b) / 3;
}
public static float ConvertFloatTo24g(float value)
{
value = Math.Max(0, Math.Min(1, value));
int b = (int)(value * 255) & 0xFF;
int g = b << 8;
int r = g << 8;
return r | g | b;
}
public static float Convert24cTo24g(float value)
{
return ConvertFloatTo24g(Convert24gToFloat(value));
}
public static float ConvertRgbTo24cTriModulator(float r, float g, float b)
{
r = Math.Max(0, Math.Min(1, r));
g = Math.Max(0, Math.Min(1, g));
b = Math.Max(0, Math.Min(1, b));
int rr = ((int)(r * 255) & 0xFF)<<16;
int gg = ((int)(g * 255) & 0xFF)<<8;
int bb = (int)(b * 255) & 0xFF;
return rr | gg | bb;
}
public class MultiplyModulator
{
public MultiplyModulator(float factor) { _factor = factor; }
private float _factor;
public float Modulate(float value) { return value * _factor; }
}
public class AdditionModulator
{
public AdditionModulator(float factor) { _factor = factor; }
private float _factor;
public float Modulate(float value) { return value + _factor; }
}
public class MaximumModulator
{
public MaximumModulator(float factor) { _factor = factor; }
private float _factor;
public float Modulate(float value) { return Math.Max(value, _factor); }
}
public class MinimumModulator
{
public MinimumModulator(float factor) { _factor = factor; }
private float _factor;
public float Modulate(float value) { return Math.Min(value, _factor); }
}
public static float AdditionBiMod(float x, float y)
{
return x + y;
}
public static float MultiplicationBiMod(float x, float y)
{
return x * y;
}
public static float ConvertNegOneOneToZeroOne(float x)
{
//convert a number on the interval of [-1,1] to [0,1]
return (x + 1) / 2;
}
public static float ConvertZeroOneToNegOneOne(float x)
{
//convert a number on the interval of [0,1] to [-1,1]
return x * 2 - 1;
}
public static STuple<float, float> ConvertNegOneOneToZeroOne(float x, float y)
{
return new STuple<float, float>(
ConvertNegOneOneToZeroOne(x),
ConvertNegOneOneToZeroOne(y));
}
public static STuple<float, float> ConvertZeroOneToNegOneOne(float x, float y)
{
return new STuple<float, float>(
ConvertZeroOneToNegOneOne(x),
ConvertZeroOneToNegOneOne(y));
}
public static STuple<float, float> ConvertEuclideanToPolar(float x, float y)
{
STuple<float, float> pair = new STuple<float, float>();
pair.Value1 = (float)Math.Sqrt(x * x + y * y);
pair.Value2 = (float)Math.Atan2(y, x);
return pair;
}
public static STuple<float, float> ConvertPolarToEuclidean(float r, float theta)
{
STuple<float, float> pair = new STuple<float, float>();
pair.Value1 = (float)(r * Math.Cos(theta));
pair.Value2 = (float)(r * Math.Sin(theta));
return pair;
}
public static float ConvertDegreesToRadians(float degrees)
{
return (float)(Math.PI * degrees / 180.0);
}
public static float ConvertRadiansToDegrees(float radians)
{
return (float)(180.0 * radians / Math.PI);
}
public static float ComplexMagnitude(float real, float imaginary)
{
return (float)Math.Sqrt(real * real + imaginary * imaginary);
}
public static float ComplexPhase(float real, float imaginary)
{
return (float)Math.Atan2(imaginary, real);
}
public static float IntervalFit(float value, float min, float max)
{
return (value - min) / (max - min);
}
public static float MeanSquareError(Matrix a, Matrix b)
{
if (a == null) { throw new ArgumentNullException("a"); }
if (b == null) { throw new ArgumentNullException("b"); }
if (a.RowCount != b.RowCount ||
a.ColumnCount != b.ColumnCount)
{
throw new ArgumentException("Matrix sizes do not match", "matrixToCompare");
}
int i;
int j;
float sum = 0;
float v;
for (i = 0; i < a.RowCount; i++)
{
for (j = 0; j < a.ColumnCount; j++)
{
v = a[i, j] - b[i, j];
sum += v * v;
}
}
sum /= a.RowCount;
sum /= a.ColumnCount;
return sum;
}
public static float MaxError(Matrix a, Matrix b)
{
if (a == null) { throw new ArgumentNullException("a"); }
if (b == null) { throw new ArgumentNullException("b"); }
if (a.RowCount != b.RowCount ||
a.ColumnCount != b.ColumnCount)
{
throw new ArgumentException("Matrix sizes do not match", "matrixToCompare");
}
int i;
int j;
float max = 0;
for (i = 0; i < a.RowCount; i++)
{
for (j = 0; j < a.ColumnCount; j++)
{
max = Math.Max(Math.Abs(a[i, j] - b[i, j]), max);
}
}
return max;
}
public static STuple<float, float, float> ConvertRgbToHsl(STuple<float, float, float> rgb)
{
float r = rgb.Value1;
float g = rgb.Value2;
float b = rgb.Value3;
float h;
float s;
float l;
ConvertRgbToHsl(r, g, b, out h, out s, out l);
return new STuple<float, float, float>(h, s, l);
}
public static void ConvertRgbToHsl(float r, float g, float b, out float h, out float s, out float l)
{
float max = Math.Max(r, Math.Max(g, b));
float min = Math.Min(r, Math.Min(g, b));
if (max == min) { h = 0; }
else if (max == r)
{
h = (g - b) / 6.0f;
if (g < b) { h += 1; }
}
else if (max == g) { h = (b - r + 2) / 6.0f; }
else { h = (r - g + 4) / 6.0f; }
l = (max + min) / 2;
if (max == min) { s = 0; }
else if (l <= 0.5f) { s = (max - min) / (2 * l); }
else { s = (max - min) / (2 - 2 * l); }
}
public static STuple<float, float, float> ConvertHslToRgb(STuple<float, float, float> hsl)
{
float h = hsl.Value1;
float s = hsl.Value2;
float l = hsl.Value3;
float r;
float g;
float b;
ConvertHslToRgb(h, s, l, out r, out g, out b);
return new STuple<float, float, float>(r, g, b);
}
public static void ConvertHslToRgb(float h, float s, float l, out float r, out float g, out float b)
{
float q;
float p;
float tr = h + 1 / 3.0f;
float tg = h;
float tb = h - 1 / 3.0f;
if (tr > 1) { tr -= 1; }
if (tb < 1) { tb += 1; }
if (l < 0.5) { q = l * (1 + s); }
else { q = l + s - l * s; }
p = 2 * l - q;
r = CalcHslToRgbConversion(q, p, tr);
g = CalcHslToRgbConversion(q, p, tg);
b = CalcHslToRgbConversion(q, p, tb);
}
public static float CalcHslToRgbConversion(float q, float p, float t)
{
float c;
if (t < 1 / 6.0f) { c = q + ((q - p) * 6 * t); }
else if (t < 0.5f) { c = q; }
else if (t < 2 / 3.0f) { c = p + ((q - p) * 6 * ((2 / 3.0f) - t)); }
else { c = p; }
return c;
}
}
}