• Stars
    star
    231
  • Rank 173,434 (Top 4 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created over 12 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Colors aren't that scary!

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)
<RGBColor red: 100, green: 100, blue: 100>

Convert it to hexadecimal

>>> rgb(100, 100, 100).hex
<HexColor red: 64, green: 64, blue: 64>

Coerce the hexadecimal to a normal string

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

Create a Hexadecimal color object

>>> hex('646464')
<HexColor red: 64, green: 64, blue: 64>

Extract the red/green/blue value from a hexadecimal

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

Convert a hexadecimal to HSV

>>> hex('646464').hsv
<HSVColor hue: 0.0, saturation: 0.0, value: 0.392156862745>

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)
<HSVColor hue: 0, saturation: 1, value: 1>

Convert it to RGB

>>> hsv(0, 1, 1).rgb
<RGBColor red: 255, green: 0.0, blue: 0.0>

Gimme a random color, any color!

>>> random()
<HSVColor hue: 0.812436498638, saturation: 0.621033239007, value: 0.379850638405>

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

Add

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

Subtract

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

Divide

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

Blend Modes

Note: All blend modes return rgb color.

Screen

>>> hex('ff9999').screen(rgb(10, 10, 10)).hex
<HexColor red: ff, green: 9d, blue: 9d>

Difference

>>> hex('ff9999').difference(rgb(10, 10, 10)).hex
<HexColor red: f5, green: 8f, blue: 8f>

Overlay

>>> hex('ff9999').overlay(rgb(10, 10, 10)).hex
<HexColor red: ff, green: 9b, blue: 9b>

Invert

>>> hex('000000').invert()
<RGBColor red: 255, green: 255, blue: 255>

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
<RGBColor red: 255, green: 0, blue: 0>

colors.rainbow

>>> import colors.rainbow
>>> colors.rainbow.indigo
<RGBColor red: 75, green: 0, blue: 130>

colors.w3c

>>> import colors.w3c
>>> colors.w3c.ghostwhite
<RGBColor red: 248, green: 248, blue: 255>

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.

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

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

More Repositories

1

jinja2-cli

CLI for Jinja2
Python
471
star
2

django-sudo

Extra security for your sensitive pages
Python
277
star
3

ec2

Sugar on top of boto for accessing EC2 instances and security groups
Python
140
star
4

proofread

Test your Django app without writing tests!
Python
89
star
5

email-me

email me when a thing is done
Go
53
star
6

sourcemap-validator

Service for validating sourcemaps
Python
51
star
7

node-snsclient

Client for Amazon AWS's SNS service
JavaScript
46
star
8

node_nibbler

Base32/base64 encode/decode library for Node
JavaScript
43
star
9

go-celery

WIP
Go
42
star
10

python-sourcemap

Parse JavaScript source maps
Python
39
star
11

jwt-node

Adapt jwt-js for use in Node.js, nothing special.
JavaScript
30
star
12

sentry-twilio

A plugin for Sentry that sends SMS notifications via Twilio
Python
22
star
13

size

A package for working with byte capacities similar to time.Duration
Go
21
star
14

firewall

Punch a hole into an AWS EC2 security group temporarily
Python
19
star
15

go-memcached

Memcached library for Go
Go
19
star
16

UT2k4-Mods

Unreal Tournament 2004 Maps and Characters
Shell
17
star
17

semaphore

A Go semaphore primitive
Go
17
star
18

go-bubs

β’·β“Šβ’·β’·β“β’Ίβ“ˆ ⒾⓃ β’Όβ“„
Go
16
star
19

mywsgi

Opinionated uWSGI setup
Python
15
star
20

node_pickle

A really, admittedly, lazy interface into Python for pickling/unpickling an object.
JavaScript
12
star
21

django-snippets

Just a bunch of small reusable snippets for projects. Middlewares, decorators, templatetags, etc.
Python
11
star
22

sentry-sprintly

A plugin for Sentry that creates Sprint.ly defects out of exceptions
Python
9
star
23

envtpl

Render a template from environment variables
Go
9
star
24

envdir-plusplus

kinda like envdir, kinda like dotenv, kinda like docker env-file
Go
9
star
25

docker-ut2004

Unreal Tournament 2004 Dedicated Server
Smarty
8
star
26

https

https proxy for local testing
Go
8
star
27

go-randid

Alternative random id generation to uuid
Go
7
star
28

connect-access

Access controls (ACLs) by location as a Connect middleware
JavaScript
7
star
29

whichpkg

Locate the path of a specific python module
Python
7
star
30

go-httpheaders

Canonicalized forms of common HTTP headers for Go
Go
7
star
31

python-stathat

A better Python library for StatHat
Python
7
star
32

node_signedcookies

Middleware for Express to sign/unsign cookies with a secret key.
JavaScript
6
star
33

node-mock-udp

Mocking and testing for UDP
JavaScript
5
star
34

sentry-groveio

A plugin for Sentry that logs errors to an IRC room on Grove.io
Python
5
star
35

pgtobq

postgres table -> bigquery table
Go
5
star
36

python-bandpage

The Python library for BandPage.
Python
5
star
37

breadsticks

Boom, you got breadsticks.
Python
5
star
38

ember

experiments with my ember mug
Python
4
star
39

python-zohodocs

Wrapper for Zoho Docs (Writer, Sheet, Show)
Python
4
star
40

twilio-js

Javascript library for Twilio
JavaScript
4
star
41

blart

to observe and report
Go
4
star
42

django-registration

Merge of https://bitbucket.org/ColeVsCode/django-registration/ & https://bitbucket.org/ubernostrum/django-registration
Python
4
star
43

tracker

Exploration with a BitTorrent tracker in Python
Python
3
star
44

emptygif

A little module for serving up an empty 1x1 transparent gif
Go
3
star
45

s3autoindex

autoindex for private s3 buckets
Go
3
star
46

django-locking-south

Prevent South from running concurrently
Python
3
star
47

nginx-and

run nginx... and something else!
Go
3
star
48

imgur-cli

upload clipboard data to imgur
Objective-C
3
star
49

python-svnserve

I'm trying to emulate the svn protocol with the Pythons
Python
3
star
50

libvmod-shards

Shell
3
star
51

jquery-sprite-plugin

Animate a sprite sheet with jQuery
JavaScript
2
star
52

connect-brotli

brotli compression for connect-go
Go
2
star
53

sentry-proxy

Go
2
star
54

python-casscache

Python
2
star
55

weather.withmatt.com

JavaScript
2
star
56

go-memcached-siege

Trying to break memcached.
Go
2
star
57

AS3-Classes

My collection of AS3 classes I wrote years ago
ActionScript
2
star
58

mattrobenolt.github.com

CSS
2
star
59

docker-twemproxy

Twemproxy (nutcracker)
Shell
1
star
60

node-redisq

A wrapper for simple Redis pub/sub in Node
JavaScript
1
star
61

callsite-shim

A shim for other browsers to conform to the CallSite API
JavaScript
1
star
62

python-npm

Python
1
star
63

hosthash

1
star
64

redis-py-contrib

Missing contrib packages for redis-py
Python
1
star
65

7languages

7 languages, 7 weeks, 1 man.
Ruby
1
star
66

update-maxmind

A little library that should be run on a cronjob to update the free MaxMind GeoIP libraries.
Python
1
star
67

withmatt.com

1
star
68

docker-vault

docker images for hashicorp's vault
Makefile
1
star
69

drunk-javascript

wtf Javascript exceptions
1
star
70

commands.py

Awesome python CLI shit, I think.
Python
1
star
71

mime.types

A collection of some normal filetypes for use in Python or Apache.
1
star
72

inboxb.in

1
star
73

jsonpd

jsonp -> esi
Go
1
star
74

django-proofread

Moved to https://github.com/mattrobenolt/proofread
1
star
75

twilio-node

A Node.js module for communicating with the Twilio API and generating TwiML
JavaScript
1
star
76

sublime-text-json-prettifier

1
star