-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBLPalette.py
More file actions
77 lines (66 loc) · 2.07 KB
/
BLPalette.py
File metadata and controls
77 lines (66 loc) · 2.07 KB
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
import PaletteCore
class BLColor(PaletteCore.Color):
def fromColor(color):
blc = BLColor()
blc.r = color.r
blc.g = color.g
blc.b = color.b
blc.a = color.a
return blc
def __init__(self, line = None):
if line == None:
super().__init__()
else:
nums = line.split()
if len(nums) != 4:
raise Exception("Malformed Palette Entry")
super().__init__(int(nums[0]), int(nums[1]), int(nums[2]), int(nums[3]))
def __str__(self):
return "{:d} {:d} {:d} {:d}".format(self.r, self.g, self.b, self.a)
class BLPalette(PaletteCore.Palette):
maxpal = 64
def __init__(self):
super().__init__()
self.i = 0
self.curdiv = 0
self.divs = list()
def add_entry(self, color):
if type(color) is not BLColor:
raise TypeError
if len(self.palette) == BLPalette.maxpal:
raise Exception("Too Many Colors")
super().add_entry(color)
def add_div(self, line, name):
if type(line) is not int or type(name) is not str:
raise TypeError
self.divs.append((line, name))
def add_line(self, line):
if len(line) > 0 and line[-1] == '\n': #remove newlines if present
line = line[:-1]
#print("\"{:s}\"".format(line))
if len(line) == 0: #ignore blank lines
return
if line[:4] == 'DIV:':
self.add_div(len(self.palette), line[4:])
else:
self.add_entry(BLColor(line))
def __iter__(self):
self.i = 0 #probably not correct, but if iteration is interrupted, reset
self.curdiv = 0
return self
def __next__(self):
if self.i == len(self.palette):
if self.curdiv < len(self.divs) and self.divs[self.curdiv][0] == self.i:
tdiv = self.curdiv
self.curdiv += 1
return "DIV:{:s}\n".format(self.divs[tdiv][1])
self.i = 0
self.curdiv = 0
raise StopIteration
if self.curdiv < len(self.divs) and self.divs[self.curdiv][0] == self.i:
tdiv = self.curdiv
self.curdiv += 1
return "DIV:{:s}\n".format(self.divs[tdiv][1])
ti = self.i
self.i += 1
return str(self.palette[ti])