-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathColor.cs
More file actions
147 lines (129 loc) · 4.7 KB
/
Color.cs
File metadata and controls
147 lines (129 loc) · 4.7 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
using System;
using System.Runtime.InteropServices;
namespace Voxels {
[StructLayout(LayoutKind.Explicit)]
public struct Color : IEquatable<Color> {
[FieldOffset(0)]
public uint RGBA;
[FieldOffset(0)]
public byte R;
[FieldOffset(1)]
public byte G;
[FieldOffset(2)]
public byte B;
[FieldOffset(3)]
public byte A;
public Color(byte r, byte g, byte b) {
RGBA = 0;
R = r;
G = g;
B = b;
A = 255;
}
public Color(byte r, byte g, byte b, byte a) {
RGBA = 0;
R = r;
G = g;
B = b;
A = a;
}
public Color(uint rgba) {
R = G = B = A = 0;
RGBA = rgba;
}
public Color(Color c, byte a) {
RGBA = 0;
R = c.R;
G = c.G;
B = c.B;
A = a;
}
public Color(float r, float g, float b, float a) {
RGBA = 0;
R = (byte)(r * 255);
G = (byte)(g * 255);
B = (byte)(b * 255);
A = (byte)(a * 255);
}
public static Color operator * (Color c, float f) {
return new Color((byte)(c.R * f), (byte)(c.G * f), (byte)(c.B * f), c.A);
}
public static Color FromRGBA(uint v) {
return new Color(v);
}
public static Color FromBGRA(uint v) {
var c = new Color(v);
return new Color(c.B, c.G, c.R, c.A);
}
public void ToHSV(out float hue, out float saturation, out float value) {
var r = R / 255f;
var g = G / 255f;
var b = B / 255f;
var max = Math.Max(r, Math.Max(g, b));
var min = Math.Min(r, Math.Min(g, b));
if (max == r) {
hue = (g - b) / (max - min);
}
else if (max == g) {
hue = 2f + (b - r) / (max - min);
}
else {
hue = 4f + (r - g) / (max - min);
}
hue = (float)Math.Round(hue * 60);
if (hue < 0) {
hue = hue + 360;
}
hue = float.IsNaN(hue) ? 0 : hue;
saturation = (max == 0) ? 0 : 1 - (min / max);
value = max;
}
public static Color FromHSV(float hue, float saturation, float value) {
var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
var f = hue / 60 - Math.Floor(hue / 60);
value = value * 255;
var v = Convert.ToByte(value);
var p = Convert.ToByte(value * (1 - saturation));
var q = Convert.ToByte(value * (1 - f * saturation));
var t = Convert.ToByte(value * (1 - (1 - f) * saturation));
if (hi == 0)
return new Color(v, t, p);
else if (hi == 1)
return new Color(q, v, p);
else if (hi == 2)
return new Color(p, v, t);
else if (hi == 3)
return new Color(p, q, v);
else if (hi == 4)
return new Color(t, p, v);
else
return new Color(v, p, q);
}
public override string ToString() {
return RGBA.ToString("X8");
}
public override int GetHashCode() {
return (int) RGBA;
}
public override bool Equals(object other) {
return Equals((Color)other);
}
public bool Equals(Color other) {
return this.RGBA == other.RGBA;
}
public static readonly Color Transparent = new Color(0, 0, 0, 0);
public static readonly Color Black = new Color(0, 0, 0);
public static readonly Color White = new Color(255, 255, 255);
public static readonly Color Red = new Color(255, 0, 0);
public static readonly Color Green = new Color(0, 255, 0);
public static readonly Color Blue = new Color(0, 0, 255);
public static readonly Color Yellow = new Color(255, 255, 0);
public static readonly Color Cyan = new Color(0, 255, 255);
public static readonly Color Magenta = new Color(255, 0, 255);
public static readonly Color Silver = new Color(192, 192, 192);
public static readonly Color Gray = new Color(128, 128, 128);
public static readonly Color Maroon = new Color(128, 0, 0);
public static readonly Color Olive = new Color(128, 128, 0);
public static readonly Color Tan = new Color(210, 180, 140);
}
}