/
CustomColorHelper.cs
179 lines (173 loc) · 6.19 KB
/
CustomColorHelper.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
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using DiskCardGame;
using InscryptionAPI.Card;
namespace ConfigurableEmissions
{
public static class CustomColorHelper
{
public static bool TryGetColorFromCardInfo(this CardInfo card, string prefix, out Color color)
{
return
TryGetColorFromHex(card.GetExtendedProperty($"{prefix}HEX"), out color) ||
TryGetRGBFromString32(card.GetExtendedProperty($"{prefix}255"), out color) ||
TryGetRGBFromString(card.GetExtendedProperty($"{prefix}1"), out color) ||
TryGetRGBFromHSVString32(card.GetExtendedProperty($"{prefix}HSV"), out color) ||
TryGetRGBFromHSVString(card.GetExtendedProperty($"{prefix}HSV1"), out color) ||
TryGetColorFromHex(card.GetExtendedProperty(prefix), out color) ||
TryGetRGBFromString32(card.GetExtendedProperty(prefix), out color);
}
public static bool TryGetRGBFromString(string str, out Color c)
{
if(str != null)
{
try
{
string[] vals = str.Split(',');
if (vals.Length > 0)
{
float.TryParse(vals[0].Trim(), out var r);
float g = 0f;
float b = 0f;
float a = 1f;
if (vals.Length > 1)
{
float.TryParse(vals[1].Trim(), out g);
if (vals.Length > 2)
{
float.TryParse(vals[2].Trim(), out b);
if (vals.Length > 3)
{
float.TryParse(vals[3].Trim(), out a);
}
}
}
c = new Color(r, g, b, a);
return true;
}
}
catch { }
}
c = Color.clear;
return false;
}
public static bool TryGetRGBFromHSVString(string str, out Color c)
{
if (str != null)
{
try
{
string[] vals = str.Split(',');
if (vals.Length > 0)
{
float.TryParse(vals[0].Trim(), out var h);
float s = 0f;
float v = 0f;
float a = 1f;
if (vals.Length > 1)
{
float.TryParse(vals[1].Trim(), out s);
if (vals.Length > 2)
{
float.TryParse(vals[2].Trim(), out v);
if (vals.Length > 3)
{
float.TryParse(vals[3].Trim(), out a);
}
}
}
c = Color.HSVToRGB(h, s, v);
c.a = a;
return true;
}
}
catch { }
}
c = Color.clear;
return false;
}
public static bool TryGetRGBFromHSVString32(string str, out Color c)
{
if (str != null)
{
try
{
string[] vals = str.Split(',');
if (vals.Length > 0)
{
byte.TryParse(vals[0].Trim(), out var h);
byte s = 0;
byte v = 0;
byte a = 1;
if (vals.Length > 1)
{
byte.TryParse(vals[1].Trim(), out s);
if (vals.Length > 2)
{
byte.TryParse(vals[2].Trim(), out v);
if (vals.Length > 3)
{
byte.TryParse(vals[3].Trim(), out a);
}
}
}
c = Color.HSVToRGB(h / 360f, s / 100f, v / 100f);
c.a = a / 255f;
return true;
}
}
catch { }
}
c = Color.clear;
return false;
}
public static bool TryGetRGBFromString32(string str, out Color c)
{
if (str != null)
{
try
{
string[] vals = str.Split(',');
if (vals.Length > 0)
{
byte.TryParse(vals[0].Trim(), out var r);
byte g = 0;
byte b = 0;
byte a = 255;
if (vals.Length > 1)
{
byte.TryParse(vals[1].Trim(), out g);
if (vals.Length > 2)
{
byte.TryParse(vals[2].Trim(), out b);
if (vals.Length > 3)
{
byte.TryParse(vals[3].Trim(), out a);
}
}
}
c = new Color32(r, g, b, a);
return true;
}
}
catch { }
}
c = Color.clear;
return false;
}
public static bool TryGetColorFromHex(string hex, out Color c)
{
try
{
return ColorUtility.TryParseHtmlString(hex, out c);
}
catch
{
c = Color.clear;
return false;
}
}
}
}