-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalette.js
71 lines (64 loc) · 1.76 KB
/
Palette.js
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
class Palette
{
constructor(name, colors)
{
if(colors.length != Constants.NUM_COLORS)
console.log(`Invalid Palette length ${colors.length}`);
this.name = undefined;
this.colors = colors;
this.selected = 0;
this.Select(0);
this.SetName(name);
}
Set(i, c)
{
if(i < 0 || i >= this.colors.length)
console.log(`Invalid Palette index ${i}`);
return this.colors[i] = c;
}
At(i)
{
if(i < 0 || i >= this.colors.length)
console.log(`Invalid Palette index ${i}`);
return this.colors[i];
}
Select(i, nowarn = false)
{
if(!nowarn && (i < 0 || i >= this.colors.length))
console.log(`Invalid Palette index ${i}`);
return this.selected = i;
}
SetName(s)
{
if(s.length > 255)
{
alert('Palette name must be <= 255 characters.');
return;
}
this.name = s;
}
Save()
{
let index = 0;
let data = new Uint8Array(Constants.NUM_COLORS * 3 + (1 + this.name.length));
data[index++] = this.name.length;
for(var i = 0; i < this.name.length; i++)
data[index++] = this.name.charCodeAt(i);
for(var i = 0; i < Constants.NUM_COLORS; i++){
let cur = this.At(i).uint;
data[index++] = (cur & (0b11111111 << 16)) >> 16;
data[index++] = (cur & (0b11111111 << 8)) >> 8;
data[index++] = cur & 0b11111111;
}
let file = new Blob([data], { type: 'application/octet-stream' });
let a = document.createElement("a"), url = URL.createObjectURL(file);
a.href = url;
a.download = `${this.name}.palette`;
document.body.appendChild(a);
a.click();
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}