gtaylor / python-colormath

A python module that abstracts common color math operations. For example, converting from CIE L*a*b to XYZ, or from RGB to CMYK
python-colormath.readthedocs.org
BSD 3-Clause "New" or "Revised" License
456 stars 83 forks source link

Problems with conversion LAB and RGB - a bug? #92

Closed ngovindarajan closed 5 years ago

ngovindarajan commented 5 years ago

Hi all,

I am facing the following issue when running:

from colormath.color_objects import LabColor, sRGBColor, AdobeRGBColor from colormath.color_conversions import convert_color

Color_LAB = LabColor(74,4,2) Color_RGB = convert_color( Color_LAB , AdobeRGBColor) Color_LAB2 = convert_color( Color_RGB , LabColor)

print(Color_LAB2)

Python returns:

LabColor (lab_l:73.9545 lab_a:3.8096 lab_b:1.9194)

Instead of the values 74, 4, and 2. Why and how can I fix this?

thank you,

Nithin

KelSolaar commented 5 years ago

Hi @ngovindarajan,

It is because the colour you specified is assumed to adopt D50: https://github.com/gtaylor/python-colormath/blob/a5102d7046ad28ec8a143434dcf3859a55741fb9/colormath/color_objects.py#L286 and you are converting it to AdobeRGBColor which adopts D65: https://github.com/gtaylor/python-colormath/blob/master/colormath/color_objects.py#L688.

As a consequence, the colour is chromatically adapted from D50 to D65. It is effectively the same colour be seen under a different illuminant. If you want to avoid that you can specify the illuminant when instantiating the LabColor class:

Color_LAB = LabColor(74,4,2, illuminant='d65')

After that the output is as follows:

LabColor (lab_l:73.9999 lab_a:4.0004 lab_b:1.9997)

The roundtrip is not perfect though because the AdobeRGBColor matrices do not have enough precision to be exact inverse.