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;

  • Pick from hundreds of colors with the built-in color picker;

  • 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",
...                   red=HexRGB("#ff0000"),
...                   green=HexRGB("00ff00"), # No need to include the # symbol
...                   blue=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("cyan", "#00ffff")
>>> palette.add("yellow", "#ffff00")
>>> palette.add("magenta", HSL(300, 1, 0.5))

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

We also passed an HSL object for “magenta”. By default, colors passed into a palette are converted to HexRGB, but we will see in a bit how to change this behaviour to work with other color formats.

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

>>> palette.update("magenta", "#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.cyan
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.5.tar.gz (21.9 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: colorir-1.0.5.tar.gz
  • Upload date:
  • Size: 21.9 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.5.tar.gz
Algorithm Hash digest
SHA256 49d776df84f1313ad831d54aa6617a932ab2a66e59e57106930cb10473742906
MD5 74aa4a1dcaf1c0ff871e478b607c6a31
BLAKE2b-256 04689f4c43f3700e52aaeefdcd34781d36bfa084955c28ec67de89ff725542c2

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