Color math and conversion library for comprehensive color space transformations and color difference calculations.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Color difference calculations provide perceptual measures of how different two colors appear to human vision. Colormath implements multiple industry-standard Delta E algorithms, each optimized for different applications and levels of accuracy.
The original CIE Delta E formula, simple but less perceptually uniform than newer methods.
def delta_e_cie1976(color1, color2):
"""
Calculate CIE 1976 Delta E difference.
Parameters:
- color1: First color object (Lab, Luv, or other color space)
- color2: Second color object (same type as color1)
Returns:
float: Delta E difference value (typically 0-100+)
Notes:
- Simple Euclidean distance in Lab space
- Values < 1: Not perceptible by human eye
- Values 1-2: Perceptible through close observation
- Values 2-10: Perceptible at a glance
- Values > 10: Colors appear as different colors
"""Improved formula with weighting factors for different viewing conditions.
def delta_e_cie1994(color1, color2, K_L=1, K_C=1, K_H=1, K_1=0.045, K_2=0.015):
"""
Calculate CIE 1994 Delta E difference.
Parameters:
- color1: First color object (Lab color recommended)
- color2: Second color object (same type as color1)
- K_L: Lightness weighting factor (default: 1)
- K_C: Chroma weighting factor (default: 1)
- K_H: Hue weighting factor (default: 1)
- K_1: First parametric factor (default: 0.045)
- K_2: Second parametric factor (default: 0.015)
Returns:
float: Delta E difference value
Notes:
- Better perceptual uniformity than CIE 1976
- Separate weighting for lightness, chroma, and hue
- Standard conditions: K_L=K_C=K_H=1
- Textiles: K_L=2, K_C=K_H=1
"""Most advanced and perceptually accurate Delta E formula currently available.
def delta_e_cie2000(color1, color2, Kl=1, Kc=1, Kh=1):
"""
Calculate CIE 2000 Delta E difference.
Parameters:
- color1: First color object (Lab color recommended)
- color2: Second color object (same type as color1)
- Kl: Lightness weighting factor (default: 1)
- Kc: Chroma weighting factor (default: 1)
- Kh: Hue weighting factor (default: 1)
Returns:
float: Delta E difference value
Notes:
- Most perceptually uniform Delta E formula
- Addresses issues with CIE 1994 in blue region
- Industry standard for color quality control
- Recommended for most applications
- Values < 1: Not perceptible
- Values 1-2: Just perceptible
- Values 2-5: Perceptible
- Values > 5: Well perceptible
"""Color Measurement Committee formula with adjustable tolerances for different industries.
def delta_e_cmc(color1, color2, pl=2, pc=1):
"""
Calculate CMC Delta E difference.
Parameters:
- color1: First color object (Lab color recommended)
- color2: Second color object (same type as color1)
- pl: Lightness tolerance factor (default: 2)
- pc: Chroma tolerance factor (default: 1)
Returns:
float: Delta E difference value
Notes:
- Designed for textile industry applications
- Standard acceptability: pl=2, pc=1 (CMC 2:1)
- Perceptibility threshold: pl=1, pc=1 (CMC 1:1)
- Good performance in neutral colors
"""from colormath.color_objects import LabColor
from colormath.color_diff import delta_e_cie2000
# Create two Lab colors
color1 = LabColor(lab_l=50.0, lab_a=20.0, lab_b=-30.0)
color2 = LabColor(lab_l=52.0, lab_a=18.0, lab_b=-28.0)
# Calculate Delta E 2000 (recommended)
diff = delta_e_cie2000(color1, color2)
print(f"Delta E 2000: {diff:.2f}")from colormath.color_objects import LabColor
from colormath.color_diff import (
delta_e_cie1976, delta_e_cie1994,
delta_e_cie2000, delta_e_cmc
)
color1 = LabColor(lab_l=50, lab_a=0, lab_b=0)
color2 = LabColor(lab_l=52, lab_a=2, lab_b=-1)
# Compare all methods
diff_1976 = delta_e_cie1976(color1, color2)
diff_1994 = delta_e_cie1994(color1, color2)
diff_2000 = delta_e_cie2000(color1, color2)
diff_cmc = delta_e_cmc(color1, color2)
print(f"CIE 1976: {diff_1976:.2f}")
print(f"CIE 1994: {diff_1994:.2f}")
print(f"CIE 2000: {diff_2000:.2f}")
print(f"CMC: {diff_cmc:.2f}")from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
# Create RGB colors
rgb1 = sRGBColor(rgb_r=0.8, rgb_g=0.2, rgb_b=0.1)
rgb2 = sRGBColor(rgb_r=0.85, rgb_g=0.15, rgb_b=0.12)
# Convert to Lab for Delta E calculation
lab1 = convert_color(rgb1, LabColor)
lab2 = convert_color(rgb2, LabColor)
# Calculate difference
diff = delta_e_cie2000(lab1, lab2)
print(f"RGB color difference: {diff:.2f}")from colormath.color_objects import LabColor
from colormath.color_diff import delta_e_cie1994, delta_e_cmc
color_standard = LabColor(lab_l=50, lab_a=10, lab_b=-5)
color_sample = LabColor(lab_l=51, lab_a=12, lab_b=-3)
# Graphic arts (standard conditions)
diff_graphics = delta_e_cie1994(color_standard, color_sample)
# Textiles (K_L=2 for textile industry)
diff_textiles = delta_e_cie1994(color_standard, color_sample, K_L=2)
# CMC for acceptability (2:1 ratio)
diff_cmc_accept = delta_e_cmc(color_standard, color_sample, pl=2, pc=1)
# CMC for perceptibility (1:1 ratio)
diff_cmc_percept = delta_e_cmc(color_standard, color_sample, pl=1, pc=1)
print(f"Graphics: {diff_graphics:.2f}")
print(f"Textiles: {diff_textiles:.2f}")
print(f"CMC Accept: {diff_cmc_accept:.2f}")
print(f"CMC Percept: {diff_cmc_percept:.2f}")from colormath.color_objects import LabColor
from colormath.color_diff import delta_e_cie2000
# Reference color
reference = LabColor(lab_l=50, lab_a=0, lab_b=0)
# Sample colors to compare
samples = [
LabColor(lab_l=52, lab_a=2, lab_b=-1),
LabColor(lab_l=48, lab_a=-1, lab_b=3),
LabColor(lab_l=50, lab_a=5, lab_b=-2),
LabColor(lab_l=53, lab_a=-3, lab_b=4)
]
# Calculate differences
differences = [delta_e_cie2000(reference, sample) for sample in samples]
# Analyze results
for i, diff in enumerate(differences):
status = "PASS" if diff < 2.0 else "FAIL"
print(f"Sample {i+1}: ΔE = {diff:.2f} [{status}]")from colormath.color_objects import LabColor
from colormath.color_diff import delta_e_cie2000
def assess_color_quality(standard_color, test_color, tolerance=2.0):
"""
Assess color quality against standard.
Parameters:
- standard_color: Reference color
- test_color: Color to test
- tolerance: Maximum acceptable Delta E
Returns:
dict: Quality assessment results
"""
diff = delta_e_cie2000(standard_color, test_color)
if diff <= tolerance * 0.5:
grade = "Excellent"
elif diff <= tolerance:
grade = "Acceptable"
else:
grade = "Reject"
return {
'delta_e': diff,
'grade': grade,
'pass': diff <= tolerance,
'tolerance': tolerance
}
# Example usage
standard = LabColor(lab_l=50, lab_a=10, lab_b=-10)
test_sample = LabColor(lab_l=51, lab_a=11, lab_b=-9)
result = assess_color_quality(standard, test_sample)
print(f"Grade: {result['grade']}")
print(f"Delta E: {result['delta_e']:.2f}")
print(f"Pass: {result['pass']}")| Delta E Range | Perception Level | Description |
|---|---|---|
| 0-1 | Not perceptible | Colors appear identical |
| 1-2 | Just perceptible | Very close observation needed |
| 2-5 | Perceptible | Colors appear similar but different |
| 5-10 | Well perceptible | Clear color difference |
| 10+ | Very different | Colors appear as different colors |
| Industry | Application | Typical Tolerance | Method |
|---|---|---|---|
| Printing | Commercial printing | ΔE < 3-5 | CIE 2000 |
| Textiles | Color matching | ΔE < 1-2 | CMC 2:1 |
| Paints | Automotive coatings | ΔE < 1 | CIE 2000 |
| Displays | Monitor calibration | ΔE < 2 | CIE 2000 |
| Photography | Color reproduction | ΔE < 2-3 | CIE 2000 |
| Method | Speed | Accuracy | Use Case |
|---|---|---|---|
| CIE 1976 | Fastest | Low | Quick screening |
| CIE 1994 | Fast | Medium | General purpose |
| CIE 2000 | Slower | High | Quality control |
| CMC | Fast | Medium | Textiles |
Install with Tessl CLI
npx tessl i tessl/pypi-colormath