From d1c2980ca4c495867860855253ebd1a754a63ec6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Jul 2014 20:12:25 +0400 Subject: [PATCH 1/3] Add a way to omit computing colors for terminal --- tools/generate_gradients.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/generate_gradients.py b/tools/generate_gradients.py index 9441f92f..807f0338 100755 --- a/tools/generate_gradients.py +++ b/tools/generate_gradients.py @@ -53,6 +53,7 @@ p.add_argument('-r', '--range', metavar='V1 V2', type=num2, help='Use this range p.add_argument('-s', '--show', action='store_true', help='If present output gradient sample') p.add_argument('-p', '--palette', choices=('16', '256'), help='Use this palette. Defaults to 240-color palette (256 colors without first 16)') p.add_argument('-w', '--weights', metavar='INT INT ...', type=nums, help='Adjust weights of colors. Number of weights must be equal to number of colors') +p.add_argument('-C', '--omit-terminal', action='store_true', help='If present do not compute values for terminal') args = p.parse_args() @@ -156,16 +157,19 @@ palettes = { None: (cterm_to_lab[16:], lambda c: c + 16), } r = [get_rgb(lab) for lab in gradient] -r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient] -r3 = [i[0] for i in groupby(r2)] +if not args.omit_terminal: + r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient] + r3 = [i[0] for i in groupby(r2)] print(json.dumps(r)) -print(json.dumps(r2)) -print(json.dumps(r3)) +if not args.omit_terminal: + print(json.dumps(r2)) + print(json.dumps(r3)) if args.show: print_colors(args.gradient, args.num_output) print_colors(gradient, args.num_output) - print_colors(r2, args.num_output) - print_colors(r3, args.num_output) + if not args.omit_terminal: + print_colors(r2, args.num_output) + print_colors(r3, args.num_output) if not args.range and args.num_output >= 32 and (args.num_output - 1) // 10 >= 4 and (args.num_output - 1) % 10 == 0: sys.stdout.write('0') sys.stdout.write(''.join(('%*u' % (args.num_output // 10, i) for i in range(10, 101, 10)))) From ca0a8a2659702f9820f28b38c01bc9fc971db916 Mon Sep 17 00:00:00 2001 From: ZyX Date: Mon, 7 Jul 2014 20:22:07 +0400 Subject: [PATCH 2/3] Refactor generate_gradients.py --- tools/generate_gradients.py | 118 ++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 51 deletions(-) diff --git a/tools/generate_gradients.py b/tools/generate_gradients.py index 807f0338..d98ef90e 100755 --- a/tools/generate_gradients.py +++ b/tools/generate_gradients.py @@ -45,19 +45,6 @@ def nums(s): return [int(i) for i in s.split()] -p = argparse.ArgumentParser(description=__doc__) -p.add_argument('gradient', nargs='*', metavar='COLOR', type=color, help='List of colors (either indexes from 8-bit palette or 24-bit RGB in hexadecimal notation)') -p.add_argument('-n', '--num_items', metavar='INT', type=int, help='Number of items in resulting list', default=101) -p.add_argument('-N', '--num_output', metavar='INT', type=int, help='Number of characters in sample', default=101) -p.add_argument('-r', '--range', metavar='V1 V2', type=num2, help='Use this range when outputting scale') -p.add_argument('-s', '--show', action='store_true', help='If present output gradient sample') -p.add_argument('-p', '--palette', choices=('16', '256'), help='Use this palette. Defaults to 240-color palette (256 colors without first 16)') -p.add_argument('-w', '--weights', metavar='INT INT ...', type=nums, help='Adjust weights of colors. Number of weights must be equal to number of colors') -p.add_argument('-C', '--omit-terminal', action='store_true', help='If present do not compute values for terminal') - -args = p.parse_args() - - def linear_gradient(start_value, stop_value, start_offset, stop_offset, offset): return start_value + ((offset - start_offset) * (stop_value - start_value) / (stop_offset - start_offset)) @@ -133,62 +120,91 @@ def dec_scale_generator(num): return r -m = args.num_items +def compute_steps(gradient, weights): + maxweight = len(gradient) - 1 + if weights: + weight_sum = sum(weights) + norm_weights = [100.0 * weight / weight_sum for weight in weights] + steps = [0] + for weight in norm_weights: + steps.append(steps[-1] + weight) + steps.pop(0) + steps.pop(0) + else: + step = m / maxweight + steps = [i * step for i in range(1, maxweight + 1)] + return steps -maxweight = len(args.gradient) - 1 -if args.weights: - weight_sum = sum(args.weights) - norm_weights = [100.0 * weight / weight_sum for weight in args.weights] - steps = [0] - for weight in norm_weights: - steps.append(steps[-1] + weight) - steps.pop(0) - steps.pop(0) -else: - step = m / maxweight - steps = [i * step for i in range(1, maxweight + 1)] -data = [(weight, args.gradient[i - 1], args.gradient[i]) for weight, i in zip(steps, range(1, len(args.gradient)))] -gr_func = generate_gradient_function(data) -gradient = [gr_func(y) for y in range(0, m)] palettes = { '16': (cterm_to_lab[:16], lambda c: c), '256': (cterm_to_lab, lambda c: c), None: (cterm_to_lab[16:], lambda c: c + 16), } -r = [get_rgb(lab) for lab in gradient] -if not args.omit_terminal: - r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient] - r3 = [i[0] for i in groupby(r2)] -print(json.dumps(r)) -if not args.omit_terminal: - print(json.dumps(r2)) - print(json.dumps(r3)) -if args.show: - print_colors(args.gradient, args.num_output) - print_colors(gradient, args.num_output) - if not args.omit_terminal: - print_colors(r2, args.num_output) - print_colors(r3, args.num_output) - if not args.range and args.num_output >= 32 and (args.num_output - 1) // 10 >= 4 and (args.num_output - 1) % 10 == 0: + + +def show_scale(rng, num_output): + if not rng and num_output >= 32 and (num_output - 1) // 10 >= 4 and (num_output - 1) % 10 == 0: sys.stdout.write('0') - sys.stdout.write(''.join(('%*u' % (args.num_output // 10, i) for i in range(10, 101, 10)))) + sys.stdout.write(''.join(('%*u' % (num_output // 10, i) for i in range(10, 101, 10)))) sys.stdout.write('\n') else: - if args.range: - vmin, vmax = args.range[1] - isint = args.range[0] + if rng: + vmin, vmax = rng[1] + isint = rng[0] else: isint = True vmin = 0 vmax = 100 s = '' lasts = ' ' + str(vmax) - while len(s) + len(lasts) < args.num_output: + while len(s) + len(lasts) < num_output: curpc = len(s) + 1 if s else 0 - curval = vmin + curpc * (vmax - vmin) / args.num_output + curval = vmin + curpc * (vmax - vmin) / num_output if isint: curval = int(round(curval)) s += str(curval) + ' ' sys.stdout.write(s[:-1] + lasts + '\n') - sys.stdout.write(dec_scale_generator(args.num_output) + '\n') + sys.stdout.write(dec_scale_generator(num_output) + '\n') + + +if __name__ == '__main__': + p = argparse.ArgumentParser(description=__doc__) + p.add_argument('gradient', nargs='*', metavar='COLOR', type=color, help='List of colors (either indexes from 8-bit palette or 24-bit RGB in hexadecimal notation)') + p.add_argument('-n', '--num_items', metavar='INT', type=int, help='Number of items in resulting list', default=101) + p.add_argument('-N', '--num_output', metavar='INT', type=int, help='Number of characters in sample', default=101) + p.add_argument('-r', '--range', metavar='V1 V2', type=num2, help='Use this range when outputting scale') + p.add_argument('-s', '--show', action='store_true', help='If present output gradient sample') + p.add_argument('-p', '--palette', choices=('16', '256'), help='Use this palette. Defaults to 240-color palette (256 colors without first 16)') + p.add_argument('-w', '--weights', metavar='INT INT ...', type=nums, help='Adjust weights of colors. Number of weights must be equal to number of colors') + p.add_argument('-C', '--omit-terminal', action='store_true', help='If present do not compute values for terminal') + + args = p.parse_args() + + m = args.num_items + + steps = compute_steps(args.gradient, args.weights) + + data = [(weight, args.gradient[i - 1], args.gradient[i]) + for weight, i in zip(steps, range(1, len(args.gradient)))] + gr_func = generate_gradient_function(data) + gradient = [gr_func(y) for y in range(0, m)] + + r = [get_rgb(lab) for lab in gradient] + if not args.omit_terminal: + r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient] + r3 = [i[0] for i in groupby(r2)] + + print(json.dumps(r)) + if not args.omit_terminal: + print(json.dumps(r2)) + print(json.dumps(r3)) + + if args.show: + print_colors(args.gradient, args.num_output) + print_colors(gradient, args.num_output) + if not args.omit_terminal: + print_colors(r2, args.num_output) + print_colors(r3, args.num_output) + + show_scale(args.range, args.num_output) From a68e01ac24c5a4ddf7f3918c58c9b54d51ceffc7 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 8 Jul 2014 18:12:54 +0400 Subject: [PATCH 3/3] Reverse JSON output and add trailing comma Makes it easier to copy-paste output to colors.json. --- tools/generate_gradients.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tools/generate_gradients.py b/tools/generate_gradients.py index d98ef90e..a667f301 100755 --- a/tools/generate_gradients.py +++ b/tools/generate_gradients.py @@ -2,7 +2,7 @@ # vim:fileencoding=utf-8:noet '''Gradients generator ''' -from __future__ import division +from __future__ import division, unicode_literals import sys import json from powerline.colorscheme import cterm_to_hex @@ -195,16 +195,16 @@ if __name__ == '__main__': r2 = [find_color(lab, *palettes[args.palette])[0] for lab in gradient] r3 = [i[0] for i in groupby(r2)] - print(json.dumps(r)) if not args.omit_terminal: - print(json.dumps(r2)) - print(json.dumps(r3)) + print(json.dumps(r3) + ',') + print(json.dumps(r2) + ',') + print(json.dumps(r)) if args.show: print_colors(args.gradient, args.num_output) - print_colors(gradient, args.num_output) if not args.omit_terminal: - print_colors(r2, args.num_output) print_colors(r3, args.num_output) + print_colors(r2, args.num_output) + print_colors(gradient, args.num_output) show_scale(args.range, args.num_output)