### Install colormath via pip Source: https://github.com/gtaylor/python-colormath/blob/master/README.rst This snippet shows how to install the colormath Python package using pip. It also includes an option to install development dependencies. ```bash pip install colormath ``` ```bash pip install 'colormath[development]' ``` -------------------------------- ### Implement Advanced Color Appearance Models Source: https://context7.com/gtaylor/python-colormath/llms.txt Provides examples for using alternative color appearance models including Nayatani95 and the Hunt model for specialized color perception research. ```python from colormath.color_appearance_models import Nayatani95, Hunt nayatani = Nayatani95( x=19.01, y=20, z=21.78, x_n=95.05, y_n=100, z_n=108.88, y_ob=20, e_o=1000, e_or=1000 ) hunt = Hunt( x=19.01, y=20, z=21.78, x_b=95.05, y_b=100, z_b=108.88, x_w=95.05, y_w=100, z_w=108.88, l_a=318.31, n_c=1.0, n_b=75 ) ``` -------------------------------- ### Calculate Delta E Color Differences (Python) Source: https://context7.com/gtaylor/python-colormath/llms.txt Provides examples of calculating color differences between two LabColor objects using various Delta E formulas: CIE 1976, CIE 1994 (Graphic Arts and Textiles), CIE 2000, and CMC l:c. These functions are essential for quantifying perceptual color differences and require the colormath library. ```python from colormath.color_objects import LabColor from colormath.color_diff import ( delta_e_cie1976, delta_e_cie1994, delta_e_cie2000, delta_e_cmc ) # Reference color and test color color1 = LabColor(lab_l=50.0, lab_a=25.0, lab_b=-15.0) color2 = LabColor(lab_l=52.0, lab_a=22.0, lab_b=-12.0) # CIE 1976 - Simple Euclidean distance in Lab space de_1976 = delta_e_cie1976(color1, color2) print(f"Delta E CIE 1976: {de_1976:.4f}") # CIE 1994 - Graphic Arts (default parameters) de_1994 = delta_e_cie1994(color1, color2) print(f"Delta E CIE 1994 (Graphic Arts): {de_1994:.4f}") # CIE 1994 - Textiles (different weighting factors) de_1994_textiles = delta_e_cie1994( color1, color2, K_L=2, # Lightness weighting for textiles K_1=0.048, # Chroma weighting for textiles K_2=0.014 # Hue weighting for textiles ) print(f"Delta E CIE 1994 (Textiles): {de_1994_textiles:.4f}") # CIE 2000 - Most perceptually uniform formula de_2000 = delta_e_cie2000(color1, color2) print(f"Delta E CIE 2000: {de_2000:.4f}") # CMC l:c - Commercial color difference # Acceptability (2:1) - looser tolerance de_cmc_accept = delta_e_cmc(color1, color2, pl=2, pc=1) print(f"Delta E CMC (2:1 Acceptability): {de_cmc_accept:.4f}") # Perceptibility (1:1) - stricter tolerance de_cmc_percept = delta_e_cmc(color1, color2, pl=1, pc=1) print(f"Delta E CMC (1:1 Perceptibility): {de_cmc_percept:.4f}") # Practical example: Color matching threshold def colors_match(lab1, lab2, threshold=2.3): """Check if two colors are perceptually similar using CIE 2000.""" delta_e = delta_e_cie2000(lab1, lab2) if delta_e < 1.0: return "Imperceptible difference" elif delta_e < 2.0: return "Very slight difference" elif delta_e < threshold: return "Acceptable match" else: return "Noticeable difference" result = colors_match(color1, color2) print(f"Match result: {result}") ``` -------------------------------- ### Calculate Delta E CIE 1976 using python-colormath Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/delta_e.rst Demonstrates how to calculate the Delta E CIE 1976 difference between two LabColor objects. This function provides a basic measure of color difference, suitable as a starting point for comparisons. ```python 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) # This is your delta E value as a float. delta_e = delta_e_cie1976(color1, color2) ``` -------------------------------- ### Initialize and Print RLAB, ATD95, and LLAB Color Models Source: https://context7.com/gtaylor/python-colormath/llms.txt Demonstrates the initialization of RLAB, ATD95, and LLAB color models with specific parameters and prints key attributes like lightness, chroma, brightness, and saturation. These models are used for perceptual color difference calculations. ```python from colormath.color_objects import RLAB, ATD95, LLAB # RLAB Model rlab = RLAB( x=19.01, y=20, z=21.78, # Test sample x_n=95.05, y_n=100, z_n=108.88, # Reference white y_n_abs=318.31, # Absolute white luminance (cd/m^2) sigma=1/2.3, # Average surround: 1/2.3, Dim: 1/2.9, Dark: 1/3.5 d=1 # Degree of adaptation ) print(f"RLAB - Lightness: {rlab.lightness:.4f}, Chroma: {rlab.chroma:.4f}") # ATD95 Model atd = ATD95( x=19.01, y=20, z=21.78, # Test sample x_0=95.05, y_0=100, z_0=108.88, # Reference white y_0_abs=318.31, # Absolute adapting luminance k_1=0.0, # Adaptation parameter k_2=50.0, # Adaptation parameter sigma=300 # Saturation parameter ) print(f"ATD95 - Brightness: {atd.brightness:.4f}, Saturation: {atd.saturation:.4f}") # LLAB Model llab = LLAB( x=19.01, y=20, z=21.78, # Test sample x_0=95.05, y_0=100, z_0=108.88, # Reference white y_b=20, # Background luminance factor f_s=3, # Surround induction factor f_l=1, # Lightness induction factor f_c=1, # Chroma induction factor l=318.31, # Absolute luminance (cd/m^2) d=1 # Discounting-the-Illuminant factor ) print(f"LLAB - Lightness: {llab.lightness:.4f}, Chroma: {llab.chroma:.4f}") ``` -------------------------------- ### Instantiating Color Objects Source: https://context7.com/gtaylor/python-colormath/llms.txt Demonstrates how to create various color space objects such as Lab, XYZ, sRGB, HSL, and CMYK. It shows how to handle different coordinate ranges, illuminants, and observer settings. ```python from colormath.color_objects import ( LabColor, XYZColor, sRGBColor, AdobeRGBColor, HSLColor, HSVColor, CMYColor, CMYKColor, LuvColor, LCHabColor, LCHuvColor, xyYColor, IPTColor, SpectralColor, BT2020Color, AppleRGBColor ) # CIE Lab color with L, a, b coordinates lab = LabColor(lab_l=50.0, lab_a=25.0, lab_b=-15.0, observer='2', illuminant='d50') # sRGB from upscaled values (0-255) rgb_upscaled = sRGBColor(200, 128, 50, is_upscaled=True) # sRGB from hex string rgb_hex = sRGBColor.new_from_rgb_hex('#ff5733') ``` -------------------------------- ### Initialize and Convert Between RGB Color Spaces Source: https://context7.com/gtaylor/python-colormath/llms.txt Shows how to initialize different RGB color spaces (sRGB, Adobe RGB, Apple RGB, BT.2020) and convert between them using `convert_color`. It also demonstrates handling out-of-gamut values by clamping. ```python from colormath.color_objects import ( sRGBColor, AdobeRGBColor, AppleRGBColor, BT2020Color ) from colormath.color_conversions import convert_color # sRGB (IEC 61966-2-1) - gamma ~2.2, D65 illuminant srgb = sRGBColor(0.5, 0.6, 0.7) print(f"sRGB native illuminant: {srgb.native_illuminant}") # d65 print(f"sRGB gamma: {srgb.rgb_gamma}") # 2.2 # Adobe RGB (1998) - gamma 2.2, D65 illuminant, wider gamut adobe = AdobeRGBColor(0.5, 0.6, 0.7) print(f"Adobe RGB gamma: {adobe.rgb_gamma}") # 2.2 # Apple RGB - gamma 1.8, D65 illuminant apple = AppleRGBColor(0.5, 0.6, 0.7) print(f"Apple RGB gamma: {apple.rgb_gamma}") # 1.8 # BT.2020 (ITU-R BT.2020) - gamma 2.4, D65, HDR/wide color gamut bt2020 = BT2020Color(0.5, 0.6, 0.7) print(f"BT.2020 gamma: {bt2020.rgb_gamma}") # 2.4 # Convert between RGB spaces srgb = sRGBColor(0.8, 0.5, 0.3) adobe_from_srgb = convert_color(srgb, AdobeRGBColor) print(f"sRGB to Adobe RGB: R={adobe_from_srgb.rgb_r:.4f}, G={adobe_from_srgb.rgb_g:.4f}, B={adobe_from_srgb.rgb_b:.4f}") # Clamped RGB values (guaranteed 0-1 range) out_of_gamut = sRGBColor(1.2, -0.1, 0.5) # Out of gamut values print(f"Clamped R: {out_of_gamut.clamped_rgb_r:.4f}") # 1.0 print(f"Clamped G: {out_of_gamut.clamped_rgb_g:.4f}") # 0.0 print(f"Clamped B: {out_of_gamut.clamped_rgb_b:.4f}") # 0.5 ``` -------------------------------- ### Set Observer and Illuminant in LabColor - Python Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/illuminants.rst Demonstrates how to instantiate a LabColor object and specify observer and illuminant values. This is useful for color spaces that rely on reflective light measurements. ```python from colormath.color_objects import LabColor lab = LabColor(0.1, 0.2, 0.3, observer='10', illuminant='d65') ``` -------------------------------- ### Utilize Color Object Utility Methods Source: https://context7.com/gtaylor/python-colormath/llms.txt Demonstrates various utility methods available for color objects, including retrieving value tuples, generating string and representation formats, converting to RGB hex, upscaling RGB values, accessing illuminant XYZ, and modifying illuminant and observer settings. ```python from colormath.color_objects import LabColor, sRGBColor, XYZColor # Get value tuple lab = LabColor(50.0, 25.0, -15.0) values = lab.get_value_tuple() print(f"Lab values tuple: {values}") # (50.0, 25.0, -15.0) # String representations print(str(lab)) # LabColor (lab_l:50.0000 lab_a:25.0000 lab_b:-15.0000 observer:2 illuminant:d50) print(repr(lab)) # LabColor(lab_l=50.0, lab_a=25.0, lab_b=-15.0, observer='2', illuminant='d50') # RGB hex conversion rgb = sRGBColor(0.8, 0.5, 0.2) print(f"Hex: {rgb.get_rgb_hex()}") # #cc7f33 # Upscaled RGB values (0-255) print(f"Upscaled: {rgb.get_upscaled_value_tuple()}") # (204, 128, 51) # Illuminant XYZ values xyz = XYZColor(0.5, 0.5, 0.5, illuminant='d50', observer='2') illum_xyz = xyz.get_illuminant_xyz() print(f"D50 XYZ: X={illum_xyz['X']:.4f}, Y={illum_xyz['Y']:.4f}, Z={illum_xyz['Z']:.4f}") # Change illuminant (metadata only, no conversion) lab = LabColor(50.0, 25.0, -15.0, illuminant='d50') lab.set_illuminant('d65') print(f"New illuminant: {lab.illuminant}") # d65 # Change observer angle lab.set_observer('10') print(f"New observer: {lab.observer}") # 10 ``` -------------------------------- ### Calculate Color Density with colormath Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/density.rst Demonstrates how to calculate ANSI and ISO density for spectral colors using the colormath library. It shows both automatic density calculation and density calculation with a specified ANSI filter. ```python from colormath.color_objects import SpectralColor from colormath.density import auto_density, ansi_density from colormath.density_standards import ANSI_STATUS_T_RED # Omitted the full spectral kwargs for brevity. color = SpectralColor(spec_340nm=0.08, ...) # ANSI T Density for the spectral color. density = auto_density(color) # Or maybe we want to specify which filter to use. red_density = ansi_density(color, ANSI_STATUS_T_RED) ``` -------------------------------- ### Compute CIECAM02 Color Appearance Model Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/color_appearance_models.rst Demonstrates how to initialize the CIECAM02 model using XYZ color coordinates, illuminant data, and viewing condition parameters. The model calculates perceptual correlates based on the provided input stimulus and environmental settings. ```python # Color stimulus color = XYZColor(19.01, 20, 21.78) # The two illuminants that will be compared. illuminant_d65 = XYZColor(95.05, 100, 108.88) illuminant_a = XYZColor(109.85, 100, 35.58) # Background relative luminance y_b = 20 # Adapting luminance l_a = 328.31 # Surround condition assumed to be average c = 0.69 n_c = 1 f = 1 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) ``` -------------------------------- ### Spectral Color and Density Calculations (Python) Source: https://context7.com/gtaylor/python-colormath/llms.txt Illustrates how to create and use SpectralColor objects, which represent spectrophotometer readings. It shows conversion to XYZ color space and calculation of color densities using standard ANSI filters or auto-detection. This functionality is part of the colormath library. ```python from colormath.color_objects import SpectralColor, XYZColor from colormath.color_conversions import convert_color from colormath.density_standards import ( ANSI_STATUS_T_RED, ANSI_STATUS_T_GREEN, ANSI_STATUS_T_BLUE ) from colormath.density import ansi_density, auto_density # Create spectral color from spectrophotometer readings (380nm-730nm) # Values represent reflectance/transmittance factors spectral = SpectralColor( observer='2', illuminant='d50', spec_380nm=0.0600, spec_390nm=0.0600, spec_400nm=0.0641, spec_410nm=0.0654, spec_420nm=0.0645, spec_430nm=0.0605, spec_440nm=0.0562, spec_450nm=0.0543, spec_460nm=0.0537, spec_470nm=0.0541, spec_480nm=0.0559, spec_490nm=0.0603, spec_500nm=0.0651, spec_510nm=0.0680, spec_520nm=0.0705, spec_530nm=0.0736, spec_540nm=0.0772, spec_550nm=0.0809, spec_560nm=0.0870, spec_570nm=0.0990, spec_580nm=0.1128, spec_590nm=0.1251, spec_600nm=0.1360, spec_610nm=0.1439, spec_620nm=0.1511, spec_630nm=0.1590, spec_640nm=0.1688, spec_650nm=0.1828, spec_660nm=0.1996, spec_670nm=0.2187, spec_680nm=0.2397, spec_690nm=0.2618, spec_700nm=0.2852, spec_710nm=0.2500, spec_720nm=0.2400, spec_730nm=0.2300, ) # Convert spectral to XYZ xyz = convert_color(spectral, XYZColor) print(f"XYZ: X={xyz.xyz_x:.4f}, Y={xyz.xyz_y:.4f}, Z={xyz.xyz_z:.4f}") # Calculate density using specific ANSI Status T filters red_density = ansi_density(spectral, ANSI_STATUS_T_RED) green_density = ansi_density(spectral, ANSI_STATUS_T_GREEN) blue_density = ansi_density(spectral, ANSI_STATUS_T_BLUE) print(f"Red Density: {red_density:.4f}") print(f"Green Density: {green_density:.4f}") print(f"Blue Density: {blue_density:.4f}") # Auto-select appropriate density filter based on color density = auto_density(spectral) print(f"Auto Density: {density:.4f}") # Calculate density directly from spectral object density_from_object = spectral.calc_density() print(f"Density (from object): {density_from_object:.4f}") ``` -------------------------------- ### Convert between color spaces using convert_color Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/conversions.rst Demonstrates the basic usage of the convert_color function to transform a color instance from one space to another, such as CIE Lab to CIE XYZ. ```python from colormath.color_objects import LabColor, XYZColor from colormath.color_conversions import convert_color lab = LabColor(0.903, 16.296, -2.22) xyz = convert_color(lab, XYZColor) ``` -------------------------------- ### Perform Chromatic Adaptation with colormath Source: https://context7.com/gtaylor/python-colormath/llms.txt Demonstrates how to transform XYZ color coordinates between different illuminants using the Bradford, von Kries, or XYZ scaling methods. It shows both direct coordinate transformation and in-place adaptation of XYZColor objects. ```python from colormath.color_objects import XYZColor from colormath.chromatic_adaptation import ( apply_chromatic_adaptation, apply_chromatic_adaptation_on_color ) xyz_x, xyz_y, xyz_z = 0.5, 0.5, 0.4 adapted_x, adapted_y, adapted_z = apply_chromatic_adaptation( xyz_x, xyz_y, xyz_z, orig_illum='d65', targ_illum='d50', observer='2', adaptation='bradford' ) xyz_color = XYZColor(0.5, 0.5, 0.4, illuminant='d65', observer='2') xyz_color.apply_adaptation('d50', adaptation='bradford') xyz_color2 = XYZColor(0.3, 0.4, 0.5, illuminant='d50', observer='2') apply_chromatic_adaptation_on_color(xyz_color2, targ_illum='a', adaptation='bradford') ``` -------------------------------- ### Convert RGB to XYZ with Target Illuminant (Python) Source: https://context7.com/gtaylor/python-colormath/llms.txt Demonstrates converting an sRGB color to the CIE XYZ color space, specifying a target illuminant like 'd50'. This is useful for accurate color reproduction across different lighting conditions. It requires the colormath library. ```python from colormath.color_objects import sRGBColor, XYZColor from colormath.color_conversions import convert_color rgb = sRGBColor(0.5, 0.6, 0.7) xyz_d50 = convert_color(rgb, XYZColor, target_illuminant='d50') print(f"XYZ (D50): X={xyz_d50.xyz_x:.4f}, Y={xyz_d50.xyz_y:.4f}, Z={xyz_d50.xyz_z:.4f}") ``` -------------------------------- ### Valid Density Constants Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/density.rst Lists the available constants that can be used with the `ansi_density` function. ```APIDOC ## Valid Density Constants ### Description These constants are defined in the `colormath.density_standards` module and can be passed as the `filter` argument to the `ansi_density` function. ### Constants - ``ANSI_STATUS_A_RED`` - ``ANSI_STATUS_A_GREEN`` - ``ANSI_STATUS_A_BLUE`` - ``ANSI_STATUS_E_RED`` - ``ANSI_STATUS_E_GREEN`` - ``ANSI_STATUS_E_BLUE`` - ``ANSI_STATUS_M_RED`` - ``ANSI_STATUS_M_GREEN`` - ``ANSI_STATUS_M_BLUE`` - ``ANSI_STATUS_T_RED`` - ``ANSI_STATUS_T_GREEN`` - ``ANSI_STATUS_T_BLUE`` - ``TYPE1`` - ``TYPE2`` - ``ISO_VISUAL`` ``` -------------------------------- ### Calculate Perceptual Attributes with CIECAM02 Source: https://context7.com/gtaylor/python-colormath/llms.txt Uses the CIECAM02 model to predict color appearance attributes such as lightness, chroma, and hue under specific viewing conditions. It demonstrates how background luminance and adapting luminance influence these perceptual correlates. ```python from colormath.color_appearance_models import CIECAM02 from colormath.color_objects import XYZColor stimulus = XYZColor(19.01, 20, 21.78) white_point = XYZColor(95.05, 100, 108.88) model = CIECAM02( x=stimulus.xyz_x, y=stimulus.xyz_y, z=stimulus.xyz_z, x_w=white_point.xyz_x, y_w=white_point.xyz_y, z_w=white_point.xyz_z, y_b=20, l_a=64, c=0.69, n_c=1.0, f=1.0 ) print(f"Lightness (J): {model.lightness:.4f}") print(f"Chroma (C): {model.chroma:.4f}") ``` -------------------------------- ### Perform color conversion through a specific RGB space Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/conversions.rst Shows how to specify an intermediate RGB color space (like AdobeRGB) when performing conversions that require an RGB bridge, ensuring consistency in round-trip conversions. ```python from colormath.color_objects import XYZColor, HSLColor, AdobeRGBColor from colormath.color_conversions import convert_color xyz = XYZColor(0.1, 0.2, 0.3) hsl = convert_color(xyz, HSLColor, through_rgb_type=AdobeRGBColor) xyz2 = convert_color(hsl, XYZColor, through_rgb_type=AdobeRGBColor) ``` -------------------------------- ### Specify target illuminant during color conversion Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/conversions.rst Illustrates how to override the default native illuminant of an RGB space by providing the target_illuminant keyword argument to the convert_color function. ```python from colormath.color_objects import XYZColor, sRGBColor from colormath.color_conversions import convert_color rgb = RGBColor(0.1, 0.2, 0.3) xyz = convert_color(rgb, XYZColor, target_illuminant='d50') ``` -------------------------------- ### Additional Color Appearance Models Source: https://context7.com/gtaylor/python-colormath/llms.txt Provides access to other color appearance models for advanced color perception research. ```APIDOC ## Additional Color Appearance Models The library includes several other color appearance models for advanced color perception research. ### Nayatani95 Model - `x`, `y`, `z` (float): Test sample XYZ tristimulus values. - `x_n`, `y_n`, `z_n` (float): Reference white XYZ tristimulus values. - `y_ob` (float): Background luminance factor (>0.18). - `e_o` (float): Viewing field illuminance (lux). - `e_or` (float): Normalizing illuminance (lux). ### Hunt Model - `x`, `y`, `z` (float): Test sample XYZ tristimulus values. - `x_b`, `y_b`, `z_b` (float): Background XYZ tristimulus values. - `x_w`, `y_w`, `z_w` (float): Reference white XYZ tristimulus values. - `l_a` (float): Adapting luminance. - `n_c` (float): Chromatic surround induction factor. - `n_b` (float): Brightness surround induction factor. ### Example Usage ```python from colormath.color_appearance_models import ( Nayatani95, Hunt ) # Nayatani95 Model nayatani = Nayatani95( x=19.01, y=20, z=21.78, # Test sample XYZ x_n=95.05, y_n=100, z_n=108.88, # Reference white XYZ y_ob=20, # Background luminance factor (>0.18) e_o=1000, # Viewing field illuminance (lux) e_or=1000 # Normalizing illuminance (lux) ) print(f"Nayatani95 - Brightness: {nayatani.brightness:.4f}, Chroma: {nayatani.chroma:.4f}") # Hunt Model hunt = Hunt( x=19.01, y=20, z=21.78, # Test sample x_b=95.05, y_b=100, z_b=108.88, # Background x_w=95.05, y_w=100, z_w=108.88, # Reference white l_a=318.31, # Adapting luminance n_c=1.0, # Chromatic surround induction n_b=75 # Brightness surround induction ) print(f"Hunt - Lightness: {hunt.lightness:.4f}, Chroma: {hunt.chroma:.4f}") ``` ``` -------------------------------- ### Density Calculation API Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/density.rst This section covers the functions for calculating color density. Density can be calculated from LabColor or SpectralColor instances. ```APIDOC ## Density Calculation Functions ### Description Provides functions to calculate ANSI and ISO density from color objects. ### Functions - `colormath.density.auto_density(color)`: Automatically calculates the density for a given color object. - `colormath.density.ansi_density(color, filter)`: Calculates the ANSI density for a given color object using a specified filter constant. ### Parameters #### `auto_density` - **color** (LabColor or SpectralColor) - Required - The color object to calculate density from. #### `ansi_density` - **color** (LabColor or SpectralColor) - Required - The color object to calculate density from. - **filter** (DensityConstant) - Required - A constant from `colormath.density_standards` specifying the filter to use. ### Request Example ```python from colormath.color_objects import SpectralColor from colormath.density import auto_density, ansi_density from colormath.density_standards import ANSI_STATUS_T_RED # Example using auto_density color = SpectralColor(spec_340nm=0.08, ...) density = auto_density(color) # Example using ansi_density with a specific filter red_density = ansi_density(color, ANSI_STATUS_T_RED) ``` ### Response #### Success Response (200) - **density** (float) - The calculated density value. ### Response Example ```json { "density": 0.5678 } ``` ``` -------------------------------- ### CIECAM02 Color Appearance Model Source: https://context7.com/gtaylor/python-colormath/llms.txt Predicts how colors appear under different viewing conditions, providing perceptual attributes like lightness, chroma, and hue. ```APIDOC ## CIECAM02 Color Appearance Model CIECAM02 predicts how colors appear under different viewing conditions, providing perceptual attributes like lightness, chroma, hue, brightness, colorfulness, and saturation. ### Model Parameters - `x`, `y`, `z` (float): XYZ tristimulus values of the stimulus. - `x_w`, `y_w`, `z_w` (float): XYZ tristimulus values of the reference white. - `y_b` (float): Background relative luminance. - `l_a` (float): Adapting luminance in cd/m^2. - `c` (float): Exponential nonlinearity (surround parameter, e.g., 0.69 for average). - `n_c` (float): Chromatic induction factor. - `f` (float): Maximum degree of adaptation. ### Perceptual Correlates - `lightness` (J) - `brightness` (Q) - `chroma` (C) - `colorfulness` (M) - `saturation` (s) - `hue_angle` (h) - `a` (red-green opponent dimension) - `b` (yellow-blue opponent dimension) ### Example Usage ```python from colormath.color_appearance_models import CIECAM02 from colormath.color_objects import XYZColor # Define stimulus and viewing conditions stimulus = XYZColor(19.01, 20, 21.78) white_point = XYZColor(95.05, 100, 108.88) # D65 reference white # Surround parameters for average viewing conditions # Average: c=0.69, N_c=1.0, F=1.0 # Dim: c=0.59, N_c=0.9, F=0.9 # Dark: c=0.525, N_c=0.8, F=0.8 # Create CIECAM02 model model = CIECAM02( x=stimulus.xyz_x, y=stimulus.xyz_y, z=stimulus.xyz_z, x_w=white_point.xyz_x, y_w=white_point.xyz_y, z_w=white_point.xyz_z, y_b=20, # Background relative luminance l_a=64, # Adapting luminance in cd/m^2 c=0.69, # Exponential nonlinearity (average surround) n_c=1.0, # Chromatic induction factor f=1.0 # Maximum degree of adaptation ) # Access perceptual correlates print(f"Lightness (J): {model.lightness:.4f}") print(f"Brightness (Q): {model.brightness:.4f}") print(f"Chroma (C): {model.chroma:.4f}") print(f"Colorfulness (M): {model.colorfulness:.4f}") print(f"Saturation (s): {model.saturation:.4f}") print(f"Hue angle (h): {model.hue_angle:.4f}") # Opponent color dimensions print(f"a (red-green): {model.a:.4f}") print(f"b (yellow-blue): {model.b:.4f}") # Compare different background luminances model_dark = CIECAM02( x=stimulus.xyz_x, y=stimulus.xyz_y, z=stimulus.xyz_z, x_w=white_point.xyz_x, y_w=white_point.xyz_y, z_w=white_point.xyz_z, y_b=10, l_a=64, c=0.69, n_c=1.0, f=1.0 ) model_light = CIECAM02( x=stimulus.xyz_x, y=stimulus.xyz_y, z=stimulus.xyz_z, x_w=white_point.xyz_x, y_w=white_point.xyz_y, z_w=white_point.xyz_z, y_b=100, l_a=64, c=0.69, n_c=1.0, f=1.0 ) print(f"\nDark background (Y_b=10): J={model_dark.lightness:.2f}, C={model_dark.chroma:.2f}") print(f"Light background (Y_b=100): J={model_light.lightness:.2f}, C={model_light.chroma:.2f}") ``` ``` -------------------------------- ### Performing Color Space Conversions Source: https://context7.com/gtaylor/python-colormath/llms.txt Uses the convert_color function to transform between different color spaces. The library automatically computes the optimal path and handles chromatic adaptation where necessary. ```python from colormath.color_conversions import convert_color from colormath.color_objects import LabColor, XYZColor, sRGBColor, CMYKColor # Lab to XYZ conversion lab = LabColor(lab_l=50.0, lab_a=25.0, lab_b=-15.0) xyz = convert_color(lab, XYZColor) # RGB to CMYK conversion rgb = sRGBColor(0.8, 0.3, 0.1) cmyk = convert_color(rgb, CMYKColor) # Converting between RGB color spaces srgb = sRGBColor(0.5, 0.6, 0.7) adobe_rgb = convert_color(srgb, AdobeRGBColor) ``` -------------------------------- ### Chromatic Adaptation Source: https://context7.com/gtaylor/python-colormath/llms.txt Transforms colors between different illuminants (white points) using methods like Bradford or Von Kries. Essential for converting colors between spaces with different reference whites. ```APIDOC ## Chromatic Adaptation Chromatic adaptation transforms colors between different illuminants (white points). This is essential when converting between color spaces with different reference whites. ### Direct XYZ Chromatic Adaptation This function directly adapts XYZ color values from one illuminant to another. #### Parameters - `xyz_x` (float) - The X component of the XYZ color. - `xyz_y` (float) - The Y component of the XYZ color. - `xyz_z` (float) - The Z component of the XYZ color. - `orig_illum` (str) - The original illuminant (e.g., 'd65', 'd50'). - `targ_illum` (str) - The target illuminant (e.g., 'd50', 'a'). - `observer` (str) - The observer type ('2' for 2-degree, '10' for 10-degree). - `adaptation` (str) - The adaptation method ('bradford', 'von_kries', 'xyz_scaling'). #### Returns - `adapted_x` (float) - The adapted X component. - `adapted_y` (float) - The adapted Y component. - `adapted_z` (float) - The adapted Z component. ### Apply Chromatic Adaptation to Color Object This method modifies a color object in place, adapting its XYZ values to a new illuminant. #### Parameters - `targ_illum` (str) - The target illuminant (e.g., 'd50', 'a'). - `adaptation` (str) - The adaptation method ('bradford', 'von_kries', 'xyz_scaling'). ### Convenience Function for Color Object Adaptation A convenience function to apply chromatic adaptation directly to a color object. #### Parameters - `color_object` (XYZColor) - The color object to adapt. - `targ_illum` (str) - The target illuminant (e.g., 'd50', 'a'). - `adaptation` (str) - The adaptation method ('bradford', 'von_kries', 'xyz_scaling'). ### Example Usage ```python from colormath.color_objects import XYZColor from colormath.chromatic_adaptation import ( apply_chromatic_adaptation, apply_chromatic_adaptation_on_color ) # Direct XYZ chromatic adaptation xyz_x, xyz_y, xyz_z = 0.5, 0.5, 0.4 # Transform from D65 to D50 illuminant using Bradford adaptation adapted_x, adapted_y, adapted_z = apply_chromatic_adaptation( xyz_x, xyz_y, xyz_z, orig_illum='d65', targ_illum='d50', observer='2', adaptation='bradford' # Options: 'bradford', 'von_kries', 'xyz_scaling' ) print(f"Original (D65): X={xyz_x}, Y={xyz_y}, Z={xyz_z}") print(f"Adapted (D50): X={adapted_x:.4f}, Y={adapted_y:.4f}, Z={adapted_z:.4f}") # Apply adaptation directly to a color object xyz_color = XYZColor(0.5, 0.5, 0.4, illuminant='d65', observer='2') print(f"Before: {xyz_color}") # This modifies the color object in place xyz_color.apply_adaptation('d50', adaptation='bradford') print(f"After (D50): X={xyz_color.xyz_x:.4f}, Y={xyz_color.xyz_y:.4f}, Z={xyz_color.xyz_z:.4f}") print(f"New illuminant: {xyz_color.illuminant}") # Alternative: using convenience function xyz_color2 = XYZColor(0.3, 0.4, 0.5, illuminant='d50', observer='2') apply_chromatic_adaptation_on_color(xyz_color2, targ_illum='a', adaptation='bradford') print(f"Adapted to illuminant A: X={xyz_color2.xyz_x:.4f}, Y={xyz_color2.xyz_y:.4f}, Z={xyz_color2.xyz_z:.4f}") ``` -------------------------------- ### Calculate Delta E Source: https://github.com/gtaylor/python-colormath/blob/master/doc_src/delta_e.rst Functions to compute the numerical difference between two LabColor objects using different industry-standard equations. ```APIDOC ## Delta E Calculation Functions ### Description These functions calculate the Delta E value, representing the visual difference between two LabColor instances. Supported algorithms include CIE 1976, CIE 1994, CIE 2000, and CMC. ### Method Python Function Call ### Parameters #### Arguments - **color1** (LabColor) - Required - The reference color object. - **color2** (LabColor) - Required - The color object to compare against the reference. ### Request Example from colormath.color_objects import LabColor from colormath.color_diff import delta_e_cie1976 color1 = LabColor(lab_l=0.9, lab_a=16.3, lab_b=-2.22) color2 = LabColor(lab_l=0.7, lab_a=14.2, lab_b=-1.80) delta_e = delta_e_cie1976(color1, color2) ### Response #### Success Response (float) - **delta_e** (float) - The calculated numerical difference between the two colors. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.