-
Notifications
You must be signed in to change notification settings - Fork 1
/
ColorData.cs
213 lines (189 loc) · 5.19 KB
/
ColorData.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using npScripts;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;
namespace npScripts
{
[System.Serializable]
public class ColorData : iXML, IComparable<ColorData>
{
public enum ColorDataType { Hex, RGB, HSL, HSV }
public ColorDataType colorType;
public string name;
public string hex;
public float red;
public float green;
public float blue;
public float hue;
public float saturationHSL;
public float light;
public float saturationHSV;
public float value;
public Color CreateColor()
{
Color c = new Color();
switch (colorType)
{
case ColorDataType.RGB:
c = new Color(red/100f, green / 100f, blue / 100f);
break;
case ColorDataType.HSV:
c = new Color();
c = Color.HSVToRGB(hue, saturationHSV, value);
break;
case ColorDataType.HSL:
c = HSLToRGBColor(this);
break;
case ColorDataType.Hex:
ColorUtility.TryParseHtmlString("#" + hex, out c);
break;
}
return c;
}
public static Color HSLToRGBColor(ColorData hsbColor)
{
float r = hsbColor.light;
float g = hsbColor.light;
float b = hsbColor.light;
if (hsbColor.saturationHSL != 0)
{
float max = hsbColor.light;
float dif = hsbColor.light * hsbColor.saturationHSL;
float min = hsbColor.light - dif;
float h = hsbColor.hue * 360f;
if (h < 60f)
{
r = max;
g = h * dif / 60f + min;
b = min;
}
else if (h < 120f)
{
r = -(h - 120f) * dif / 60f + min;
g = max;
b = min;
}
else if (h < 180f)
{
r = min;
g = max;
b = (h - 120f) * dif / 60f + min;
}
else if (h < 240f)
{
r = min;
g = -(h - 240f) * dif / 60f + min;
b = max;
}
else if (h < 300f)
{
r = (h - 240f) * dif / 60f + min;
g = min;
b = max;
}
else if (h <= 360f)
{
r = max;
g = min;
b = -(h - 360f) * dif / 60 + min;
}
else
{
r = 0;
g = 0;
b = 0;
}
}
return new Color(Mathf.Clamp01(r), Mathf.Clamp01(g), Mathf.Clamp01(b));
}
public void ReadWikipediaColorLine (string line)
{
string [] splits = line.Split(new char[]{ '\t'});
name = splits [0];
hex = splits [1].Substring(1);
if (splits [2].Length > 2)
red = float.Parse(splits [2].Remove(splits [2].Length - 2));
else
{
red = float.Parse(splits [2].Remove(splits [2].Length - 1));
}
if (splits [3].Length > 2)
green = float.Parse(splits [3].Remove(splits [3].Length - 2));
else
{
green = float.Parse(splits [3].Remove(splits [3].Length - 1));
}
if (splits [4].Length > 2)
blue = float.Parse(splits [4].Remove(splits [4].Length - 2));
else
blue = float.Parse(splits [4].Remove(splits [4].Length - 1));
if (splits [5].Length > 2)
hue = float.Parse(splits [5].Remove(splits [5].Length - 2));
else
hue = float.Parse(splits [5].Remove(splits [5].Length - 1));
if (splits [6].Length > 2)
saturationHSL = float.Parse(splits [6].Remove(splits [6].Length - 2));
else
saturationHSL = float.Parse(splits [6].Remove(splits [6].Length - 1));
if (splits [7].Length > 2)
light = float.Parse(splits [7].Remove(splits [7].Length - 2));
else
light = float.Parse(splits [7].Remove(splits [7].Length - 1));
if (splits [8].Length > 2)
saturationHSV = float.Parse(splits [8].Remove(splits [8].Length - 2));
else
saturationHSV = float.Parse(splits [8].Remove(splits [8].Length - 1));
if (splits [9].Length > 2)
value = float.Parse(splits [9].Remove(splits [9].Length - 2));
else
value = float.Parse(splits [9].Remove(splits [9].Length - 1));
/*
for (int i = 0; i < splits.Length; i++)
{
Debug.Log(splits [i]);
}*/
}
public void ReadFromXML(XmlReader reader)
{
name = reader.GetAttribute("name");
hex = reader.GetAttribute("hex");
red = float.Parse(reader.GetAttribute("red"));
green = float.Parse(reader.GetAttribute("green"));
blue = float.Parse(reader.GetAttribute("blue"));
hue = float.Parse(reader.GetAttribute("hue"));
saturationHSL = float.Parse(reader.GetAttribute("saturationHSL"));
light = float.Parse(reader.GetAttribute("light"));
saturationHSV = float.Parse(reader.GetAttribute("saturationHSV"));
value = float.Parse(reader.GetAttribute("value"));
}
public void WriteToXML(XmlWriter writer)
{
writer.WriteStartElement("Color");
writer.WriteAttributeString("name", name);
writer.WriteAttributeString("hex", hex);
writer.WriteAttributeString("red", red.ToString());
writer.WriteAttributeString("green", green.ToString());
writer.WriteAttributeString("blue", blue.ToString());
writer.WriteAttributeString("hue", hue.ToString());
writer.WriteAttributeString("saturationHSL", saturationHSL.ToString());
writer.WriteAttributeString("light", light.ToString());
writer.WriteAttributeString("saturationHSV", saturationHSV.ToString());
writer.WriteAttributeString("value", value.ToString());
writer.WriteEndElement();
}
public string GetElementName()
{
return "Color";
}
public override string ToString()
{
return name;
}
public int CompareTo(ColorData other)
{
return name.CompareTo(other.name);
}
}
}