-
Notifications
You must be signed in to change notification settings - Fork 53
/
util.py
executable file
·175 lines (153 loc) · 5.84 KB
/
util.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
import re
import argparse
import glob
from braceexpand import braceexpand
from codecs import encode
try:
import matplotlib.colors
except ImportError:
# only needed for palette stuff
pass
# file helpers
def real_glob(rglob):
glob_list = braceexpand(rglob)
files = []
for g in glob_list:
files = files + glob.glob(g)
return sorted(files)
####### argparse bools ##########
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
####### PALETTE SECTION ##########
# canonical interpolation function, like https://p5js.org/reference/#/p5/map
def map_number(n, start1, stop1, start2, stop2):
return ((n-start1)/(stop1-start1))*(stop2-start2)+start2;
# parses either (255,255,0) or [1,1,0] as yellow, etc
def parse_triple_to_rgb(s):
s2 = re.sub(r'[(\[\])]', '', s)
t = s2.split("+")
rgb = [float(n) for n in t]
if s[0] == "(":
rgb = [n / 255.0 for n in rgb]
return rgb
# here are examples of what can be parsed
# white (16 color black to white ramp)
# red (16 color black to red ramp)
# rust\8 (8 color black to rust ramp)
# red->rust (16 color red to rust ramp)
# red->#ff0000 (16 color red to yellow ramp)
# red->#ff0000\20 (20 color red to yellow ramp)
# black->red->white (16 color black/red/white ramp)
# [black, red, #ff0000] (three colors)
# red->white;blue->yellow (32 colors across two ramps of 16)
# red;blue;yellow (48 colors from combining 3 ramps)
# red\8;blue->yellow\8 (16 colors from combining 2 ramps)
# red->yellow;[black] (16 colors from ramp and also black)
#
# TODO: maybe foo.jpg, foo.json, foo.png, foo.asc
def get_single_rgb(s):
palette_lookups = {
"pixel_green": [0.44, 1.00, 0.53],
"pixel_orange": [1.00, 0.80, 0.20],
"pixel_blue": [0.44, 0.53, 1.00],
"pixel_red": [1.00, 0.53, 0.44],
"pixel_grayscale": [1.00, 1.00, 1.00],
}
if s[0] == "(" or s[0] == "[":
rgb = parse_triple_to_rgb(s)
elif s in palette_lookups:
rgb = palette_lookups[s]
elif s[:4] == "mat:":
rgb = matplotlib.colors.to_rgb(s[4:])
elif matplotlib.colors.is_color_like(f"xkcd:{s}"):
rgb = matplotlib.colors.to_rgb(f"xkcd:{s}")
else:
rgb = matplotlib.colors.to_rgb(s)
return rgb
def expand_colors(colors, num_steps):
index_episilon = 1e-6;
pal = []
num_colors = len(colors)
for n in range(num_steps):
cur_float_index = map_number(n, 0, num_steps-1, 0, num_colors-1)
cur_int_index = int(cur_float_index)
cur_float_offset = cur_float_index - cur_int_index
if(cur_float_offset < index_episilon or (1.0-cur_float_offset) < index_episilon):
# debug print(n, "->", cur_int_index)
pal.append(colors[cur_int_index])
else:
# debug print(n, num_steps, num_colors, cur_float_index, cur_int_index, cur_float_offset)
rgb1 = colors[cur_int_index]
rgb2 = colors[cur_int_index+1]
r = map_number(cur_float_offset, 0, 1, rgb1[0], rgb2[0])
g = map_number(cur_float_offset, 0, 1, rgb1[1], rgb2[1])
b = map_number(cur_float_offset, 0, 1, rgb1[2], rgb2[2])
pal.append([r, g, b])
return pal
def get_rgb_range(s):
# get the list that defines the range
if s.find('->') > 0:
parts = s.split('->')
else:
parts = ["black", s]
# look for a number of parts at the end
if parts[-1].find('\\') > 0:
colname, steps = parts[-1].split('\\')
parts[-1] = colname
num_steps = int(steps)
else:
num_steps = 16
colors = [get_single_rgb(s) for s in parts]
#debug print("We have colors: ", colors)
pal = expand_colors(colors, num_steps)
return pal
def palette_from_section(s):
s = s.strip()
if s[0] == '[':
# look for a number of parts at the end
if s.find('\\') > 0:
col_list, steps = s.split('\\')
s = col_list
num_steps = int(steps)
else:
num_steps = None
chunks = s[1:-1].split(",")
# chunks = [s.strip().tolower() for c in chunks]
pal = [get_single_rgb(c.strip()) for c in chunks]
if num_steps is not None:
pal = expand_colors(pal, num_steps)
return pal
elif s[0] == '@':
# pull from a file or URL
if s.endswith(".act"):
# https://stackoverflow.com/a/48873783/1010653
with open(s[1:], 'rb') as act:
raw_data = act.read() # Read binary data
hex_data = encode(raw_data, 'hex') # Convert it to hexadecimal values
total_colors_count = (int(hex_data[-7:-4], 16)) # Get last 3 digits to get number of colors total
misterious_count = (int(hex_data[-4:-3], 16)) # I have no idea what does it do
colors_count = (int(hex_data[-3:], 16)) # Get last 3 digits to get number of nontransparent colors
# Decode colors from hex to string and split it by 6 (because colors are #1c1c1c)
colors = [hex_data[i:i+6].decode() for i in range(0, total_colors_count*6, 6)]
# Add # to each item and filter empty items if there is a corrupted total_colors_count bit
colors = ['#'+i for i in colors if len(i)]
colors = [get_single_rgb(s) for s in colors]
return colors
else:
raise ValueError(f'Unknown file type: {s}')
else:
return get_rgb_range(s)
def palette_from_string(s):
s = s.strip()
pal = []
chunks = s.split(';')
for c in chunks:
pal = pal + palette_from_section(c)
return pal