Convert colors between rgb, hsv, and hex, perform arithmetic, blend modes, and generate random colors within boundaries
$ pip install colors.py
>>> from colors import rgb, hsv, hex, random
>>> rgb(100, 100, 100)
<RGBColor red: 100, green: 100, blue: 100>
>>> rgb(100, 100, 100).hex
<HexColor red: 64, green: 64, blue: 64>
>>> str(rgb(100, 100, 100).hex)
646464
>>> hex('646464')
<HexColor red: 64, green: 64, blue: 64>
>>> hex('646464').rgb.red
100
>>> hex('646464').hsv
<HSVColor hue: 0.0, saturation: 0.0, value: 0.392156862745>
>>> list(hex('646464').hsv)
[0.0, 0.0, 0.39215686274509803]
>>> hsv(0, 1, 1)
<HSVColor hue: 0, saturation: 1, value: 1>
>>> hsv(0, 1, 1).rgb
<RGBColor red: 255, green: 0.0, blue: 0.0>
>>> random()
<HSVColor hue: 0.812436498638, saturation: 0.621033239007, value: 0.379850638405>
>>> '#%s' % random().hex
'#2f2336'
>>> 'style="color: rgb(%s)"' % random().rgb
'style="color: rgb(80.3414147839, 124.403236079, 71.4620739603)"'
>>> rgb(100, 100, 100) == hex('646464')
True
>>> hsv(0, 1, 1) == rgb(255, 0, 0)
True
Note: All arithmetic operations return rgb
color.
>>> hex('ff9999') * hex('cccccc')
<RGBColor red: 204.0, green: 122.4, blue: 122.4>
>>> _.hex
<HexColor red: cc, green: 7a, blue: 7a>
>>> rgb(100, 100, 100).multiply(hsv(0, 1, 1)).hex
>>> <HexColor red: 64, green: 00, blue: 00>
>>> hex('ff9999') + rgb(10, 10, 10)
<RGBColor red: 255, green: 163, blue: 163>
>>> hex('aaffcc').add(rgb(10, 10, 10))
<RGBColor red: 180, green: 255, blue: 214>
>>> hex('ff9999') - rgb(10, 10, 10)
<RGBColor red: 245, green: 143, blue: 143>
>>> hex('aaffcc').subtract(rgb(10, 10, 10))
<RGBColor red: 160, green: 245, blue: 194>
>>> hex('ff9999') / rgb(10, 10, 10)
<RGBColor red: 25.5, green: 15.3, blue: 15.3>
>>> hex('aaffcc').divide(rgb(10, 10, 10))
<RGBColor red: 17.0, green: 25.5, blue: 20.4>
>>> rgb(100, 100, 100) / hex('00ffff')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "colors.py", line 73, in divide
raise ZeroDivisionError
ZeroDivisionError
Note: All blend modes return rgb
color.
>>> hex('ff9999').screen(rgb(10, 10, 10)).hex
<HexColor red: ff, green: 9d, blue: 9d>
>>> hex('ff9999').difference(rgb(10, 10, 10)).hex
<HexColor red: f5, green: 8f, blue: 8f>
>>> hex('ff9999').overlay(rgb(10, 10, 10)).hex
<HexColor red: ff, green: 9b, blue: 9b>
>>> hex('000000').invert()
<RGBColor red: 255, green: 255, blue: 255>
colors.py
current ships with three color palettes full of constants. See source for all available colors.
>>> import colors.primary
>>> colors.primary.red
<RGBColor red: 255, green: 0, blue: 0>
>>> import colors.rainbow
>>> colors.rainbow.indigo
<RGBColor red: 75, green: 0, blue: 130>
>>> import colors.w3c
>>> colors.w3c.ghostwhite
<RGBColor red: 248, green: 248, blue: 255>
The color wheel allows you to randomly choose colors while keeping the colors relatively evenly distributed. Think generating random colors without pooling in one hue, e.g., not 50 green, and 1 red.
>>> from colors import ColorWheel
>>> wheel = ColorWheel()
ColorWheel is an iterable, but be careful if using inside any type of loop. It will iterate forever until you interject.
>>> wheel.next()
<HSVColor hue: 0.177410230076, saturation: 1, value: 0.8>
>>> wheel.next()
<HSVColor hue: 0.278803914372, saturation: 1, value: 0.8>
>>> for color in wheel:
... print color.hex
00cca4
002ecc
# Forever and ever and ever and ever