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
Spectral color and density functionality provides comprehensive support for working with spectral power distributions and calculating color densities using industry standards. This is essential for applications in printing, color reproduction, and spectroscopic analysis.
Automatically determine the best density standard and calculate density for a spectral color.
def auto_density(color):
"""
Automatically calculate density for spectral color.
Parameters:
- color: SpectralColor object with wavelength data
Returns:
float: Calculated density value
Notes:
- Automatically selects appropriate density standard
- Uses visual density threshold for standard selection
- Optimized for typical printing and reproduction applications
"""Calculate density using specific ANSI density standards for precise control over measurement conditions.
def ansi_density(color, density_standard):
"""
Calculate density with specific ANSI standard.
Parameters:
- color: SpectralColor object with wavelength data
- density_standard: Density standard dictionary (from density_standards module)
Returns:
float: Calculated density value using specified standard
Notes:
- Provides precise control over density measurement
- Supports all ANSI status standards (T, A, E, M)
- Essential for color reproduction and printing applications
"""Spectral colors represent full spectral power distributions from 340nm to 830nm in 10nm intervals.
from colormath.color_objects import SpectralColor
# Create spectral color with wavelength data
spectral = SpectralColor(
spec_400nm=0.064, # 400nm
spec_410nm=0.065, # 410nm
spec_420nm=0.066, # 420nm
spec_430nm=0.067, # 430nm
spec_440nm=0.068, # 440nm
spec_450nm=0.069, # 450nm
# Continue for all wavelengths...
spec_680nm=0.102, # 680nm
spec_690nm=0.105, # 690nm
spec_700nm=0.108, # 700nm
observer='2',
illuminant='d65'
)from colormath.color_objects import SpectralColor
# Create spectral color
spectral = SpectralColor(
spec_400nm=0.064,
spec_410nm=0.065,
# ... more wavelength data
spec_700nm=0.108
)
# Calculate density using built-in method
density = spectral.calc_density()
# Convert to NumPy array for analysis
import numpy as np
spectral_array = spectral.get_numpy_array()
# Get wavelength values
wavelengths = np.arange(340, 840, 10) # 340-830nm in 10nm stepsStatus T standards for transmission density measurements.
from colormath.density_standards import (
ANSI_STATUS_T_RED,
ANSI_STATUS_T_GREEN,
ANSI_STATUS_T_BLUE
)
from colormath.density import ansi_density
# Calculate density using Status T Red
density_red = ansi_density(spectral_color, ANSI_STATUS_T_RED)
density_green = ansi_density(spectral_color, ANSI_STATUS_T_GREEN)
density_blue = ansi_density(spectral_color, ANSI_STATUS_T_BLUE)Status A standards for reflection density measurements.
from colormath.density_standards import (
ANSI_STATUS_A_RED,
ANSI_STATUS_A_GREEN,
ANSI_STATUS_A_BLUE
)
from colormath.density import ansi_density
# Calculate reflection densities
density_a_red = ansi_density(spectral_color, ANSI_STATUS_A_RED)
density_a_green = ansi_density(spectral_color, ANSI_STATUS_A_GREEN)
density_a_blue = ansi_density(spectral_color, ANSI_STATUS_A_BLUE)Status E standards for photographic applications.
from colormath.density_standards import (
ANSI_STATUS_E_RED,
ANSI_STATUS_E_GREEN,
ANSI_STATUS_E_BLUE
)Status M standards for motion picture applications.
from colormath.density_standards import (
ANSI_STATUS_M_RED,
ANSI_STATUS_M_GREEN,
ANSI_STATUS_M_BLUE
)ISO visual density standard for visual assessment.
from colormath.density_standards import ISO_VISUAL, VISUAL_DENSITY_THRESH
from colormath.density import ansi_density, auto_density
# Use ISO visual standard
density_visual = ansi_density(spectral_color, ISO_VISUAL)
# Visual density threshold for automatic selection
threshold = VISUAL_DENSITY_THRESHfrom colormath.color_objects import SpectralColor, XYZColor
from colormath.color_conversions import convert_color
from colormath.density import auto_density, ansi_density
from colormath.density_standards import ANSI_STATUS_T_RED
# Create spectral color from measurement data
spectral_data = {
'spec_400nm': 0.064, 'spec_410nm': 0.065, 'spec_420nm': 0.066,
'spec_430nm': 0.067, 'spec_440nm': 0.068, 'spec_450nm': 0.069,
'spec_460nm': 0.070, 'spec_470nm': 0.071, 'spec_480nm': 0.072,
'spec_490nm': 0.073, 'spec_500nm': 0.074, 'spec_510nm': 0.075,
'spec_520nm': 0.076, 'spec_530nm': 0.077, 'spec_540nm': 0.078,
'spec_550nm': 0.079, 'spec_560nm': 0.080, 'spec_570nm': 0.081,
'spec_580nm': 0.082, 'spec_590nm': 0.083, 'spec_600nm': 0.084,
'spec_610nm': 0.085, 'spec_620nm': 0.086, 'spec_630nm': 0.087,
'spec_640nm': 0.088, 'spec_650nm': 0.089, 'spec_660nm': 0.090,
'spec_670nm': 0.091, 'spec_680nm': 0.092, 'spec_690nm': 0.093,
'spec_700nm': 0.094
}
spectral = SpectralColor(observer='2', illuminant='d50', **spectral_data)
# Calculate densities
auto_dens = auto_density(spectral)
ansi_dens = ansi_density(spectral, ANSI_STATUS_T_RED)
# Convert to tristimulus values
xyz = convert_color(spectral, XYZColor)
print(f"Auto density: {auto_dens:.3f}")
print(f"ANSI T Red density: {ansi_dens:.3f}")
print(f"XYZ: X={xyz.xyz_x:.3f}, Y={xyz.xyz_y:.3f}, Z={xyz.xyz_z:.3f}")from colormath.color_objects import SpectralColor
from colormath.density import ansi_density
from colormath.density_standards import (
ANSI_STATUS_T_RED, ANSI_STATUS_T_GREEN, ANSI_STATUS_T_BLUE
)
def analyze_print_density(spectral_measurements):
"""
Analyze print density for CMYK quality control.
Parameters:
- spectral_measurements: List of SpectralColor objects
Returns:
dict: Density analysis results
"""
results = []
for i, spectral in enumerate(spectral_measurements):
density_red = ansi_density(spectral, ANSI_STATUS_T_RED)
density_green = ansi_density(spectral, ANSI_STATUS_T_GREEN)
density_blue = ansi_density(spectral, ANSI_STATUS_T_BLUE)
results.append({
'patch': i + 1,
'red_density': density_red,
'green_density': density_green,
'blue_density': density_blue,
'max_density': max(density_red, density_green, density_blue)
})
return results
# Example usage with multiple spectral measurements
spectral_patches = [spectral1, spectral2, spectral3] # List of SpectralColor objects
density_analysis = analyze_print_density(spectral_patches)
for result in density_analysis:
print(f"Patch {result['patch']}: Max density = {result['max_density']:.3f}")import numpy as np
from colormath.color_objects import SpectralColor
def create_spectral_from_array(wavelengths, reflectances, observer='2', illuminant='d65'):
"""
Create SpectralColor from wavelength and reflectance arrays.
Parameters:
- wavelengths: Array of wavelengths (nm)
- reflectances: Array of reflectance values
- observer: Observer angle
- illuminant: Illuminant specification
Returns:
SpectralColor object
"""
# Interpolate to standard 10nm intervals from 340-830nm
standard_wavelengths = np.arange(340, 840, 10)
interpolated_reflectances = np.interp(standard_wavelengths, wavelengths, reflectances)
# Create spectral data dictionary
spectral_data = {}
for wl, refl in zip(standard_wavelengths, interpolated_reflectances):
spectral_data[f'spec_{wl}nm'] = refl
return SpectralColor(observer=observer, illuminant=illuminant, **spectral_data)
# Example usage
measured_wavelengths = np.array([380, 400, 420, 440, 460, 480, 500, 520, 540, 560, 580, 600, 620, 640, 660, 680, 700, 720])
measured_reflectances = np.array([0.05, 0.06, 0.07, 0.08, 0.09, 0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22])
spectral_color = create_spectral_from_array(measured_wavelengths, measured_reflectances)
density = auto_density(spectral_color)from colormath.density_standards import *
from colormath.density import ansi_density
def compare_density_standards(spectral_color):
"""
Compare density calculations across different standards.
Parameters:
- spectral_color: SpectralColor object
Returns:
dict: Density values for different standards
"""
standards = {
'Status T Red': ANSI_STATUS_T_RED,
'Status T Green': ANSI_STATUS_T_GREEN,
'Status T Blue': ANSI_STATUS_T_BLUE,
'Status A Red': ANSI_STATUS_A_RED,
'Status A Green': ANSI_STATUS_A_GREEN,
'Status A Blue': ANSI_STATUS_A_BLUE,
'ISO Visual': ISO_VISUAL
}
results = {}
for name, standard in standards.items():
try:
density = ansi_density(spectral_color, standard)
results[name] = density
except Exception as e:
results[name] = f"Error: {e}"
return results
# Example usage
density_comparison = compare_density_standards(spectral_color)
for standard, density in density_comparison.items():
print(f"{standard}: {density}")Each density standard contains:
Install with Tessl CLI
npx tessl i tessl/pypi-colormath