Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ColorHumanReadableConverter.cs
浏览该文件的文档.
1namespace Engine.Serialization {
4 public string ConvertToString(object value) {
5 Color color = (Color)value;
6 return color.A != byte.MaxValue
7 ? HumanReadableConverter.ValuesListToString(',', color.R, color.G, color.B, color.A)
8 : HumanReadableConverter.ValuesListToString(',', color.R, color.G, color.B);
9 }
10
11 public object ConvertFromString(Type type, string data) {
12 if (data[0] == '#') {
13 if (data.Length == 7) {
14 byte r = (byte)(16 * HexToDecimal(data[1]) + HexToDecimal(data[2]));
15 byte g = (byte)(16 * HexToDecimal(data[3]) + HexToDecimal(data[4]));
16 byte b = (byte)(16 * HexToDecimal(data[5]) + HexToDecimal(data[6]));
17 return new Color(r, g, b);
18 }
19 if (data.Length == 9) {
20 byte r2 = (byte)(16 * HexToDecimal(data[1]) + HexToDecimal(data[2]));
21 byte g2 = (byte)(16 * HexToDecimal(data[3]) + HexToDecimal(data[4]));
22 byte b2 = (byte)(16 * HexToDecimal(data[5]) + HexToDecimal(data[6]));
23 byte a = (byte)(16 * HexToDecimal(data[7]) + HexToDecimal(data[8]));
24 return new Color(r2, g2, b2, a);
25 }
26 throw new Exception();
27 }
28 int[] array = HumanReadableConverter.ValuesListFromString<int>(',', data);
29 return array.Length == 3 ? new Color(array[0], array[1], array[2]) :
30 array.Length == 4 ? (object)new Color(array[0], array[1], array[2], array[3]) : throw new Exception();
31 }
32
33 static int HexToDecimal(char digit) {
34 if (digit >= '0'
35 && digit <= '9') {
36 return digit - 48;
37 }
38 return digit >= 'A' && digit <= 'F' ? digit - 65 + 10 :
39 digit >= 'a' && digit <= 'f' ? digit - 97 + 10 : throw new Exception();
40 }
41 }
42}
Engine.Color Color