-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColor01.cs
More file actions
107 lines (96 loc) · 3.52 KB
/
Copy pathColor01.cs
File metadata and controls
107 lines (96 loc) · 3.52 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
using System;
using System.Collections.Generic;
using System.Text;
namespace EnesShahn
{
namespace RayTracingEngine
{
class Color01
{
public float R { get; set; }
public float G { get; set; }
public float B { get; set; }
public float A { get; set; }
//TODO: To Hex Conversion
//TODO: Parse from Hex
public Color01(float r, float g, float b, float a)
{
R = r;
G = g;
B = b;
A = a;
Normalize();
}
public Color01(float r, float g, float b) : this(r, g, b, 1f) { }
#region Primitive Colors
public static Color01 Black
{
get { return new Color01(0, 0, 0, 1); }
}
public static Color01 White
{
get { return new Color01(1, 1, 1, 1); }
}
public static Color01 Red
{
get { return new Color01(1, 0, 0, 1); }
}
public static Color01 Green
{
get { return new Color01(0, 1, 0, 1); }
}
public static Color01 Blue
{
get { return new Color01(0, 0, 1, 1); }
}
#endregion
public void Normalize()
{
R = Math.Clamp(R, 0f, 1f);
G = Math.Clamp(G, 0f, 1f);
B = Math.Clamp(B, 0f, 1f);
A = Math.Clamp(A, 0f, 1f);
}
public static Color01 FromArray(float[] rgba)
{
if (rgba.Length > 4)
throw new Exception("Color field in json file has more than 4 values");
else if (rgba.Length < 3)
throw new Exception("Color field in json file needs at least 3 values");
if (rgba.Length == 4)
return new Color01(rgba[0], rgba[1], rgba[2], rgba[3]);
return new Color01(rgba[0], rgba[1], rgba[2]);
}
#region Overloaded Operators
public static Color01 operator *(Color01 color, float b)
{
return new Color01(color.R * b, color.G * b, color.B * b, color.A);
}
public static Color01 operator *(float b, Color01 color)
{
return new Color01(color.R * b, color.G * b, color.B * b, color.A);
}
public static Color01 operator *(Color01 color1, Color01 color2)
{
return new Color01(color1.R * color2.R, color1.G * color2.G, color1.B * color2.B, color1.A * color2.A);
}
public static Color01 operator +(Color01 color1, Color01 color2)
{
return new Color01(color1.R + color2.R, color1.G + color2.G, color1.B + color2.B, color1.A + color2.A);
}
public static bool operator ==(Color01 color1, Color01 color2)
{
return (color1.R == color2.R && color1.G == color2.G && color1.B == color2.B && color1.A == color2.A);
}
public static bool operator !=(Color01 color1, Color01 color2)
{
return (color1.R != color2.R || color1.G != color2.G || color1.B != color2.B || color1.A != color2.A);
}
#endregion
public override string ToString()
{
return $"RGBA({R}, {G}, {B}, {A})";
}
}
}
}