PaletteOptimiser

Change of optimiser.py

33:da4132364cfd
optimiser.py
     1.1 --- a/optimiser.py	Sat Oct 03 00:32:58 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 03 01:32:26 2015 +0200
     1.3 @@ -52,6 +52,13 @@
     1.4  def saturate_value(x, exp):
     1.5      return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp))
     1.6  
     1.7 +def replace(value, values):
     1.8 +    if value not in values:
     1.9 +        for i, v in list(enumerate(values))[::-1]:
    1.10 +            if v != value:
    1.11 +                values[i] = value
    1.12 +                return
    1.13 +
    1.14  def test():
    1.15      size = 512
    1.16      for r in (0, 63, 127, 191, 255):
    1.17 @@ -135,6 +142,12 @@
    1.18          most = [value for n, value in c[:4]]
    1.19          least = [value for n, value in c[4:]]
    1.20  
    1.21 +        if least:
    1.22 +            if (0, 0, 0) in least[:2]:
    1.23 +                replace((0, 0, 0), most)
    1.24 +            if (255, 255, 255) in least[:2]:
    1.25 +                replace((255, 255, 255), most)
    1.26 +
    1.27          for x in range(0, width):
    1.28              rgb = im.getpixel((x, y))
    1.29  
    1.30 @@ -144,7 +157,7 @@
    1.31              value = get_value(rgb)
    1.32              if value in least:
    1.33                  rgb = im.getpixel((x, y))
    1.34 -                value = pattern(value, most)[0][1]
    1.35 +                value = get_value(value, most)
    1.36  
    1.37              im.putpixel((x, y), value)
    1.38