Color transformationΒΆ

../_images/img_transform.jpeg

Create a new HCL color object with a set of (manually specified) colors:

from colorspace.colorlib import HCL
# Let's specify a set of HCL colors.
# These three colors are a bright blue (260, 100, 50),
# a neutral light gray (310, 0, 90) and a bright red (360, 100, 50)
# from the "Red-Blue 2" diverging color palette.
cols = HCL(H = [160, 210, 260, 310, 360],
           C = [ 70,  40,  10,  40,  70],
           L = [ 50,  70,  90,  70,  50])
cols.swatchplot()

# Show current values, as "c" is a HCL object the method
# prints the H, C, L color coordinates.
print cols
# Following the image above we would like to do the following:
# (1) convert HCL to CIEXYZ,
## polarLUV color object (5 colors)
##            H       C       L
##   1:  160.00   70.00   50.00
##       210.00   40.00   70.00
##       260.00   10.00   90.00
##       310.00   40.00   70.00
##       360.00   70.00   50.00
cols.to("CIEXYZ")
print cols
# (2) convert CIEXYZ to sRGB,
## CIEXYZ color object (5 colors)
##            X       Y       Z
##   1:    7.93   18.42   14.65
##        32.82   40.75   59.19
##        73.30   76.30   91.77
##        47.69   40.75   61.60
##        27.04   18.42   16.88
cols.to("sRGB");      print cols
# (3) convert sRGB to hex colors,
## sRGB color object (5 colors)
##            R       G       B
##   1:   -1.28    0.56    0.38
##         0.41    0.72    0.77
##         0.88    0.89    0.93
##         0.80    0.61    0.79
##         0.74    0.33    0.43
cols.to("hex");       print cols
# (4) convert hex colors to RGB,
## hexcols color object (5 colors)
##         hex_
##   1: #008F62
##      #69B7C5
##      #E0E2EE
##      #CD9BCB
##      #BD556E
cols.to("RGB");       print cols
# (5) convert RGB to polarLAB,
## RGB color object (5 colors)
##            R       G       B
##   1:    0.00    0.27    0.12
##         0.14    0.47    0.56
##         0.75    0.76    0.85
##         0.61    0.33    0.60
##         0.51    0.09    0.16
cols.to("polarLAB");  print cols
# (6) and finally back to HCL.
## polarLAB color object (5 colors)
##            L       A       B
##   1:   52.43   46.41  161.20
##        70.10   25.00  215.65
##        90.05    6.25  284.27
##        69.99   31.79  326.89
##        50.03   44.73    8.22
cols.to("HCL");       print cols
# Plotting colors
## polarLUV color object (5 colors)
##            H       C       L
##   1:  149.77   51.97   52.43
##       208.97   39.91   70.10
##       259.59    9.84   90.05
##       309.48   40.15   69.99
##         0.06   69.92   50.03
cols.swatchplot()

As you can see the final object cols is, again, the one we have specified at the beginning of the example - or at least mainly the same. The small differences between the initial values and the Hue-Chroma-Luminance values in our final object result from the hex-transformation (to and from hex colors). Furthermore, for some colors having low chroma and high luminance the identification of the exact hue is not always unambiguous which can also yield differences when transforming the colors back and forth.