Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mattrobenolt/colors.py

Colors aren't that scary!
https://github.com/mattrobenolt/colors.py

Last synced: 2 months ago
JSON representation

Colors aren't that scary!

Awesome Lists containing this project

README

        

# 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
```python
>>> from colors import rgb, hsv, hex, random
```
### Create an RGB color object
```python
>>> rgb(100, 100, 100)

```
### Convert it to hexadecimal
```python
>>> rgb(100, 100, 100).hex

```
### Coerce the hexadecimal to a normal string
```python
>>> str(rgb(100, 100, 100).hex)
646464
```
### Create a Hexadecimal color object
```python
>>> hex('646464')

```
### Extract the red/green/blue value from a hexadecimal
```python
>>> hex('646464').rgb.red
100
```
### Convert a hexadecimal to HSV
```python
>>> hex('646464').hsv

```
### Coerce hsv/rgb values to a list/tuple of values
```python
>>> list(hex('646464').hsv)
[0.0, 0.0, 0.39215686274509803]
```
### Create an HSV color object
```python
>>> hsv(0, 1, 1)

```
### Convert it to RGB
```python
>>> hsv(0, 1, 1).rgb

```
### Gimme a random color, any color!
```python
>>> random()

```
### Coerce a hexadecimal color to a string with formatting
```python
>>> '#%s' % random().hex
'#2f2336'
```
### Coerce RGB/HSV objects to a string for formatting
```python
>>> 'style="color: rgb(%s)"' % random().rgb
'style="color: rgb(80.3414147839, 124.403236079, 71.4620739603)"'
```
### Compare color equality
```python
>>> 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
```python
>>> hex('ff9999') * hex('cccccc')

>>> _.hex

>>> rgb(100, 100, 100).multiply(hsv(0, 1, 1)).hex
>>>
```
### Add
```python
>>> hex('ff9999') + rgb(10, 10, 10)

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

```
### Subtract
```python
>>> hex('ff9999') - rgb(10, 10, 10)

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

```
### Divide
```python
>>> 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
```python
>>> hex('ff9999').screen(rgb(10, 10, 10)).hex

```
### Difference
```python
>>> hex('ff9999').difference(rgb(10, 10, 10)).hex

```
### Overlay
```python
>>> hex('ff9999').overlay(rgb(10, 10, 10)).hex

```
### Invert
```python
>>> hex('000000').invert()

```
## Color palettes
`colors.py` current ships with three color palettes full of constants. See source for all available colors.
### `colors.primary`
```python
>>> import colors.primary
>>> colors.primary.red

```
### `colors.rainbow`
```python
>>> import colors.rainbow
>>> colors.rainbow.indigo

```
### `colors.w3c`
```python
>>> 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
```