Need help with colors.py?
Click the βchatβ button below for chat support from the developer who created it, or find similar developers for support.

mattrobenolt
227 Stars 23 Forks BSD 2-Clause "Simplified" License 30 Commits 6 Opened issues

#### Description

Colors aren't that scary!

!
?

# 2,572
Applica...
error-m...
sentry
sentry-...
26 commits
# 6,716
Python
Shell
jinja
2 commits

# 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)

```

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

```>>> hex('ff9999') + 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