Transform Color Space

Description

Allows to transform the current object into a different color space, if possible. Converting the colors of the current object into another color space. After calling this method, the object will be of a different class.

Usage

CIEXYZ.to(to, fixup=True)

Arguments

tostr
Name of the color space into which the colors should be converted (e.g., "CIELUV", "HCL", "hex", "sRGB", …).
fixupbool
Whether or not colors outside the defined rgb color space should be corrected if necessary, defaults to True.

Examples

from colorspace import CIEXYZ
x = CIEXYZ([ 8.5, 27.8, 46.2, 62.1],
           [ 4.4, 14.5, 34.1, 65.9],
           [27.2, 31.9, 17.2, 40.0])
x
CIEXYZ color object (4 colors)
            X       Y       Z
  1:     8.50    4.40   27.20
        27.80   14.50   31.90
        46.20   34.10   17.20
        62.10   65.90   40.00
type(x)
colorspace.colorlib.CIEXYZ
# Convert colors to sRGB
x.to("sRGB")
x
sRGB color object (4 colors)
            R       G       B
  1:     0.30    0.11    0.57
         0.75    0.13    0.60
         0.95    0.48    0.41
         0.91    0.83    0.60
type(x)
colorspace.colorlib.sRGB
# Convert from sRGB to hex
x.to("hex")
x
  • #4C1C91
  • #BF229A
  • #F27B68
  • #E7D39A
# Convert back to CIEXYZ colors.
# Round-off errors due to conversion to 'hex'.
x.to("CIEXYZ")
x
CIEXYZ color object (4 colors)
            X       Y       Z
  1:     8.50    4.41   27.18
        27.89   14.56   31.90
        46.20   34.05   17.23
        62.08   65.91   40.02
# Extracting hex colors (returns list of str)
x.colors()
['#4C1C91', '#BF229A', '#F27B68', '#E7D39A']