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 handling methods are different to allow to use the color maps in python in an easy and generalized way.

Color Palettes

In the R colorspace <https://cran.r-project.org/package=colorspace>_ package the palettes are functions returning a set of hex colors as a character list, the default method how colors are used in R.

The python colorspace package does it slightly different. Palettes are objects rather than functions which allow to retrieve lists of hex colors using the colors() method but also provide additiona methods which allow to use the palettes for plotting in python.

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
pal    = diverging_hcl()  # Initialize object
colors = pal.colors(11)   # Retrieve colors
# Plotting a simple color map
swatchplot(colors)

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 (as) python objects provide .as(...) methods. 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
## [u'#023FA5', u'#A1A6C8', u'#E2E2E2', u'#CA9CA4', u'#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