Package SmootLight :: Package util :: Module ColorOps
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.util.ColorOps

 1  import random 
 2  import colorsys 
 3  from util.TimeOps import Stopwatch 
4 -def randomColor():
5 return [random.randint(0,255) for i in range(3)]
6
7 -def chooseRandomColor(colorList):
8 """Given a list of colors, pick one at random""" 9 return random.choice(colorList)
10 -def safeColor(c):
11 """Ensures that a color is valid""" 12 c[0] = c[0] if c[0] < 255 else 255 13 c[1] = c[1] if c[1] < 255 else 255 14 c[2] = c[2] if c[2] < 255 else 255 15 16 17 return c
18
19 -def combineColors(colors):
20 result = [0,0,0] 21 for c in colors: 22 result[0] += c[0] 23 result[1] += c[1] 24 result[2] += c[2] 25 return safeColor(result)
26
27 -def multiplyColor(color, percent):
28 return safeColor([channel*(percent) for channel in color])
29
30 -def floatToIntColor(rgb):
31 rgb[0] = int(rgb[0]*256 + .5) 32 rgb[1] = int(rgb[1]*256 + .5) 33 rgb[2] = int(rgb[2]*256 + .5) 34 return safeColor(rgb)
35
36 -def randomBrightColor():
37 hue = random.random() 38 sat = random.random()/2.0 + .5 39 val = 1.0 40 hue, sat, val = colorsys.hsv_to_rgb(hue, sat, val) 41 ret = [hue, sat, val] 42 return floatToIntColor(ret)
43
44 -class Color(object):
45 - def __init__(self, r,g,b):
46 self.rep = [r,g,b]
47