1.1 --- a/optimiserlib.py Sat Oct 10 15:36:48 2015 +0200
1.2 +++ b/optimiserlib.py Sat Oct 10 15:59:38 2015 +0200
1.3 @@ -21,6 +21,7 @@
1.4 """
1.5
1.6 from random import random, randrange
1.7 +import itertools
1.8
1.9 corners = [
1.10 (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0),
1.11 @@ -172,6 +173,24 @@
1.12 def amplify_value(x, exp):
1.13 return int(pow(x / 255.0, exp) * 255.0)
1.14
1.15 +# Image operations.
1.16 +
1.17 +def get_combinations(c, n):
1.18 +
1.19 + """
1.20 + Get combinations of colours from 'c' of size 'n' in decreasing order of
1.21 + probability.
1.22 + """
1.23 +
1.24 + all = []
1.25 + for l in itertools.combinations(c, n):
1.26 + total = 0
1.27 + for f, value in l:
1.28 + total += f
1.29 + all.append((total, l))
1.30 + all.sort(reverse=True)
1.31 + return [l for total, l in all]
1.32 +
1.33 # Exercise functions for Shedskin.
1.34
1.35 if __name__ == "__main__":
1.36 @@ -182,5 +201,6 @@
1.37 get_value(rgb, [(255, 255, 255), (255, 0, 0), (255, 255, 0), (0, 0, 0)])
1.38 combine([(1.0, (255, 0, 0)), (0.0, (0, 0, 0))])
1.39 clip(200.0)
1.40 + get_combinations([(0.5, (255, 0, 0)), (0.25, (255, 255, 0)), (0.25, (0, 0, 0))], 2)
1.41
1.42 # vim: tabstop=4 expandtab shiftwidth=4