Requirements for colormath
masterTo use colormath, ensure your environment meets the following requirements:
numpyNetworkX2.0 or higherPython2.7 orPython3.5 or higher
Note: This module is no longer actively maintained.
repository·master·Indexed 19 days ago
https://github.com/gtaylor/python-colormathA Python module for color mathematics, providing tools for color space conversions via convert_color(), Delta E calculations (CIE 1976, 1994, 2000, and CMC), and density to spectral conversions. It includes support for Color Appearance Models such as CIECAM02, Nayatani95, Hunt, RLAB, ATD95, and LLAB to predict perceptual correlates like lightness, chroma, and hue.
To use colormath, ensure your environment meets the following requirements:
numpyNetworkX 2.0 or higherPython 2.7 or Python 3.5 or higherNote: This module is no longer actively maintained.
Color appearance models in colormath allow you to predict perceptual correlates (such as lightness, chroma, or hue) of a surface color under specific viewing conditions. These conditions include the illuminant, the surround, and the background luminance.
Each model is implemented as a class in colormath.color_appearance_models. When instantiated, the model performs its computation and stores the predicted perceptual correlates as instance attributes.
Supported models include:
CIECAM02 / CIECAM02m1Nayatani95HuntRLABATD95LLABfrom colormath.color_spaces import XYZColor
from colormath.color_appearance_models import CIECAM02
# Define the color stimulus in XYZ
color = XYZColor(19.01, 20, 21.78)
# Define the illuminant (e.g., D65)
illuminant_d65 = XYZColor(95.05, 100, 108.88)
# Viewing conditions
y_b = 20 # Background relative luminance
l_a = 328.31 # Adapting luminance
c = 0.69 # Surround condition (average)
n_c = 1 # Surround condition
f = 1 # Surround condition
# Instantiate the model to compute correlates
model = CIECAM02(color.xyz_x, color.xyz_y, color.xyz_z,
illuminant_d65.xyz_x, illuminant_d65.xyz_y, illuminant_d65.xyz_z,
y_b, l_a, c, n_c, f)
# Predicted correlates are available as attributes on the 'model' instancecolormath on Windows, you must first download and install the NumPy binary distribution before installing colormath.If you are contributing to the project or need development tools, install the package with the development extra.
$ pip install 'colormath[development]'Delta E equations quantify the visual difference between two LabColor instances. The library provides several implementations depending on the required precision and application context.
To calculate a difference, import the desired Delta E function from colormath.color_diff and pass two LabColor objects as arguments. The function returns the Delta E value as a float.
from colormath.color_objects import LabColor
from colormath.color_diff import delta_e_cie1976
# Reference color
color1 = LabColor(lab_l=0.9, lab_a=16.3, lab_b=-2.22)
# Color to be compared to the reference
color2 = LabColor(lab_l=0.7, lab_a=14.2, lab_b=-1.80)
# Calculate Delta E value as a float
delta_e = delta_e_cie1976(color1, color2)When converting from an RGB space to a CIE space (like XYZ, Lab, LCH, or Luv), the conversion passes through the XYZ color space. RGB spaces have native illuminants (e.g., sRGB uses D65).
To override the default native illuminant and explicitly request a specific one, provide the target_illuminant keyword argument to convert_color.
from colormath.color_objects import XYZColor, sRGBColor
from colormath.color_conversions import convert_color
# Note: Ensure RGBColor is imported or use sRGBColor
rgb = sRGBColor(0.1, 0.2, 0.3)
xyz = convert_color(rgb, XYZColor, target_illuminant='d50')Install the colormath package using pip. This is the easiest way to get started with color space conversions, Delta E calculations, and density to spectral operations.
$ pip install colormathOn Linux or macOS, you can install colormath using pip or easy_install.
pip install colormathSome color conversions (e.g., XYZ to HSL or XYZ to CMYK) require an intermediate step through an RGB color space. By default, sRGB is used. If you need to use a different RGB space to maintain color accuracy or gamut characteristics, use the through_rgb_type keyword argument in convert_color.
Note: If you convert from Space A $\rightarrow$ Space B $\rightarrow$ Space A, ensure you use the same through_rgb_type for both directions to avoid color shifts.
from colormath.color_objects import XYZColor, HSLColor, AdobeRGBColor
from colormath.color_conversions import convert_color
xyz = XYZColor(0.1, 0.2, 0.3)
# Convert using AdobeRGB as the intermediate step
hsl = convert_color(xyz, HSLColor, through_rgb_type=AdobeRGBColor)
# Convert back using the same intermediate space to preserve accuracy
xyz2 = convert_color(hsl, XYZColor, through_rgb_type=AdobeRGBColor)When working with color spaces that use reflective light, you can specify the illuminant and the observer angle during initialization. This is done using the observer and illuminant keyword arguments in the LabColor class.
lab = LabColor(0.1, 0.2, 0.3, observer='10', illuminant='d65')Density can be calculated from SpectralColor instances using two primary functions in colormath.density:
auto_density(color): Calculates the density automatically based on the provided color object.ansi_density(color, filter): Calculates the density using a specific filter constant provided by colormath.density_standards.Both functions accept SpectralColor objects as input.
from colormath.color_objects import SpectralColor
from colormath.density import auto_density, ansi_density
from colormath.density_standards import ANSI_STATUS_T_RED
# Create a SpectralColor instance
color = SpectralColor(spec_340nm=0.08)
# Calculate automatic density
density = auto_density(color)
# Calculate density using a specific ANSI filter
red_density = ansi_density(color, ANSI_STATUS_T_RED)The CIECAM02 class computes perceptual correlates based on the CIECAM02 color appearance model. It requires the XYZ components of the color stimulus, the XYZ components of the illuminant, and several viewing condition parameters.
Parameters:
color_x, color_y, color_z: XYZ components of the color stimulus.illuminant_x, illuminant_y, illuminant_z: XYZ components of the illuminant.y_b: Background relative luminance.l_a: Adapting luminance.c: Surround condition (e.g., 0.69 for average).n_c: Surround condition.f: Surround condition.model = CIECAM02(color_x, color_y, color_z, illuminant_x, illuminant_y, illuminant_z, y_b, l_a, c, n_c, f)