# HG changeset patch # User Paul Boddie # Date 1444428865 -7200 # Node ID f789f95ee65637b7660a60a98832a258ffe77af6 # Parent 92c6d7aa93436ea007001133246f440b2e1dc3e4 Simplified further, raising an exception in get_value instead of returning None. diff -r 92c6d7aa9343 -r f789f95ee656 optimiser.py --- a/optimiser.py Sat Oct 10 00:05:54 2015 +0200 +++ b/optimiser.py Sat Oct 10 00:14:25 2015 +0200 @@ -141,15 +141,18 @@ """ Get an output colour for 'rgb', optionally limited to any specified 'chosen' - colours. If 'fail' is set to a true value, return None if the colour cannot - be expressed using any of the chosen colours. + colours. If 'fail' is set to a true value, raise ValueError if the colour + cannot be expressed using any of the chosen colours. """ l = pattern(rgb, chosen) - limit = sum([f for f, c in l]) + limit = 0 + for f, c in l: + limit += f + if not limit: if fail: - return None + raise ValueError else: return l[randrange(0, len(l))][1] @@ -264,10 +267,14 @@ for y in range(0, height): for x in range(0, width): rgb = im.getpixel((x, y)) - if saturate or desaturate: - rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2 * desaturate) - if darken or brighten: - rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken) + if saturate: + rgb = saturate_rgb(rgb, 0.5 / saturate) + elif desaturate: + rgb = saturate_rgb(rgb, 2 * desaturate) + if darken: + rgb = amplify_rgb(rgb, 2 * darken) + elif brighten: + rgb = amplify_rgb(rgb, 0.5 / brighten) im.putpixel((x, y), rgb) def preview_image(im, half_resolution_preview=False): @@ -299,16 +306,21 @@ c = get_colours(im, y) for l in get_combinations(c, 4): - most = [value for f, value in l] + most = [] + for f, value in l: + most.append(value) for x in range(0, width): rgb = im.getpixel((x, y)) - value = get_value(rgb, most, True) - if value is None: + try: + value = get_value(rgb, most, True) + except ValueError: break # try next combination else: break # use this combination else: - most = [value for f, value in c[:4]] # use the first four + most = [] + for f, value in c[:4]: # use the first four + most.append(value) for x in range(0, width): rgb = im.getpixel((x, y))