Make generate_gradients tool be able to show custom scale

This commit is contained in:
ZyX 2013-03-19 08:17:11 +04:00
parent 11aa74c634
commit da8647895a
1 changed files with 26 additions and 7 deletions

View File

@ -6,7 +6,7 @@ from itertools import groupby
if len(sys.argv) == 1:
sys.stderr.write('''
Usage: generate_gradients.py colors itemnum[ "show"]
Usage: generate_gradients.py colors itemnum[ "show" [ min max num]]
colors: JSON list with either cterm ([200, 42, 6]) or RGB (["abcdef",
"feffef"]) colors.
@ -15,6 +15,14 @@ if len(sys.argv) == 1:
"show": static string, determines whether gradient sample should be
printed to stdout as well.
min, max: floating point values.
num: integer
all of the above are used to generate sample gradient for given
range (just like the above gradients shown with "show", but with
different scale (controlled by min and max) and, possibly,
different length (controlled by num)).
''')
@ -73,9 +81,9 @@ def print_color(color):
sys.stdout.write('\033[48;' + colstr + 'm ')
def print_colors(colors):
for i in range(101):
color = colors[int(round(i * (len(colors) - 1) / 100))]
def print_colors(colors, num=100):
for i in range(num + 1):
color = colors[int(round(i * (len(colors) - 1) / num))]
print_color(color)
sys.stdout.write('\033[0m\n')
@ -90,9 +98,9 @@ gradient = [gr_func(y) for y in range(0, m - 1)]
r = [get_rgb(*color) for color in gradient]
r2 = [find_color(color, cterm_to_hex)[0] for color in gradient]
r3 = [i[0] for i in groupby(r2)]
print json.dumps(r)
print json.dumps(r2)
print json.dumps(r3)
print(json.dumps(r))
print(json.dumps(r2))
print(json.dumps(r3))
if len(sys.argv) > 3 and sys.argv[3] == 'show':
print_colors(gradient)
print_colors(r2)
@ -103,3 +111,14 @@ if len(sys.argv) > 3 and sys.argv[3] == 'show':
nums = (''.join((str(i) for i in range(10))))
sys.stdout.write(''.join(((('\033[1m' if j % 2 else '\033[0m') + nums) for j in range(10))))
sys.stdout.write('\033[0m0\n')
if len(sys.argv) > 6:
vmin = float(sys.argv[4])
vmax = float(sys.argv[5])
num = int(sys.argv[6])
print_colors(gradient, num)
s = ''
while len(s) < num:
curpc = len(s) + 1 if s else 0
curval = vmin + curpc * (vmax - vmin) / 100.0
s += str(curval) + ' '
print(s)