-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorHex.cs
162 lines (139 loc) · 4.79 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
// ColorHex struct - Mainly used as a way to create a Color32 using a hex code
// @Author: Travis Abendshien (https://github.com/CyanVoxel)
// @Version: 1.1
// A simple C# script that allows for creating UnityEngine Color32 variables via a hex code string
// Getting Started
// Getting ColorHex into your Unity project is as simple as dropping the ColorHex.cs file into your Assets/Scripts project folder.
// Usage
// In order to have access to ColorHex in your script, make sure to include using ColorHexUtility; at the top of any script that requires it.
// Instead of using Color32’s constructor which requires you to pass byte values, you can now use the ColorHex constructor to pass in a hex code string. Since ColorHex is designed to be interoperable with Color32, an implementation can look like this:
// public Color32 tintRed500 = new ColorHex("#F14234");
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;
}
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 ==(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 !=(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 override int GetHashCode()
{
return r ^ g ^ b ^ a;
}
}
}