colors.py

by mattrobenolt

mattrobenolt / colors.py

Colors aren't that scary!

213 Stars 20 Forks Last release: Not found BSD 2-Clause "Simplified" License 30 Commits 4 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

colors.py

Convert colors between rgb, hsv, and hex, perform arithmetic, blend modes, and generate random colors within boundaries

Installation

$ pip install colors.py

Basic Uses

Importing

>>> from colors import rgb, hsv, hex, random

Create an RGB color object

>>> rgb(100, 100, 100)

Convert it to hexadecimal

>>> rgb(100, 100, 100).hex

Coerce the hexadecimal to a normal string

>>> str(rgb(100, 100, 100).hex)
646464

Create a Hexadecimal color object

>>> hex('646464')

Extract the red/green/blue value from a hexadecimal

>>> hex('646464').rgb.red
100

Convert a hexadecimal to HSV

>>> hex('646464').hsv

Coerce hsv/rgb values to a list/tuple of values

>>> list(hex('646464').hsv)
[0.0, 0.0, 0.39215686274509803]

Create an HSV color object

>>> hsv(0, 1, 1)

Convert it to RGB

>>> hsv(0, 1, 1).rgb

Gimme a random color, any color!

>>> random()

Coerce a hexadecimal color to a string with formatting

>>> '#%s' % random().hex
'#2f2336'

Coerce RGB/HSV objects to a string for formatting

>>> 'style="color: rgb(%s)"' % random().rgb
'style="color: rgb(80.3414147839, 124.403236079, 71.4620739603)"'

Compare color equality

>>> rgb(100, 100, 100) == hex('646464')
True
>>> hsv(0, 1, 1) == rgb(255, 0, 0)
True

Arithmetic

Note: All arithmetic operations return

rgb
color.

Multiply

>>> hex('ff9999') * hex('cccccc')

>>> _.hex

>>> rgb(100, 100, 100).multiply(hsv(0, 1, 1)).hex
>>> 

Add

>>> hex('ff9999') + rgb(10, 10, 10)

>>> hex('aaffcc').add(rgb(10, 10, 10))

Subtract

>>> hex('ff9999') - rgb(10, 10, 10)

>>> hex('aaffcc').subtract(rgb(10, 10, 10))

Divide

>>> hex('ff9999') / rgb(10, 10, 10)

>>> hex('aaffcc').divide(rgb(10, 10, 10))

>>> rgb(100, 100, 100) / hex('00ffff')
Traceback (most recent call last):
  File "", line 1, in 
  File "colors.py", line 73, in divide
    raise ZeroDivisionError
ZeroDivisionError

Blend Modes

Note: All blend modes return

rgb
color.

Screen

>>> hex('ff9999').screen(rgb(10, 10, 10)).hex

Difference

>>> hex('ff9999').difference(rgb(10, 10, 10)).hex

Overlay

>>> hex('ff9999').overlay(rgb(10, 10, 10)).hex

Invert

>>> hex('000000').invert()

Color palettes

colors.py
current ships with three color palettes full of constants. See source for all available colors.

colors.primary

>>> import colors.primary
>>> colors.primary.red

colors.rainbow

>>> import colors.rainbow
>>> colors.rainbow.indigo

colors.w3c

>>> import colors.w3c
>>> colors.w3c.ghostwhite

The Color Wheel!

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. ```python

from colors import ColorWheel wheel = ColorWheel() ```

Iterate the wheel to get the next value

ColorWheel is an iterable, but be careful if using inside any type of loop. It will iterate forever until you interject. ```python wheel.next() wheel.next() for color in wheel: ... print color.hex 00cca4 002ecc

Forever and ever and ever and ever


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.