PaletteOptimiser

Change of optimiser.py

36:7437f3302bc8
optimiser.py
     1.1 --- a/optimiser.py	Sat Oct 03 19:08:11 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 03 19:53:13 2015 +0200
     1.3 @@ -47,13 +47,35 @@
     1.4      return rgb, restore(invert(scale(rgb)))
     1.5  
     1.6  def balance(d):
     1.7 +
     1.8 +    # Find the dominant complementary pair.
     1.9 +
    1.10 +    d.sort(reverse=True)
    1.11 +    found = set()
    1.12 +    for f, value in d:
    1.13 +        value, complement = complements(value)
    1.14 +        if value in found or complement in found:
    1.15 +            found = [value, complement]
    1.16 +            break
    1.17 +        else:
    1.18 +            found.add(value)
    1.19 +
    1.20 +    # Remove the dominant primary (or black) from the list of colours.
    1.21 +
    1.22 +    colours = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]
    1.23 +    if found[0] in colours:
    1.24 +        colours.remove(found[0])
    1.25 +    else:
    1.26 +        colours.remove(found[1])
    1.27 +
    1.28      d = dict([(value, f) for f, value in d])
    1.29 -    for primary, secondary in map(complements, [(255, 0, 0), (0, 255, 0), (0, 0, 255)]):
    1.30 +
    1.31 +    for primary, secondary in map(complements, colours):
    1.32          common = min(d[primary], d[secondary])
    1.33          d[primary] -= common
    1.34          d[secondary] -= common
    1.35 -        d[(0, 0, 0)] += common
    1.36 -        d[(255, 255, 255)] += common
    1.37 +        d[found[0]] += common
    1.38 +        d[found[1]] += common
    1.39      return [(f, value) for value, f in d.items()]
    1.40  
    1.41  def combine(d):