Package Vision :: Module colours
[hide private]
[frames] | no frames]

Source Code for Module Vision.colours

 1  """ 
 2  Some simple functions to generate colours. 
 3  """ 
 4  from matplotlib.numerix import asarray, asum 
 5  from matplotlib.mlab import linspace 
 6  from matplotlib.colors import colorConverter 
 7  from matplotlib.numerix import sum 
 8   
9 -def pastel(colour, weight=2.4):
10 """ Convert colour into a nice pastel shade""" 11 rgb = asarray(colorConverter.to_rgb(colour)) 12 # scale colour 13 maxc = max(rgb) 14 if maxc < 1.0 and maxc > 0: 15 # scale colour 16 scale = 1.0 / maxc 17 rgb = rgb * scale 18 # now decrease saturation 19 total = asum(rgb) 20 slack = 0 21 for x in rgb: 22 slack += 1.0 - x 23 24 # want to increase weight from total to weight 25 # pick x s.t. slack * x == weight - total 26 # x = (weight - total) / slack 27 x = (weight - total) / slack 28 29 rgb = [c + (x * (1.0-c)) for c in rgb] 30 31 return rgb
32
33 -def get_colours(n):
34 """ Return n pastel colours. """ 35 base = asarray([[1,0,0], [0,1,0], [0,0,1]]) 36 37 if n <= 3: 38 return base[0:n] 39 40 # how many new colours to we need to insert between 41 # red and green and between green and blue? 42 needed = (((n - 3) + 1) / 2, (n - 3) / 2) 43 44 colours = [] 45 for start in (0, 1): 46 for x in linspace(0, 1, needed[start]+2): 47 colours.append((base[start] * (1.0 - x)) + 48 (base[start+1] * x)) 49 50 return [pastel(c) for c in colours[0:n]]
51