CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-colormath

Color math and conversion library for comprehensive color space transformations and color difference calculations.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

color-diff.mddocs/

Color Difference Calculations

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.

Capabilities

CIE 1976 Delta E

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
    """

CIE 1994 Delta E

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
    """

CIE 2000 Delta E

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
    """

CMC Delta E

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
    """

Usage Examples

Basic Color Difference

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}")

Comparing Different Delta E Methods

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}")

Working with Different Color Spaces

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}")

Industry-Specific Tolerances

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}")

Batch Color Difference Analysis

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}]")

Quality Control Workflow

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 Interpretation Guidelines

General Perception Thresholds

Delta E RangePerception LevelDescription
0-1Not perceptibleColors appear identical
1-2Just perceptibleVery close observation needed
2-5PerceptibleColors appear similar but different
5-10Well perceptibleClear color difference
10+Very differentColors appear as different colors

Industry-Specific Tolerances

IndustryApplicationTypical ToleranceMethod
PrintingCommercial printingΔE < 3-5CIE 2000
TextilesColor matchingΔE < 1-2CMC 2:1
PaintsAutomotive coatingsΔE < 1CIE 2000
DisplaysMonitor calibrationΔE < 2CIE 2000
PhotographyColor reproductionΔE < 2-3CIE 2000

Method Selection Guidelines

When to Use Each Method

  • CIE 1976: Simple applications, historical compatibility
  • CIE 1994: Better than 1976, still widely used in industry
  • CIE 2000: Most accurate, recommended for new applications
  • CMC: Textile industry, neutral color evaluation

Computational Performance

MethodSpeedAccuracyUse Case
CIE 1976FastestLowQuick screening
CIE 1994FastMediumGeneral purpose
CIE 2000SlowerHighQuality control
CMCFastMediumTextiles

Install with Tessl CLI

npx tessl i tessl/pypi-colormath

docs

chromatic-adaptation.md

color-appearance-models.md

color-conversions.md

color-diff.md

color-objects.md

constants-standards.md

index.md

spectral-density.md

tile.json