-
Notifications
You must be signed in to change notification settings - Fork 5
/
colormap.py
executable file
·51 lines (45 loc) · 2.01 KB
/
colormap.py
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
import omg.palette
import omg.lump
class Colormap:
"""An editor for Doom's COLORMAP lump. The colormap holds 34 tables
of indices to the game's palette. The first 32 tables hold data
for different brightness levels, the 33rd holds the indices used
by the invulnerability powerup, and the 34th is unused."""
def __init__(self, from_lump=None):
"""Create new, optionally from an existing lump."""
self.tables = [[0]*256]*34
if from_lump:
self.from_lump(from_lump)
def build_fade(self, palette=None, fade=(0,0,0)):
"""Build fade tables. The default fade color is black;
this may be overriden. Light color is not yet supported."""
palette = palette or omg.palette.default
x, y, z = fade
for n in range(32):
e = 31-n
for c in range(256):
r, g, b = palette.colors[c]
r = (r*n + x*e) // 32
g = (g*n + y*e) // 32
b = (b*n + z*e) // 32
self.tables[e][c] = palette.match((r,g,b))
def build_invuln(self, palette=None, start=(0,0,0), end=(255,255,255)):
"""Build range used by the invulnerability powerup."""
palette = palette or omg.palette.default
ar, ag, ab = start
br, bg, bb = end
for i in range(256):
bright = sum(palette.colors[i]) // 3
r = (ar*bright + br*(256-bright)) // 256
g = (ag*bright + bg*(256-bright)) // 256
b = (ab*bright + bb*(256-bright)) // 256
self.tables[32][i] = palette.match((r,g,b))
def from_lump(self, lump):
"""Load from a COLORMAP lump."""
assert len(lump.data) == 34*256
for n in range(34):
self.tables[n] = [ord(lump.data[i]) for i in range(n*256,(n+1)*256)]
def to_lump(self):
"""Pack to a COLORMAP lump."""
packed = [''.join([chr(c) for c in t]) for t in self.tables]
return omg.lump.Lump(''.join(packed))