forked from S-C-A-N/SCANsat
/
SCANPalette.cs
129 lines (105 loc) · 2.42 KB
/
SCANPalette.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
#region license
/*
* [Scientific Committee on Advanced Navigation]
* S.C.A.N. Satellite
*
* SCANPalette - Class to hold color palette info
*
* Copyright (c)2014 David Grandy <david.grandy@gmail.com>;
* Copyright (c)2014 technogeeky <technogeeky@gmail.com>;
* Copyright (c)2014 (Your Name Here) <your email here>; see LICENSE.txt for licensing details.
*/
#endregion
using System;
using UnityEngine;
using SCANsat.SCAN_Platform;
namespace SCANsat.SCAN_Palettes
{
public class SCANPalette : SCAN_ConfigNodeStorage
{
[Persistent]
private string Colors;
private int _count;
private int _hash;
private string _name;
private SCANPaletteKind _kind;
private Color32[] _colors;
private Color32[] _colorsReverse;
public int Count
{
get { return _count; }
}
public int Hash
{
get { return _hash; }
}
public string Name
{
get { return _name; }
set
{
_name = value;
_hash = (_name + _count).GetHashCode();
}
}
public SCANPaletteKind Kind
{
get { return _kind; }
set { _kind = value; }
}
public Color32[] ColorsArray
{
get { return _colors; }
}
public Color32[] ColorsReverse
{
get { return _colorsReverse; }
}
public SCANPalette() { }
public SCANPalette(Color32[] colors, string name, SCANPaletteKind kind, int size)
{
_name = name;
_count = size;
_kind = kind;
_hash = (name + size).GetHashCode();
_colors = colors;
_colorsReverse = new Color32[_count];
_colors.CopyTo(_colorsReverse, 0);
Array.Reverse(_colorsReverse);
}
public override void OnDecodeFromConfigNode()
{
string[] split = Colors.Split('|');
_count = split.Length;
_colors = new Color32[_count];
for (int i = 0; i < _count; i++)
{
string color = split[i];
Color32 c = Color.clear;
try
{
c = ConfigNode.ParseColor32(color);
}
catch (Exception e)
{
SCANUtil.SCANlog("Error while parsing SCANsat palette colors - {0}\n{1}", color, e);
}
_colors[i] = c;
}
_colorsReverse = new Color32[_count];
_colors.CopyTo(_colorsReverse, 0);
Array.Reverse(_colorsReverse);
}
public override void OnEncodeToConfigNode()
{
string[] colors = new string[_colors.Length];
for (int i = 0; i < _colors.Length; i++)
{
Color32 c = _colors[i];
string s = string.Format("{0},{1},{2},{3}", c.r, c.g, c.b, c.a);
colors[i] = s;
}
Colors = string.Join("|", colors);
}
}
}