Skip to content

mvrozanti/colp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

colp

Python versions PRs welcome Build Status Coverage Status PyPI License

The color processor

Objective

This tool is inspired on various processing tools such as awk, sed and imagemagick. There is no processing library for single color operations, be it interactively or not. colp attempts to solve that. It supports a number of color models (specified later in this README). The goal is to have the ability to script and automate any algorithmic color transformation.

Installation

pip install colp

Example Usage

Interactively operate on colors

$ colp 'a=RGB(1,1,1)' # optionally load a color to memory

β”Œβ”€β”β”Œβ”€β”β”¬  ┬─┐
β”‚  β”‚ β”‚β”‚  β”‚β”€β”˜
β””β”€β”˜β”˜β”€β”˜β”†β”€β”˜β”†   the color processor

> a = a.redder()                  # single channel increment
> a
RGB(2, 1, 1)
> a = a + 1                       # all channels increment
> a
RGB(3, 2, 2)
> lightgoldenrodyellow            # X11/HTML constants
RGB(250, 250, 210)
> -a                              # inversion 
RGB(252, 253, 253)
> red.rotate(180) == -red == cyan # rotate hue in HSV space
True
> skyblue ; skyblue.brighter()    # brighter in HSV space
RGB(135, 206, 235)
RGB(136, 208, 237)
> a_set_of_colors = [RGB(25,25,112), HSV(186,23,90), plum, orchid, fuchsia, HEX('#800080')]
> sorted(a_set_of_colors)
[RGB(25, 25, 112), HEX('#800080'), RGB(218, 112, 214), RGB(221, 160, 221), HSV(186, 23, 90), HEX('#ff00ff')]
>
> cursor = RGB(0,0,0)             # define your own custom functions
> def pop_and_turn(x):
>       if x: # equivalent to RGB(0,0,0) == 0
>             x = x.redder() * 255
>       return x.rotate(30)
> 
> for i in range(10):
>       cursor = pop_and_turn(cursor)
>       print(cursor)
>
RGB(255, 0, 127)
RGB(255, 0, 0)
RGB(255, 127, 0)
RGB(255, 255, 0)
RGB(127, 255, 0)
RGB(0, 255, 0)
RGB(0, 255, 127)
RGB(0, 255, 255)
RGB(0, 127, 255)
RGB(0, 0, 255)
> for c in red.interpolate(blue,10): # interpolate between colors
>       print(c)
>
RGB(255, 0, 0)
RGB(226, 0, 28)
RGB(198, 0, 56)
RGB(170, 0, 85)
RGB(141, 0, 113)
RGB(113, 0, 141)
RGB(85, 0, 170)
RGB(56, 0, 198)
RGB(28, 0, 226)
HEX('#0000ff')

Or pipe commands in

$ echo "HEX('#ff0000').rotate(15.).to(RGB)" | colp

RGB(255, 63, 0)

Usage from Python script

from colp import *
print(RGB(1,2,3).to(HEX))

outputs:

HEX('#010203')

Class Hierarchy

Color
β”œβ”€β”€ RGB
β”‚   β”œβ”€β”€ HEX
β”‚   └── CMYK
β”œβ”€β”€ YIQ
β”‚   β”œβ”€β”€ YUV 
β”‚   β”œβ”€β”€ YPbPr
β”‚   β”œβ”€β”€ YDbDr
β”‚   └── YCbCr
β”‚       └── xvYCC
β”œβ”€β”€ CIE
β”‚   β”œβ”€β”€ CIELAB
β”‚   β”œβ”€β”€ CIELCh
β”‚   β”œβ”€β”€ CIEUVW
β”‚   └── CIEXYZ
└── HSV = HSD = HSB = HSI
    └── HSL

Formal Parameters

usage: colp [-h] [-n] [-v] [-c] [-s] [-l SCRIPT_FILE] [VAR [VAR ...]]

Color Processor

positional arguments:
  VAR                   arbitrary python code execution

optional arguments:
  -h, --help            show this help message and exit
  -n, --no-banner       don't show banner on interactive mode
  -v, --visualizer      visualize current color processed
  -c, --css-mode        css-compliant output
  -s, --scripting-mode  colp script output, which can be reinterpreted by colp
  -l SCRIPT_FILE, --load-script SCRIPT_FILE
                        load script from file or stdin

Basic Color Operators

- + * /
// ~ ^ |
& ** % >>
<< < > <=
>= == is not

Named Constants (140)

Good Resources

Supported Color Models

βœ“ RGB/RGBA/HEX: βœ“ HSV=HSB=HSI=HSD:
drawing drawing
HSL: CMY[K]:
drawing drawing
[CIE]LAB: YCbCr:
drawing drawing
YIQ: YUV:
drawing drawing

Contributing

There are many other useful colorspaces to convert to and only some are listed in this README. To cover a new colorspace, just add a class that extends Color or, even better, a Color subclass. There's also definitely some room for automating CSS-file editing.

Pull requests are welcomed!

Releases

No releases published

Packages

No packages published

Languages