PaletteOptimiser

Changeset

72:92c6d7aa9343
2015-10-10 Paul Boddie raw files shortlog changelog graph Used more explicit coding practices to help Shedskin. shedskin
optimiser.py (file)
     1.1 --- a/optimiser.py	Fri Oct 09 23:48:21 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 00:05:54 2015 +0200
     1.3 @@ -44,13 +44,16 @@
     1.4      return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2))
     1.5  
     1.6  def restore(srgb):
     1.7 -    return tuple(map(lambda x: int(x * 255.0), srgb))
     1.8 +    r, g, b = srgb
     1.9 +    return int(r * 255.0), int(g * 255.0), int(b * 255.0)
    1.10  
    1.11  def scale(rgb):
    1.12 -    return tuple(map(lambda x: x / 255.0, rgb))
    1.13 +    r, g, b = rgb
    1.14 +    return r / 255.0, g / 255.0, b / 255.0
    1.15  
    1.16  def invert(srgb):
    1.17 -    return tuple(map(lambda x: 1.0 - x, srgb))
    1.18 +    r, g, b = srgb
    1.19 +    return 1.0 - r, 1.0 - g, 1.0 - b
    1.20  
    1.21  # Colour distribution functions.
    1.22  
    1.23 @@ -96,12 +99,18 @@
    1.24      and replacing their common contributions with black and white contributions.
    1.25      """
    1.26  
    1.27 -    d = dict([(value, f) for f, value in d])
    1.28 -    for primary, secondary in map(complements, [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]):
    1.29 -        common = min(d[primary], d[secondary])
    1.30 -        d[primary] -= common
    1.31 -        d[secondary] -= common
    1.32 -    return [(f, value) for value, f in d.items()]
    1.33 +    dd = {}
    1.34 +    for f, value in d:
    1.35 +        dd[value] = f
    1.36 +    for colour in [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]:
    1.37 +        primary, secondary = complements(colour)
    1.38 +        common = min(dd[primary], dd[secondary])
    1.39 +        dd[primary] -= common
    1.40 +        dd[secondary] -= common
    1.41 +    d = []
    1.42 +    for value, f in dd.items():
    1.43 +        d.append((f, value))
    1.44 +    return d
    1.45  
    1.46  def combine(d):
    1.47  
    1.48 @@ -121,7 +130,10 @@
    1.49      specified 'chosen' colours.
    1.50      """
    1.51  
    1.52 -    l = [(f, value) for f, value in combination(rgb) if not chosen or value in chosen]
    1.53 +    l = []
    1.54 +    for f, value in combination(rgb):
    1.55 +        if not chosen or value in chosen:
    1.56 +            l.append((f, value))
    1.57      l.sort(reverse=True)
    1.58      return l
    1.59  
    1.60 @@ -155,13 +167,15 @@
    1.61      return x >= 0 and 1 or -1
    1.62  
    1.63  def saturate_rgb(rgb, exp):
    1.64 -    return tuple([saturate_value(x, exp) for x in rgb])
    1.65 +    r, g, b = rgb
    1.66 +    return saturate_value(r, exp), saturate_value(g, exp), saturate_value(b, exp)
    1.67  
    1.68  def saturate_value(x, exp):
    1.69      return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp))
    1.70  
    1.71  def amplify_rgb(rgb, exp):
    1.72 -    return tuple([amplify_value(x, exp) for x in rgb])
    1.73 +    r, g, b = rgb
    1.74 +    return amplify_value(r, exp), amplify_value(g, exp), amplify_value(b, exp)
    1.75  
    1.76  def amplify_value(x, exp):
    1.77      return int(pow(x / 255.0, exp) * 255.0)
    1.78 @@ -185,9 +199,11 @@
    1.79              else:
    1.80                  c[value] += f
    1.81  
    1.82 -    c = [(n/width, value) for value, n in c.items()]
    1.83 -    c.sort(reverse=True)
    1.84 -    return c
    1.85 +    d = []
    1.86 +    for value, n in c.items():
    1.87 +        d.append((n/width, value))
    1.88 +    d.sort(reverse=True)
    1.89 +    return d
    1.90  
    1.91  def get_combinations(c, n):
    1.92  
    1.93 @@ -203,7 +219,10 @@
    1.94              total += f
    1.95          all.append((total, l))
    1.96      all.sort(reverse=True)
    1.97 -    return [l for total, l in all]
    1.98 +    cc = []
    1.99 +    for total, l in all:
   1.100 +        cc.append(l)
   1.101 +    return cc
   1.102  
   1.103  def test_slice(im, size, r):
   1.104      for g in range(0, size):
   1.105 @@ -298,12 +317,20 @@
   1.106  
   1.107              if x < width - 1:
   1.108                  rgbn = im.getpixel((x+1, y))
   1.109 -                rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 4.0), zip(rgbn, rgb, value)))
   1.110 +                rgbn = (
   1.111 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
   1.112 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
   1.113 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
   1.114 +                    )
   1.115                  im.putpixel((x+1, y), rgbn)
   1.116  
   1.117              if y < height - 1:
   1.118                  rgbn = im.getpixel((x, y+1))
   1.119 -                rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value)))
   1.120 +                rgbn = (
   1.121 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
   1.122 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
   1.123 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
   1.124 +                    )
   1.125                  im.putpixel((x, y+1), rgbn)
   1.126  
   1.127  class SimpleImage: