-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorPalette.cs
49 lines (45 loc) · 1.41 KB
/
ColorPalette.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
using SkiaSharp;
namespace wad3_cli
{
internal class ColorPalette
{
SKColor[] colors;
Dictionary<string, int> assoc;
int currentIndex;
public ColorPalette(int paletteSize = 256)
{
colors = new SKColor[paletteSize];
assoc = new Dictionary<string, int>();
currentIndex = 0;
}
public int GetOrAddPaletteIndex(SKColor color)
{
string colorName = color.Red.ToString("000") + color.Green.ToString("000") + color.Blue.ToString("000");
if (!assoc.ContainsKey(colorName))
{
colors[currentIndex] = color;
assoc[colorName] = currentIndex;
currentIndex += 1;
}
return assoc[colorName];
}
public byte[] GetBytes()
{
byte[] paletteData = new byte[colors.Length * 3];
int paletteIndex = 0;
foreach (SKColor color in colors)
{
paletteData[paletteIndex] = color.Red;
paletteData[paletteIndex + 1] = color.Green;
paletteData[paletteIndex + 2] = color.Blue;
paletteIndex += 3;
}
// Pad missing bytes
while (paletteIndex < paletteData.Length)
{
paletteData[paletteIndex] = 0;
}
return paletteData;
}
}
}