colormath is a Python library that simplifies complex color mathematics and conversions. The current stable version is 3.0.0. It provides support for a wide range of color spaces (e.g., CIE Lab, XYZ, sRGB, HSL, HSV, CMY/CMYK), enabling conversions between them, calculation of color differences (Delta E), and chromatic adaptations. The project maintains an active status with ongoing documentation updates, though code releases are less frequent.
pip install colormathVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate a LabColor object and convert it to the XYZColor space using the `convert_color` function.
Replace `color_object.convert_to()` with `colormath.color_conversions.convert_color(color_object, TargetColorClass)`. Replace `RGBColor` with `sRGBColor` or another appropriate `BaseRGBColor` subclass.
Ensure RGB input values are normalized to `[0.0, 1.0]`. If you need the `[1-255]` range, use the `is_upscaled=True` parameter in constructors or `get_upscaled_value_tuple()` where available.
Explicitly specify the desired `target_illuminant` keyword argument in the `convert_color()` function (e.g., `convert_color(rgb_color, XYZColor, target_illuminant='d50')`).
If your conversion chain is producing unexpected results or clipping colors, consider explicitly setting the `through_rgb_type` parameter in `convert_color()` to a different RGB space (e.g., `AdobeRGBColor`) if a wider gamut is needed.
Use the module-level `convert_color()` function: `from colormath.color_conversions import convert_color; new_color = convert_color(old_color, TargetColorClass)`.
Replace `RGBColor` with a specific RGB color space class, most commonly `sRGBColor`: `from colormath.color_objects import sRGBColor; my_srgb = sRGBColor(r, g, b)`.
Normalize your RGB values to `[0.0, 1.0]`. Alternatively, use `is_upscaled=True` in the constructor if you intend to pass `[0-255]` values, although `[0.0-1.0]` is the recommended standard. For example: `sRGBColor(r/255.0, g/255.0, b/255.0)` or `sRGBColor(r, g, b, is_upscaled=True)`.