PaletteOptimiser

Changeset

73:f789f95ee656
2015-10-10 Paul Boddie raw files shortlog changelog graph Simplified further, raising an exception in get_value instead of returning None. shedskin
optimiser.py (file)
     1.1 --- a/optimiser.py	Sat Oct 10 00:05:54 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 00:14:25 2015 +0200
     1.3 @@ -141,15 +141,18 @@
     1.4  
     1.5      """
     1.6      Get an output colour for 'rgb', optionally limited to any specified 'chosen'
     1.7 -    colours. If 'fail' is set to a true value, return None if the colour cannot
     1.8 -    be expressed using any of the chosen colours.
     1.9 +    colours. If 'fail' is set to a true value, raise ValueError if the colour
    1.10 +    cannot be expressed using any of the chosen colours.
    1.11      """
    1.12  
    1.13      l = pattern(rgb, chosen)
    1.14 -    limit = sum([f for f, c in l])
    1.15 +    limit = 0
    1.16 +    for f, c in l:
    1.17 +        limit += f
    1.18 +
    1.19      if not limit:
    1.20          if fail:
    1.21 -            return None
    1.22 +            raise ValueError
    1.23          else:
    1.24              return l[randrange(0, len(l))][1]
    1.25  
    1.26 @@ -264,10 +267,14 @@
    1.27          for y in range(0, height):
    1.28              for x in range(0, width):
    1.29                  rgb = im.getpixel((x, y))
    1.30 -                if saturate or desaturate:
    1.31 -                    rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2 * desaturate)
    1.32 -                if darken or brighten:
    1.33 -                    rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken)
    1.34 +                if saturate:
    1.35 +                    rgb = saturate_rgb(rgb, 0.5 / saturate)
    1.36 +                elif desaturate:
    1.37 +                    rgb = saturate_rgb(rgb, 2 * desaturate)
    1.38 +                if darken:
    1.39 +                    rgb = amplify_rgb(rgb, 2 * darken)
    1.40 +                elif brighten:
    1.41 +                    rgb = amplify_rgb(rgb, 0.5 / brighten)
    1.42                  im.putpixel((x, y), rgb)
    1.43  
    1.44  def preview_image(im, half_resolution_preview=False):
    1.45 @@ -299,16 +306,21 @@
    1.46          c = get_colours(im, y)
    1.47  
    1.48          for l in get_combinations(c, 4):
    1.49 -            most = [value for f, value in l]
    1.50 +            most = []
    1.51 +            for f, value in l:
    1.52 +                most.append(value)
    1.53              for x in range(0, width):
    1.54                  rgb = im.getpixel((x, y))
    1.55 -                value = get_value(rgb, most, True)
    1.56 -                if value is None:
    1.57 +                try:
    1.58 +                    value = get_value(rgb, most, True)
    1.59 +                except ValueError:
    1.60                      break # try next combination
    1.61              else:
    1.62                  break # use this combination
    1.63          else:
    1.64 -            most = [value for f, value in c[:4]] # use the first four
    1.65 +            most = []
    1.66 +            for f, value in c[:4]: # use the first four
    1.67 +                most.append(value)
    1.68  
    1.69          for x in range(0, width):
    1.70              rgb = im.getpixel((x, y))