In Contrast to R

This package is inspired and based on the R colorspace package. The two packages are very similar, however, some of the objects and methods differ to allow to use the color maps in python in an easy and “native” way.

Color Palettes

In R, the color palettes themselfes are functions returning a set of hex colors as a character vector, the default method to use colors in R.

The python colorspace package it handling it slightly different. Palettes are objects rather than functions and allow to retrieve lists of hex colors using the colors() method or the default object call, but also provide additional methods to inspect and/or manipulate the color palettes.

In R

# Loading the library
library("colorspace")
## Loading required package: methods
# Loading a set of 11 colors
colors <- diverging_hcl(11)
# Plotting a simple color map
par(mar = rep(0,4))
swatchplot(colors)

In python

# Loading the module
from colorspace import diverging_hcl, swatchplot
# Initialize the diverging_hcl object with default arguments,
# by default it is the "Blue-Red" diverging color palette.
pal    = diverging_hcl()
# Draw a set of 11 unique colors from the diverging palette.
colors = pal.colors(11)
# Plotting a simple color map
swatchplot(colors)
# Or simply:

pal.swatchplot(11)

Color Transformation

Another difference between the R and the python implementation is the way how colors are transformed between the different color spaces.

While the R colorspace packge provides a set of coercing functions (\u201cas\u201d), the python color objects provide a method \u201c.as(...)\u201d. \u201c.as(...)\u201d converts a set of colors from one color space into another, even if we have to go trough a set of intermid color spaces (e.g., from hex colors to HCL which goes from hex to sRGB, RGB, CIE-XYZ, CIE-LUV, HCL). The example below shows how a set of hex colors are transformed to HCL colors and back.

In R

# Loading the library
library("colorspace")
# Prepare a set of 5 colors from the default deverging_hcl palette
colors <- diverging_hcl(5)
print(colors)
## [1] "#023FA5" "#A1A6C8" "#E2E2E2" "#CA9CA4" "#8E063B"
# Convert to HCL
HCL <- as(hex2RGB(colors), "polarLUV")
print(HCL)
##             L            C           H
## [1,] 30.11593 80.093435919 259.9636996
## [2,] 68.76492 28.767127812 260.8507005
## [3,] 89.88369  0.006909247  94.0993148
## [4,] 68.76252 28.081877985   0.5009380
## [5,] 29.90803 79.930834932   0.0292239
# Convert back to hex
colors <- hex(as(HCL, "RGB"))
print(colors)
## [1] "#023FA5" "#A1A6C8" "#E2E2E2" "#CA9CA4" "#8E063B"

In python

# Loading the module
from colorspace import diverging_hcl
# Prepare a set of 5 colors from the default diverging_hcl palette
colors = diverging_hcl()(5)
print(colors)
# Convert to HCL
## ['#023FA5', '#A1A6C8', '#E2E2E2', '#CA9CA4', '#8E063B']
from colorspace.colorlib import hexcols
colors = hexcols(colors)
colors.to("HCL")
print(colors)
# Getting hex colors again by either using the call method
# or transforming the object back to a "hexcols" object.
## polarLUV color object (5 colors)
##            H       C       L
##   1:  259.96   80.09   30.12
##       260.85   28.77   68.76
##        94.10    0.01   89.88
##         0.50   28.08   68.76
##         0.03   79.93   29.91
print(colors())
# Transform ...
## [u'#023FA5', u'#A1A6C8', u'#E2E2E2', u'#CA9CA4', u'#8E063B']
colors.to("hex")
print(colors)
## hexcols color object (5 colors)
##         hex_
##   1: #023FA5
##      #A1A6C8
##      #E2E2E2
##      #CA9CA4
##      #8E063B