Skip to main content

A python package for easy management of colors and palettes.

Project description

What is colorir?

colorir is a package developed to unify your workflow with colors across different projects.

With colorir you can:

  • Keep a unified selection of colors you like and use them in your different projects;

  • Use these colors directly as input for other graphical or web frameworks;

  • Easily convert between different color systems and formats;

  • Create gradients between colors and sample from them;

  • And much more!

colorir was designed to be your best friend when dealing with colors so that you won’t ever need to write this kind of code again:

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
CSS_ALICEBLUE = (240, 248, 255)
COOL_PURPLE = (11, 0, 51)
MY_FAVORITE_GREEN = (113, 180, 141)
TOP_NOTCH_RED = (131, 34, 50)
# ... unnecessarily long and ugly list of colors

Installation

To install colorir with pip use following command:

$ python -m pip install colorir

Quick-Start

Create a palette with the additive elementary colors and call it ‘elementary’:

>>> palette = Palette(name="elementary",
...                   redadd=HexRGB("#ff0000"),
...                   greenadd=HexRGB("00ff00"), # No need to include the # symbol
...                   blueadd=HexRGB("#0000ff"))

Following CSS color-naming conventions, our color names are all lowercase with no underscores, but you may name a color as you wish as long as it complies with python’s syntax for attribute names.

We can add colors by providing a name and a color-like object to the Palette.add() method:

>>> palette.add("cyansub", "#00ffff")
>>> palette.add("yellowsub", "#ffff00")
>>> palette.add("magentasub", HSL(300, 1, 0.5))

Note how in how we passed hex strings as arguments without initializing HexRGB colors this time. Most objects in the colorir package can interpret strings and tuples as colors implicitly!

We also passed an HSL object for “magentasub”. Because we initialized the palette with HexRGB colors, the HSL value will be internally converted to a HexRGB, which can be useful if we don’t know the hex code of a color or want to modify attributes exposed in other color systems, such as saturation or luminance.

For more details on how color-like objects are interpreted, visit the documentation of Palette and ColorFormat.format().

To then modify a color after it has been added, use the Palette.update() method:

>>> palette.update("magentasub", "#ff11ff") # Mix some green component into the magenta

Now suppose we want to finally use the colors we added to our palette. For that we can get them individually as attributes of the palette:

>>> palette.cyansub
HexRGB(#00ffff)

Or we can get them all at once with the Palette.colors property:

>>> palette.colors
[HexRGB(#ff0000), HexRGB(#00ff00), HexRGB(#0000ff), HexRGB(#00ffff), HexRGB(#ffff00), \
HexRGB(#ff11ff)]

Since we are done using our palette for now, let’s save it to the default palette directory:

>>> palette.save()

We can then latter load the palette (even from other projects if we wish!):

>>> palette = Palette.load("elementary")

When loading or instantiating a palette, a ColorFormat may be passed to the constructor to specify how we want the color to be represented:

>>> c_format = ColorFormat(color_sys=HSL)
>>> css = Palette.load("css", color_format=c_format)
>>> css.red
HSL(0.0, 1.0, 0.5)

We can also change the format of all colors in a palette at any time by re-assigning its Palette.color_format property:

>>> css.color_format = ColorFormat(color_sys=sRGB, max_rgba=1)
>>> css.red
sRGB(1.0, 0.0, 0.0)

Alternatively, we can temporarily change the default color format system-wide so that new palettes (that don’t already hold any color objects) default to it:

>>> from colorir import config, PYGAME_COLOR_FORMAT
>>> config.DEFAULT_COLOR_FORMAT = PYGAME_COLOR_FORMAT # Change default format to PyGame
>>> pygame_palette = Palette(red=(255, 0, 0))
>>> pygame_palette.red
sRGB(255, 0, 0)

This makes it easy to configure colorir to work with any color format right out of the box!

By default, the default color format is lowercase hex strings, like what you expect to find working with web development or matplotlib.

>>> from colorir import config, WEB_COLOR_FORMAT
>>> config.DEFAULT_COLOR_FORMAT = WEB_COLOR_FORMAT # Change default back to web-compatible
>>> web_palette = Palette.load("css")
>>> web_palette.red
HexRGB(#ff0000)

It is worth noting that all color classes inherit either tuple or str, meaning that no conversion is needed when passing them to other frameworks such as PyGame, Kivy and HTML embedding templates like Jinja.

Documentation and Examples

The full documentation (including use-case examples) for colorir is available here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

colorir-1.0.1.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

colorir-1.0.1-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file colorir-1.0.1.tar.gz.

File metadata

  • Download URL: colorir-1.0.1.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for colorir-1.0.1.tar.gz
Algorithm Hash digest
SHA256 3444c0154c4b4b928c8ff65c5c65712e62c2c74f7f01e2978fce19316ce829d9
MD5 36efc080a09c187f11d830040709669c
BLAKE2b-256 47bacf4d7cdd1dd0fec645d6808029e8c943891dfbbff9f81275cb3fdcb644af

See more details on using hashes here.

File details

Details for the file colorir-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: colorir-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.8

File hashes

Hashes for colorir-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f764cea33ee023ebdeab1abec4d83b083ef4778658e3a29c13b9aff3be8ae1e2
MD5 3a76de77b61aeb029769704e4a721b2c
BLAKE2b-256 9efcfdd85b5538a31bcdc43daf403f9f14ca12d96ddd45e548f41db940f2ee33

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page