Falco Engine 3.9.0.1 (beta)
Color.cs
Go to the documentation of this file.
1using System;
2
3namespace FalcoEngine
4{
5 public struct Color
6 {
7 public float r;
8 public float g;
9 public float b;
10 public float a;
11
15 public static Color red => new Color(1f, 0f, 0f, 1f);
16
20 public static Color green => new Color(0f, 1f, 0f, 1f);
21
25 public static Color blue => new Color(0f, 0f, 1f, 1f);
26
30 public static Color white => new Color(1f, 1f, 1f, 1f);
31
35 public static Color black => new Color(0f, 0f, 0f, 1f);
36
40 public static Color yellow => new Color(1f, 0.921568632f, 0.0156862754f, 1f);
41
45 public static Color cyan => new Color(0f, 1f, 1f, 1f);
46
50 public static Color magenta => new Color(1f, 0f, 1f, 1f);
51
55 public static Color gray => new Color(0.5f, 0.5f, 0.5f, 1f);
56
60 public static Color grey => new Color(0.5f, 0.5f, 0.5f, 1f);
61
65 public static Color clear => new Color(0f, 0f, 0f, 0f);
66
70 public static Color aqua => new Color(0.0f, 1.0f, 1.0f, 1.0f);
71
75 public static Color cream => new Color(1.0f, 0.98f, 0.94f, 1.0f);
76
80 public static Color fuchsia => new Color(1.0f, 0.0f, 1.0f, 1.0f);
81
85 public static Color darkGreen => new Color(0.0f, 0.5f, 0.0f, 1.0f);
86
90 public static Color maroon => new Color(0.5f, 0.0f, 0.0f, 1.0f);
91
95 public static Color darkBlue => new Color(0.0f, 0.0f, 0.5f, 1.0f);
96
100 public static Color darkRed => new Color(0.5f, 0.0f, 0.0f, 1.0f);
101
105 public static Color olive => new Color(0.5f, 0.5f, 0.0f, 1.0f);
106
110 public static Color silver => new Color(0.75f, 0.75f, 0.75f, 1.0f);
111
115 public static Color teal => new Color(0.0f, 0.5f, 0.5f, 1.0f);
116
120 public float grayscale => 0.299f * r + 0.587f * g + 0.114f * b;
121
122 public float this[int index]
123 {
124 get
125 {
126 switch (index)
127 {
128 case 0:
129 return r;
130 case 1:
131 return g;
132 case 2:
133 return b;
134 case 3:
135 return a;
136 default:
137 throw new IndexOutOfRangeException("Invalid Color index(" + index + ")!");
138 }
139 }
140 set
141 {
142 switch (index)
143 {
144 case 0:
145 r = value;
146 break;
147 case 1:
148 g = value;
149 break;
150 case 2:
151 b = value;
152 break;
153 case 3:
154 a = value;
155 break;
156 default:
157 throw new IndexOutOfRangeException("Invalid Color index(" + index + ")!");
158 }
159 }
160 }
161
169 public Color(float r, float g, float b, float a)
170 {
171 this.r = r;
172 this.g = g;
173 this.b = b;
174 this.a = a;
175 }
176
183 public Color(float r, float g, float b)
184 {
185 this.r = r;
186 this.g = g;
187 this.b = b;
188 a = 1f;
189 }
190
195 public override string ToString()
196 {
197 return String.Format("RGBA({0:F3}, {1:F3}, {2:F3}, {3:F3})", r, g, b, a);
198 }
199
204 public string ToString(string format)
205 {
206 return String.Format("RGBA({0}, {1}, {2}, {3})", r.ToString(), g.ToString(), b.ToString(), a.ToString());
207 }
208
209 public override bool Equals(object other)
210 {
211 if (!(other is Color))
212 {
213 return false;
214 }
215 return Equals((Color)other);
216 }
217
218 public bool Equals(Color other)
219 {
220 return r.Equals(other.r) && g.Equals(other.g) && b.Equals(other.b) && a.Equals(other.a);
221 }
222
223 public static Color operator +(Color a, Color b)
224 {
225 return new Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
226 }
227
228 public static Color operator -(Color a, Color b)
229 {
230 return new Color(a.r - b.r, a.g - b.g, a.b - b.b, a.a - b.a);
231 }
232
233 public static Color operator *(Color a, Color b)
234 {
235 return new Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
236 }
237
238 public static Color operator *(Color a, float b)
239 {
240 return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
241 }
242
243 public static Color operator *(float b, Color a)
244 {
245 return new Color(a.r * b, a.g * b, a.b * b, a.a * b);
246 }
247
248 public static Color operator /(Color a, float b)
249 {
250 return new Color(a.r / b, a.g / b, a.b / b, a.a / b);
251 }
252
253 public static bool operator ==(Color lhs, Color rhs)
254 {
255 return (Vector4)lhs == (Vector4)rhs;
256 }
257
258 public static bool operator !=(Color lhs, Color rhs)
259 {
260 return !(lhs == rhs);
261 }
262
263 public static implicit operator Vector4(Color c)
264 {
265 return new Vector4(c.r, c.g, c.b, c.a);
266 }
267
268 public static implicit operator Color(Vector4 v)
269 {
270 return new Color(v.x, v.y, v.z, v.w);
271 }
272
279 public static Color Lerp(Color a, Color b, float t)
280 {
281 t = Mathf.Clamp01(t);
282 return new Color(a.r + (b.r - a.r) * t, a.g + (b.g - a.g) * t, a.b + (b.b - a.b) * t, a.a + (b.a - a.a) * t);
283 }
284
291 public static Color LerpUnclamped(Color a, Color b, float t)
292 {
293 return new Color(a.r + (b.r - a.r) * t, a.g + (b.g - a.g) * t, a.b + (b.b - a.b) * t, a.a + (b.a - a.a) * t);
294 }
295
296 public static void RGBToHSV(Color rgbColor, out float H, out float S, out float V)
297 {
298 if (rgbColor.b > rgbColor.g && rgbColor.b > rgbColor.r)
299 {
300 RGBToHSVHelper(4f, rgbColor.b, rgbColor.r, rgbColor.g, out H, out S, out V);
301 }
302 else if (rgbColor.g > rgbColor.r)
303 {
304 RGBToHSVHelper(2f, rgbColor.g, rgbColor.b, rgbColor.r, out H, out S, out V);
305 }
306 else
307 {
308 RGBToHSVHelper(0f, rgbColor.r, rgbColor.g, rgbColor.b, out H, out S, out V);
309 }
310 }
311
312 private static void RGBToHSVHelper(float offset, float dominantcolor, float colorone, float colortwo, out float H, out float S, out float V)
313 {
314 V = dominantcolor;
315 if (V != 0f)
316 {
317 float num = 0f;
318 num = ((!(colorone > colortwo)) ? colorone : colortwo);
319 float num2 = V - num;
320 if (num2 != 0f)
321 {
322 S = num2 / V;
323 H = offset + (colorone - colortwo) / num2;
324 }
325 else
326 {
327 S = 0f;
328 H = offset + (colorone - colortwo);
329 }
330 H /= 6f;
331 if (H < 0f)
332 {
333 H += 1f;
334 }
335 }
336 else
337 {
338 S = 0f;
339 H = 0f;
340 }
341 }
342
353 public static Color HSVToRGB(float H, float S, float V)
354 {
355 return HSVToRGB(H, S, V, hdr: true);
356 }
357
368 public static Color HSVToRGB(float H, float S, float V, bool hdr)
369 {
371 if (S == 0f)
372 {
373 white.r = V;
374 white.g = V;
375 white.b = V;
376 }
377 else if (V == 0f)
378 {
379 white.r = 0f;
380 white.g = 0f;
381 white.b = 0f;
382 }
383 else
384 {
385 white.r = 0f;
386 white.g = 0f;
387 white.b = 0f;
388 float num = H * 6f;
389 int num2 = (int)Mathf.Floor(num);
390 float num3 = num - (float)num2;
391 float num4 = V * (1f - S);
392 float num5 = V * (1f - S * num3);
393 float num6 = V * (1f - S * (1f - num3));
394 switch (num2)
395 {
396 case 0:
397 white.r = V;
398 white.g = num6;
399 white.b = num4;
400 break;
401 case 1:
402 white.r = num5;
403 white.g = V;
404 white.b = num4;
405 break;
406 case 2:
407 white.r = num4;
408 white.g = V;
409 white.b = num6;
410 break;
411 case 3:
412 white.r = num4;
413 white.g = num5;
414 white.b = V;
415 break;
416 case 4:
417 white.r = num6;
418 white.g = num4;
419 white.b = V;
420 break;
421 case 5:
422 white.r = V;
423 white.g = num4;
424 white.b = num5;
425 break;
426 case 6:
427 white.r = V;
428 white.g = num6;
429 white.b = num4;
430 break;
431 case -1:
432 white.r = V;
433 white.g = num4;
434 white.b = num5;
435 break;
436 }
437 if (!hdr)
438 {
439 white.r = Mathf.Clamp(white.r, 0f, 1f);
440 white.g = Mathf.Clamp(white.g, 0f, 1f);
441 white.b = Mathf.Clamp(white.b, 0f, 1f);
442 }
443 }
444 return white;
445 }
446
447 public override int GetHashCode()
448 {
449 return base.GetHashCode();
450 }
451 }
452}
static float Floor(float f)
Definition: Mathf.cs:344
static float Clamp(float value, float min, float max)
Definition: Mathf.cs:400
static float Clamp01(float value)
Definition: Mathf.cs:436
static Color HSVToRGB(float H, float S, float V, bool hdr)
Definition: Color.cs:368
static Color gray
Definition: Color.cs:55
static Color operator+(Color a, Color b)
Definition: Color.cs:223
static Color fuchsia
Definition: Color.cs:80
static Color red
Definition: Color.cs:15
static Color white
Definition: Color.cs:30
static Color LerpUnclamped(Color a, Color b, float t)
Definition: Color.cs:291
override string ToString()
Definition: Color.cs:195
float grayscale
Definition: Color.cs:120
bool Equals(Color other)
Definition: Color.cs:218
static bool operator!=(Color lhs, Color rhs)
Definition: Color.cs:258
static Color operator/(Color a, float b)
Definition: Color.cs:248
static Color blue
Definition: Color.cs:25
static Color HSVToRGB(float H, float S, float V)
Definition: Color.cs:353
override int GetHashCode()
Definition: Color.cs:447
static Color magenta
Definition: Color.cs:50
Color(float r, float g, float b)
Definition: Color.cs:183
static bool operator==(Color lhs, Color rhs)
Definition: Color.cs:253
static Color operator*(Color a, Color b)
Definition: Color.cs:233
static Color darkRed
Definition: Color.cs:100
static Color operator-(Color a, Color b)
Definition: Color.cs:228
string ToString(string format)
Definition: Color.cs:204
static Color black
Definition: Color.cs:35
static Color yellow
Definition: Color.cs:40
static Color green
Definition: Color.cs:20
static Color teal
Definition: Color.cs:115
static Color silver
Definition: Color.cs:110
override bool Equals(object other)
Definition: Color.cs:209
static Color olive
Definition: Color.cs:105
static Color darkBlue
Definition: Color.cs:95
static Color grey
Definition: Color.cs:60
Color(float r, float g, float b, float a)
Definition: Color.cs:169
static Color aqua
Definition: Color.cs:70
static void RGBToHSV(Color rgbColor, out float H, out float S, out float V)
Definition: Color.cs:296
static Color cream
Definition: Color.cs:75
static Color cyan
Definition: Color.cs:45
static Color Lerp(Color a, Color b, float t)
Definition: Color.cs:279
static Color clear
Definition: Color.cs:65
static Color darkGreen
Definition: Color.cs:85
static Color maroon
Definition: Color.cs:90