Skip to main content

Tools for color models

Project description

colorio

Tools for color models.

CircleCI codecov Code style: black colorful PyPI pyversions PyPi Version DOI GitHub stars PyPi downloads

Color spaces

All color spaces implement the two methods

vals = colorspace.from_xyz100(xyz)
xyz = colorspace.to_xyz100(vals)

for conversion from and to XYZ100. Adding new color spaces is as easy as writing a class that provides those two methods.

The following color spaces are implemented:

  • XYZ (colorio.XYZ())
  • xyY (colorio.XYY())
  • Linear SRGB (colorio.SrgbLinear()) This class has the two additional methods
    from_srgb1()
    to_srgb1()
    
    for conversion from and to standard RGB.
  • HSL and HSV (colorio.HSL(), colorio.HSV()) These classes have the two methods
    from_srgb1()
    to_srgb1()
    
    for conversion from and to standard RGB.
  • OSA-UCS (colorio.OsaUcs())
  • CIELAB (colorio.CIELAB())
  • CIELUV (colorio.CIELUV())
  • RLAB (colorio.RLAB())
  • ICtCp (colorio.ICtCp())
  • IPT (colorio.IPT())
  • CIECAM02 / CAM02-UCS
    import colorio
    
    L_A = 64 / numpy.pi / 5
    ciecam02 = colorio.CIECAM02(0.69, 20, L_A)
    cam02 = colorio.CAM02('UCS', 0.69, 20, L_A)
    
    The implementation contains a few improvements over the CIECAM02 specification (see here).
  • CAM16 / CAM16-UCS
    import colorio
    
    L_A = 64 / numpy.pi / 5
    cam16 = colorio.CAM16(0.69, 20, L_A)
    cam16ucs = colorio.CAM16UCS(0.69, 20, L_A)
    
    The implementation contains a few improvements over the CAM16 specification (see here).
  • Jzazbz (colorio.JzAzBz())

All methods in colorio are fully vectorized, i.e., computation is really fast.

Tools

colorio provides a number of useful tools for analyzing and visualizing color spaces.

Visualizing the SRGB gamut

XYZ CIELAB CAM16-UCS

The SRGB gamut is a perfect cube in SRGB space, and takes curious shapes when translated into other color spaces. The above image shows the SRGB gamut in XYZ space. The image data was created with

import colorio

colorspace = colorio.CIELAB()
colorspace.save_srgb_gamut( "srgb.vtk", n=50, cut_000=False)

# The HDR (Rec.2100, Rec.2020) gamut works the same way
colorspace.save_hdr_gamut("hdr.vtk", n=50, cut_000=False)

The VTK file can then be opened in, e.g., ParaView, where the following instructions apply:

  1. Open the file in ParaView and execute the following steps in the Properties tab to the left.
  2. Press the Apply button.
  3. Under the Coloring section, change Solid Color to srgb.
  4. If necessary, press the gear button to the right of the search field to activate advanced options.
  5. Under the Scalar Coloring section, uncheck Map Scalars.

More images are in the gh-pages branch.

The data can be written in most formats supported by meshio. (You might have to install additional packages for some formats.)

Visualizing the visible gamut

XYZ CIELAB CAM16-UCS

Same as above, but with the gamut visible under a given illuminant.

import colorio

illuminant = colorio.illuminants.d65()
observer = colorio.observers.cie_1931_2()

colorspace = colorio.XYZ()
colorspace.save_visible_gamut(observer, illuminant, "visible.vtk")

The gamut is shown in grey since SRGB screens are not able to display the colors anyway.

Slices through the color spaces

Instead of fiddling around with the proper 3D objects, colorio can plot slices through all color spaces. One simply provides the slice index (typically the one that corresponds to "lightness" in the respective color space, e.g., 2 in xyY and 0 in CIELAB) and the slice level.

The solid line corresponds to monochromatic light; for convenience, the slice through the SRGB gamut is also displayed.

xyY (at Y=0.4) CIELAB (at L=50) CAM16-UCS (at J'=50)
import colorio

# xyy = colorio.XYY()
# xyy.show_visible_slice("xyy-visible-slice.png", 2, 0.4)

# cielab = colorio.CIELAB()
# cielab.show_visible_slice(0, 50)

cam16 = colorio.CAM16UCS(0.69, 20, 4.07)
cam16.show_visible_slice(0, 50)
# cam16.save_visible_slice("cam16ucs-visible-slice.png", 0, 50)

For convenience, it is also possible to show the classical visible gamut in xy with Planckian locus and the SRGB colors (at maximum luminosity).

import colorio

colorio.show_flat_gamut()

Show experimental data

colorio contains lots of experimental data sets some of which can be used to assess certain properties of color spaces.

MacAdam
xyY (at Y=0.4) CIELAB (at L=50) CAM16 (at L=50)

The famous MacAdam ellipses (from this article) can be plotted with

import colorio

# xyy = colorio.XYY()
# xyy.show_macadam(0.4)
# xyy.save_macadam("macadam-xyy.png", 0.4)

cieluv = colorio.CIELUV()
cieluv.show_macadam(50)
Luo-Rigg
xyY (at Y=0.4) CIELAB (at L=50) CAM16 (at L=50)

Likewise for Luo-Rigg.

import colorio

# xyy = colorio.XYY()
# xyy.show_luo_rigg(0.4)
# xyy.save_luo_rigg("luo-rigg-xyy.png", 0.4)

cieluv = colorio.CIELUV()
cieluv.show(50)
Ebner-Fairchild
xyY CIELAB CAM16

For example

import colorio

colorspace = colorio.JzAzBz()
colorspace.show_ebner_fairchild()

shows constant-hue data from the Ebner-Fairchild experiments in the hue-plane of some color spaces. (Ideally, all colors in one set sit on a line.)

Hung-Berns

Likewise for Hung-Berns:

xyY CIELAB CAM16

Note the dark blue distortion in CIELAB and CAM16.

import colorio

colorspace = colorio.JzAzBz()
colorspace.show_hung_berns()
Xiao et al.

Likewise for Xiao et al.:

xyY CIELAB CAM16
import colorio

colorspace = colorio.CIELAB()
colorspace.show_xiao()
Munsell
xyY CIELAB CAM16

Munsell color data is visualized with

import colorio

colorspace = colorio.CIELUV()
colorspace.show_munsell(V=5)

To retrieve the Munsell data in xyY format, use

import colorio

h, V, C, xyy = colorio.get_munsell_data()

Color differences

Color differences in any space can be computed with colorio.delta(a, b).

Articles

Installation

colorio is available from the Python Package Index, so just use

pip install colorio

to install.

Testing

To run the tests, simply check out this repository and run

tox

License

This software is published under the GPLv3 license.

Project details


Download files

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

Files for colorio, version 0.6.0
Filename, size File type Python version Upload date Hashes
Filename, size colorio-0.6.0-py3-none-any.whl (135.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size colorio-0.6.0.tar.gz (122.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page