-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpalette.py
195 lines (159 loc) · 6.09 KB
/
palette.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from collections import OrderedDict
import json,os
palettes = OrderedDict()
available_palettes = []
def _build_c64_palettes():
global palettes
print('building C64 palette')
# gamma corrected colors from
# http://unusedino.de/ec64/technical/misc/vic656x/colors/
palette = [
[ 0.0, 0.0, 0.0 ],
[254.999999878, 254.999999878, 254.999999878],
[103.681836072, 55.445357742, 43.038096345],
[111.932673473, 163.520631667, 177.928819803],
[111.399725075, 60.720543693, 133.643433983],
[ 88.102223525, 140.581101312, 67.050415368],
[ 52.769271594, 40.296416104, 121.446211753],
[183.892638117, 198.676829993, 110.585717385],
[111.399725075, 79.245328562, 37.169652483],
[ 66.932804788, 57.383702891, 0.0 ],
[153.690586380, 102.553762644, 89.111118307],
[ 67.999561813, 67.999561813, 67.999561813],
[107.797780127, 107.797780127, 107.797780127],
[154.244479632, 209.771445903, 131.584994128],
[107.797780127, 94.106015515, 180.927622164],
[149.480882981, 149.480882981, 149.480882981],
]
palettes['c64'] = [[c/255. for c in color] for color in palette]
def _build_websafe_palettes():
# note that there is only an _accepted_ set of 216 "websafe" colors, but
# there is no fully standardized set
# this is using the common set of 216 6-bit colors
global palettes
print('building websafe palette')
palette = []
for r in range(6):
for g in range(6):
for b in range(6):
palette.append([r/5.0, g/5.0, b/5.0])
palettes['websafe'] = palette
def _build_grayscale_palettes():
global palettes
print('building grayscale palettes')
for bit_depth in range(1, 8):
levels = 2**bit_depth - 1
pname = '{}bit_gray'.format(bit_depth)
palette = [ [0.0, 0.0, 0.0] ]
for l in range(levels):
val = float(l+1)/(levels)
palette.append([val, val, val])
palettes[pname] = palette
def _build_cga_palettes():
# this actually builds all possible colors based on rgb combinations of
# on/off, though some of the colors were not available on CGA
global palettes
print('building cga palettes')
# generate all the low/dark colors
low = []
off_on = (0.0, 2./3.)
for r in off_on:
for g in off_on:
for b in off_on:
low.append([r, g, b])
# set brown
low[6][1] /= 2.
# generate all the high colors
high = []
off_on = (1./3., 1.0)
for r in off_on:
for g in off_on:
for b in off_on:
high.append([r, g, b])
# add the colors to their respective palettes
palettes['cga_mode4_1'] = [ low[0], # black
low[3], # low cyan
low[5], # low magenta
low[7] ] # low white
palettes['cga_mode4_2'] = [ low[0], # black
low[2], # low green
low[4], # low red
low[6] ] # low yellow (brown)
palettes['cga_mode4_1_high'] = [ low[0], # black
high[3], # high cyan
high[5], # high magenta
high[7] ] # high white
palettes['cga_mode4_2_high'] = [ low[0], # black
high[2], # high green
high[4], # high red
high[6] ] # high yellow
palettes['cga_mode5'] = [ low[0], # black
low[3], # low cyan
low[4], # low red
low[7] ] # low white
palettes['cga_mode5_high'] = [ low[0], # black
high[3], # high cyan
high[4], # high red
high[7] ] # high white
def _build_ega_palettes():
global palettes
print('building ega palettes')
# generate all the low/dark colors
low = []
off_on = (0.0, 2./3.)
for r in off_on:
for g in off_on:
for b in off_on:
low.append([r, g, b])
# set brown
low[6][1] /= 2.
# generate all the high colors
high = []
off_on = (1./3., 1.0)
for r in off_on:
for g in off_on:
for b in off_on:
high.append([r, g, b])
palettes['ega_default'] = low + high # how convenient
def _build_palettes():
print('building palettes')
_build_grayscale_palettes()
_build_cga_palettes()
_build_ega_palettes()
_build_websafe_palettes()
_build_c64_palettes()
global palettes
with open('palettes.cache', 'w') as pf:
json.dump(palettes, pf)
global available_palettes
available_palettes = palettes.keys()
# check if a palette file exists
if os.access('palettes.cache', os.R_OK):
# check its mtime
me = os.path.realpath(__file__)
my_mtime = os.stat(me).st_mtime
cache_mtime = os.stat('palettes.cache').st_mtime
if my_mtime > cache_mtime:
# rebuild cache
_build_palettes()
else:
# read in the cache
with open('palettes.cache', 'r') as pf:
palettes = json.load(pf, object_pairs_hook=OrderedDict)
available_palettes = palettes.keys()
else:
_build_palettes()
if __name__ == '__main__':
print(available_palettes)
from PIL import Image, ImageDraw
width = max([len(palettes[p]) for p in palettes])*16
height = len(available_palettes) * 16
swatches = Image.new('RGB', (width, height))
draw = ImageDraw.Draw(swatches)
for p_i, pname in enumerate(available_palettes):
for c_i, color in enumerate(palettes[pname]):
pil_color = tuple([int(255*e) for e in color])
draw.rectangle([c_i*16, p_i*16, (c_i+1)*16, (p_i+1)*16],
fill=pil_color, outline=None)
del draw
swatches.show()