-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorHex.cs
189 lines (170 loc) · 5.33 KB
/
ColorHex.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
/// <summary>
/// Struct <c>ColorHex</c> - Used to create Unity Colors via hex codes.
/// Author: Travis Abendshien (https://github.com/CyanVoxel)
/// Version: 1.2
/// </summary>
namespace ColorHexUtility
{
public struct ColorHex
{
byte r;
byte g;
byte b;
byte a;
// Normal Color32-style byte constructor.
public ColorHex(byte r, byte g, byte b, byte a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
// String hex constructor, handles optional '#' character as well as optional alpha values.
public ColorHex(string hex)
{
string h = hex;
if (h.Contains("#"))
{
h = h.Remove(hex.IndexOf("#"), 1);
}
switch (h.Length)
{
case 6:
this.r = byte.Parse(h.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
this.g = byte.Parse(h.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
this.b = byte.Parse(h.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
this.a = 255;
break;
case 8:
this.r = byte.Parse(h.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
this.g = byte.Parse(h.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
this.b = byte.Parse(h.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
this.a = byte.Parse(h.Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
break;
default:
this.r = 0;
this.g = 0;
this.b = 0;
this.a = 0;
break;
}
}
public override bool Equals(object obj)
{
bool typeCheck = false;
if (this.GetType().Equals(obj.GetType()) || obj is UnityEngine.Color32)
{
typeCheck = true;
}
else if (this.GetType().Equals(obj.GetType()) || obj is UnityEngine.Color)
{
typeCheck = true;
}
if (obj == null || !typeCheck)
{
return false;
}
else
{
ColorHex c = (ColorHex)obj;
return (r == c.r && g == c.g && b == c.b && a == c.a);
}
}
public static bool operator ==(UnityEngine.Color32 left, ColorHex right)
{
if (left.r == right.r
&& left.g == right.g
&& left.b == right.b
&& left.a == right.a)
{
return true;
}
else
{
return false;
}
}
public static bool operator ==(UnityEngine.Color left, ColorHex right)
{
if (left.r == right.r
&& left.g == right.g
&& left.b == right.b
&& left.a == right.a)
{
return true;
}
else
{
return false;
}
}
public static bool operator ==(ColorHex left, ColorHex right)
{
if (left.r == right.r
&& left.g == right.g
&& left.b == right.b
&& left.a == right.a)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(UnityEngine.Color32 left, ColorHex right)
{
if (left.r != right.r
|| left.g != right.g
|| left.b != right.b
|| left.a != right.a)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(UnityEngine.Color left, ColorHex right)
{
if (left.r != right.r
|| left.g != right.g
|| left.b != right.b
|| left.a != right.a)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(ColorHex left, ColorHex right)
{
if (left.r != right.r
|| left.g != right.g
|| left.b != right.b
|| left.a != right.a)
{
return true;
}
else
{
return false;
}
}
public static implicit operator UnityEngine.Color32(ColorHex c)
{
return new UnityEngine.Color32(c.r, c.g, c.b, c.a);
}
public static implicit operator UnityEngine.Color(ColorHex c)
{
return new UnityEngine.Color(c.r, c.g, c.b, c.a);
}
public override int GetHashCode()
{
return r ^ g ^ b ^ a;
}
}
}