-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palette.cs
81 lines (74 loc) · 2.26 KB
/
Palette.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
using Rippix.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rippix {
public interface IPalette {
int GetARGB(int index);
}
public interface INeedsPalette {
IPalette Palette { get; set; }
int ColorCount { get; }
}
public class GrayscalePalette : IPalette {
private int length;
public int Length {
get { return length; }
set { length = value; }
}
public int GetARGB(int index) {
if (index < 0 || index >= Length || Length <= 1) return 0;
var v = 255 * index / (Length - 1);
v = ColorFormat.Pack(v, v, v, 255);
return v;
}
}
public class CachedPalette : IPalette {
private int[] palCache;
private bool isCacheDirty;
private IPalette nested;
private int length;
public CachedPalette(IPalette nested, int length) {
this.nested = nested;
this.length = length;
}
public int Length { get { return length; } }
public int GetARGB(int index) {
if (index < 0 || index > Length) return 0;
if (isCacheDirty) {
if (palCache == null || palCache.Length != Length) {
palCache = new int[Length];
}
for (int i = 0; i < Length; i++) {
palCache[i] = nested.GetARGB(i);
}
isCacheDirty = false;
}
return palCache[index];
}
public void Invalidate() {
isCacheDirty = true;
}
}
public class CodedPalette : IPalette {
private IPaletteDecoder decoder;
private byte[] data;
private int offset;
public CodedPalette(IPaletteDecoder decoder) {
this.decoder = decoder;
}
public byte[] Data {
get { return data; }
set { data = value; }
}
public int Offset {
get { return offset; }
set { offset = value; }
}
public int GetARGB(int index) {
if (index < 0 || index >= decoder.Length) return 0;
return decoder.GetARGB(Data, Offset, index);
}
}
}